Source:
public void testNetworksConnectContainer() throws Exception { requireDockerApiVersionAtLeast("1.21", "createNetwork and listNetworks"); assumeFalse(CIRCLECI); final String networkName = randomName(); final String containerName = randomName(); final NetworkCreation networkCreation = sut.createNetwork(NetworkConfig.builder().name(networkName).build()); assertThat(networkCreation.id(), is(notNullValue())); final ContainerConfig containerConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done") .build(); final ContainerCreation containerCreation = sut.createContainer(containerConfig, containerName); assertThat(containerCreation.id(), is(notNullValue())); sut.startContainer(containerCreation.id()); sut.connectToNetwork(containerCreation.id(), networkCreation.id()); Network network = sut.inspectNetwork(networkCreation.id()); assertThat(network.containers().size(), equalTo(1)); assertThat(network.containers().get(containerCreation.id()), notNullValue()); final ContainerInfo containerInfo = sut.inspectContainer(containerCreation.id()); assertThat(containerInfo.networkSettings().networks().size(), is(2)); final AttachedNetwork attachedNetwork = containerInfo.networkSettings().networks().get(networkName); assertThat(attachedNetwork, is(notNullValue())); assertThat(attachedNetwork.networkId(), is(notNullValue())); assertThat(attachedNetwork.endpointId(), is(notNullValue())); assertThat(attachedNetwork.gateway(), is(notNullValue())); assertThat(attachedNetwork.ipAddress(), is(notNullValue())); assertThat(attachedNetwork.ipPrefixLen(), is(notNullValue())); assertThat(attachedNetwork.macAddress(), is(notNullValue())); assertThat(attachedNetwork.ipv6Gateway(), is(notNullValue())); assertThat(attachedNetwork.globalIPv6Address(), is(notNullValue())); assertThat(attachedNetwork.globalIPv6PrefixLen(), greaterThanOrEqualTo(0)); sut.disconnectFromNetwork(containerCreation.id(), networkCreation.id()); network = sut.inspectNetwork(networkCreation.id()); assertThat(network.containers().size(), equalTo(0)); sut.stopContainer(containerCreation.id(), 1); sut.removeContainer(containerCreation.id()); sut.removeNetwork(networkCreation.id()); }

Target:
public void testNetworksConnectContainer() throws Exception { requireDockerApiVersionAtLeast("1.21", "createNetwork and listNetworks"); assumeFalse(CIRCLECI); final String networkName = randomName(); final String containerName = randomName(); final NetworkCreation networkCreation = sut.createNetwork(NetworkConfig.builder().name(networkName).build()); assertThat(networkCreation.id(), is(notNullValue())); final ContainerConfig containerConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done") .build(); final ContainerCreation containerCreation = sut.createContainer(containerConfig, containerName); assertThat(containerCreation.id(), is(notNullValue())); sut.startContainer(containerCreation.id()); sut.connectToNetwork(containerCreation.id(), networkCreation.id()); Network network = sut.inspectNetwork(networkCreation.id()); assertThat(network.containers().size(), equalTo(1)); assertThat(network.containers().get(containerCreation.id()), notNullValue()); final ContainerInfo containerInfo = sut.inspectContainer(containerCreation.id()); assertThat(containerInfo.networkSettings().networks().size(), is(2)); final AttachedNetwork attachedNetwork = containerInfo.networkSettings().networks().get(networkName); assertThat(attachedNetwork, is(notNullValue())); if (dockerApiVersionAtLeast("1.22")) { assertThat(attachedNetwork.networkId(), is(notNullValue())); } assertThat(attachedNetwork.endpointId(), is(notNullValue())); assertThat(attachedNetwork.gateway(), is(notNullValue())); assertThat(attachedNetwork.ipAddress(), is(notNullValue())); assertThat(attachedNetwork.ipPrefixLen(), is(notNullValue())); assertThat(attachedNetwork.macAddress(), is(notNullValue())); assertThat(attachedNetwork.ipv6Gateway(), is(notNullValue())); assertThat(attachedNetwork.globalIPv6Address(), is(notNullValue())); assertThat(attachedNetwork.globalIPv6PrefixLen(), greaterThanOrEqualTo(0)); sut.disconnectFromNetwork(containerCreation.id(), networkCreation.id()); network = sut.inspectNetwork(networkCreation.id()); assertThat(network.containers().size(), equalTo(0)); sut.stopContainer(containerCreation.id(), 1); sut.removeContainer(containerCreation.id()); sut.removeNetwork(networkCreation.id()); }