public static Map<Integer, Integer> dockerPortMappingsFor( JcloudsLocation docker, String containerId) { ComputeServiceContext context = null; try { Properties properties = new Properties(); properties.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, Boolean.toString(true)); properties.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, Boolean.toString(true)); context = ContextBuilder.newBuilder("docker") .endpoint(docker.getEndpoint()) .credentials(docker.getIdentity(), docker.getCredential()) .overrides(properties) .modules(ImmutableSet.<Module>of(new SLF4JLoggingModule(), new SshjSshClientModule())) .build(ComputeServiceContext.class); DockerApi api = context.unwrapApi(DockerApi.class); Container container = api.getContainerApi().inspectContainer(containerId); Map<Integer, Integer> portMappings = Maps.newLinkedHashMap(); Map<String, List<Map<String, String>>> ports = container.networkSettings().ports(); if (ports == null) ports = ImmutableMap.<String, List<Map<String, String>>>of(); LOG.debug("Docker will forward these ports {}", ports); for (Map.Entry<String, List<Map<String, String>>> entrySet : ports.entrySet()) { String containerPort = Iterables.get(Splitter.on("/").split(entrySet.getKey()), 0); String hostPort = Iterables.getOnlyElement( Iterables.transform( entrySet.getValue(), new Function<Map<String, String>, String>() { @Override public String apply(Map<String, String> hostIpAndPort) { return hostIpAndPort.get("HostPort"); } })); portMappings.put(Integer.parseInt(containerPort), Integer.parseInt(hostPort)); } return portMappings; } finally { if (context != null) { context.close(); } } }
@Test(dependsOnMethods = "testListImages") public void testCreateContainer() throws IOException, InterruptedException { Config containerConfig = Config.builder() .imageId(image.getId()) .cmd( ImmutableList.of("/bin/sh", "-c", "while true; do echo hello world; sleep 1; done")) .build(); container = api().createContainer("testCreateContainer", containerConfig); assertNotNull(container); assertNotNull(container.getId()); }
@Override @Consumes(MediaType.APPLICATION_JSON) public Container expected() { return Container.builder() .id("6c9932f478bd761f32ddb54ed28ab42ab6fac6f2a279f561ea31503ee9d39524") .created( new SimpleDateFormatDateService().iso8601DateParse("2014-10-31T17:00:21.544197943Z")) .path("/home/weave/weaver") .name("/weave") .args(ImmutableList.of("-iface", "ethwe", "-wait", "5", "-name", "7a:63:a2:39:7b:0f")) .config( Config.builder() .hostname("6c9932f478bd") .env( ImmutableList.of( "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin")) .image("57e570db16baba1e8c0d6f3c15868ddb400f64ff76ec948e65c3ca3f15fb3587") .domainname("") .user("") .cmd(ImmutableList.of("-name", "7a:63:a2:39:7b:0f")) .entrypoint(ImmutableList.of("/home/weave/weaver", "-iface", "ethwe", "-wait", "5")) .image("zettio/weave") .workingDir("/home/weave") .exposedPorts( ImmutableMap.of("6783/tcp", ImmutableMap.of(), "6783/udp", ImmutableMap.of())) .build()) .state( State.create( 10357, true, 0, "2015-11-10T09:33:21.68146124Z", "0001-01-01T00:00:00Z", false, false, "running", false, false, "")) .image("57e570db16baba1e8c0d6f3c15868ddb400f64ff76ec948e65c3ca3f15fb3587") .networkSettings( NetworkSettings.builder() .sandboxId("3ef128b055eb9ef62a6a2c281d97a2dfde5f47947d490f1dd2a81612611d961f") .hairpinMode(false) .linkLocalIPv6Address("") .linkLocalIPv6PrefixLen(0) .globalIPv6Address("") .globalIPv6PrefixLen(0) .ipv6Gateway("") .sandboxKey("/var/run/docker/netns/3ef128b055eb") .endpointId("9e8dcc0c8288938a923018fee0728cee8e6de7c01a5150738ee6e51c1caf8cf6") .ipAddress("172.17.0.2") .ipPrefixLen(16) .gateway("172.17.0.1") .bridge("") .ports(ImmutableMap.<String, List<Map<String, String>>>of()) .macAddress("02:42:ac:11:00:02") .networks( ImmutableMap.of( "JCLOUDS_NETWORK", NetworkSettings.Details.create( "04268fbb4dc368b5a53bb1c3f89294a4f0c72095deb944db3c4efc6d6a439304", "172.19.0.1", "172.19.0.2", 16, "", "", 0, "02:42:ac:13:00:02"), "bridge", NetworkSettings.Details.create( "9e8dcc0c8288938a923018fee0728cee8e6de7c01a5150738ee6e51c1caf8cf6", "172.17.0.1", "172.17.0.2", 16, "", "", 0, "02:42:ac:11:00:02"))) .build()) .resolvConfPath( "/var/lib/docker/containers/6c9932f478bd761f32ddb54ed28ab42ab6fac6f2a279f561ea31503ee9d39524/resolv.conf") .hostConfig( HostConfig.builder() .containerIDFile("") .portBindings( ImmutableMap.<String, List<Map<String, String>>>of( "6783/tcp", ImmutableList.<Map<String, String>>of( ImmutableMap.of("HostIp", "", "HostPort", "6783")), "6783/udp", ImmutableList.<Map<String, String>>of( ImmutableMap.of("HostIp", "", "HostPort", "6783")))) .dns(ImmutableList.of("8.8.8.8", "8.8.4.4")) .extraHosts(ImmutableList.<String>of("extra:169.254.0.1")) .privileged(true) .networkMode("bridge") .build()) .driver("aufs") .execDriver("native-0.2") .hostnamePath( "/var/lib/docker/containers/6c9932f478bd761f32ddb54ed28ab42ab6fac6f2a279f561ea31503ee9d39524/hostname") .hostsPath( "/var/lib/docker/containers/6c9932f478bd761f32ddb54ed28ab42ab6fac6f2a279f561ea31503ee9d39524/hosts") .mountLabel("") .processLabel("") .build(); }
@Test(dependsOnMethods = "testStopContainer", expectedExceptions = NullPointerException.class) public void testRemoveContainer() { api().removeContainer(container.getId()); assertFalse(api().inspectContainer(container.getId()).getState().isRunning()); }
@Test(dependsOnMethods = "testStartContainer") public void testStopContainer() { api().stopContainer(container.getId()); assertFalse(api().inspectContainer(container.getId()).getState().isRunning()); }
@Test(dependsOnMethods = "testCreateContainer") public void testStartContainer() throws IOException, InterruptedException { api().startContainer(container.getId()); assertTrue(api().inspectContainer(container.getId()).getState().isRunning()); }