private void removeNetwork(String id) { try { List<Network> networks; try { // Try to find the network if it still exists // Listing by ID first prevents docker-java logging an error if we just go blindly into removeNetworkCmd networks = dockerClient.listNetworksCmd().withIdFilter(id).exec(); } catch (Exception e) { LOGGER.trace("Error encountered when looking up network for removal (name: {}) - it may not have been removed", id); return; } // at this point networks should contain either 0 or 1 entries, depending on whether the network exists // using a for loop we essentially treat the network like an optional, only applying the removal if it exists for (Network network : networks) { try { dockerClient.removeNetworkCmd(network.getId()).exec(); registeredNetworks.remove(network.getId()); LOGGER.debug("Removed network: {}", id); } catch (Exception e) { LOGGER.trace("Error encountered removing network (name: {}) - it may not have been removed", network.getName()); } } } finally { registeredNetworks.remove(id); } }
/** * @param networkName the name of the network * @deprecated see {@link ResourceReaper#registerNetworkIdForCleanup(String)} */ @Deprecated public void registerNetworkForCleanup(String networkName) { try { // Try to find the network by name, so that we can register its ID for later deletion dockerClient.listNetworksCmd() .withNameFilter(networkName) .exec() .forEach(network -> registerNetworkIdForCleanup(network.getId())); } catch (Exception e) { LOGGER.trace("Error encountered when looking up network (name: {})", networkName); } }
@Test public void testBuilder() throws Exception { try ( Network network = Network.builder() .driver("macvlan") .build(); ) { String id = network.getId(); assertEquals( "Flag is set", "macvlan", DockerClientFactory.instance().client().inspectNetworkCmd().withNetworkId(id).exec().getDriver() ); } }
@Test public void container_should_be_attached_to_front_network() { final InspectContainerResponse pingpong = dockerClient.inspectContainerCmd("pingpong").exec(); final Network front = dockerClient.inspectNetworkCmd().withNetworkId("front").exec(); assertThat(front.getContainers()).containsOnlyKeys(pingpong.getId()); }
@Test public void network_should_be_used_provided_ipam() { final Network app_net = dockerClient.inspectNetworkCmd().withNetworkId("app_net").exec(); assertThat(app_net.getIpam().getDriver()).isEqualTo("default"); assertThat(app_net.getIpam().getConfig()).extracting("subnet", "gateway") .contains(Tuple.tuple("172.16.238.0/24", "172.16.238.1"), Tuple.tuple("2001:3984:3989::/64", "2001:3984:3989::1")); }
@Test public void network_should_be_start_with_driver_opts() { final Network app_net = dockerClient.inspectNetworkCmd().withNetworkId("app_net").exec(); assertThat(app_net.getOptions()).containsEntry("com.docker.network.enable_ipv6", "true"); }
@Test public void testReusability() throws Exception { try (Network network = Network.newNetwork()) { String firstId = network.getId(); assertNotNull( "Network exists", DockerClientFactory.instance().client().inspectNetworkCmd().withNetworkId(firstId).exec() ); network.close(); assertNotEquals( "New network created", firstId, DockerClientFactory.instance().client().inspectNetworkCmd().withNetworkId(network.getId()).exec().getId() ); } } }
@Test public void testModifiers() throws Exception { try ( Network network = Network.builder() .createNetworkCmdModifier(cmd -> cmd.withDriver("macvlan")) .build(); ) { String id = network.getId(); assertEquals( "Flag is set", "macvlan", DockerClientFactory.instance().client().inspectNetworkCmd().withNetworkId(id).exec().getDriver() ); } }
@Test public void container_should_be_attached_to_back_network() { final InspectContainerResponse pingpong = dockerClient.inspectContainerCmd("pingpong").exec(); final Network front = dockerClient.inspectNetworkCmd().withNetworkId("back").exec(); assertThat(front.getContainers()).containsOnlyKeys(pingpong.getId()); }
private void removeNetwork(String id) { try { List<Network> networks; try { // Try to find the network if it still exists // Listing by ID first prevents docker-java logging an error if we just go blindly into removeNetworkCmd networks = dockerClient.listNetworksCmd().withIdFilter(id).exec(); } catch (Exception e) { LOGGER.trace("Error encountered when looking up network for removal (name: {}) - it may not have been removed", id); return; } // at this point networks should contain either 0 or 1 entries, depending on whether the network exists // using a for loop we essentially treat the network like an optional, only applying the removal if it exists for (Network network : networks) { try { dockerClient.removeNetworkCmd(network.getId()).exec(); registeredNetworks.remove(network.getId()); LOGGER.debug("Removed network: {}", id); } catch (Exception e) { LOGGER.trace("Error encountered removing network (name: {}) - it may not have been removed", network.getName()); } } } finally { registeredNetworks.remove(id); } }