CreateContainerCommandImpl implements ContainerEngine.CreateContainerCommand { @Override public String toString() { List<String> labelList = labels.entrySet().stream() .map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.toList()); List<String> ulimitList = ulimits.stream() .map(ulimit -> ulimit.getName() + "=" + ulimit.getSoft() + ":" + ulimit.getHard()) .collect(Collectors.toList()); List<String> addCapabilitiesList = addCapabilities.stream().map(Enum<Capability>::toString).sorted().collect(Collectors.toList()); List<String> dropCapabilitiesList = dropCapabilities.stream().map(Enum<Capability>::toString).sorted().collect(Collectors.toList()); Optional<String> entrypointExecuteable = entrypoint.map(args -> args[0]); String entrypointArgs = entrypoint.map(Stream::of).orElseGet(Stream::empty) .skip(1) .collect(Collectors.joining(" ")); return Stream.of( "--name " + containerName.asString(), toOptionalOption("--hostname", hostName), toOptionalOption("--cpu-shares", containerResources.map(ContainerResources::cpuShares)), toOptionalOption("--cpus", containerResources.map(ContainerResources::cpus)), toOptionalOption("--memory", containerResources.map(ContainerResources::memoryBytes)), toRepeatedOption("--label", labelList), toRepeatedOption("--ulimit", ulimitList), "--pids-limit -1", toRepeatedOption("--env", environmentAssignments), toRepeatedOption("--volume", volumeBindSpecs), toRepeatedOption("--cap-add", addCapabilitiesList), toRepeatedOption("--cap-drop", dropCapabilitiesList), toRepeatedOption("--security-opt", securityOpts), toRepeatedOption("--dns-option", dnsOptions), toOptionalOption("--net", networkMode), toOptionalOption("--ip", ipv4Address), toOptionalOption("--ip6", ipv6Address), toOptionalOption("--entrypoint", entrypointExecuteable), toFlagOption("--privileged", privileged), dockerImage.asString(), entrypointArgs) .filter(s -> !s.isEmpty()) .collect(Collectors.joining(" ")); } CreateContainerCommandImpl(DockerClient docker, DockerImage dockerImage, ContainerName containerName); @Override ContainerEngine.CreateContainerCommand withHostName(String hostName); @Override ContainerEngine.CreateContainerCommand withResources(ContainerResources containerResources); @Override ContainerEngine.CreateContainerCommand withLabel(String name, String value); ContainerEngine.CreateContainerCommand withManagedBy(String manager); @Override ContainerEngine.CreateContainerCommand withAddCapability(String capabilityName); @Override ContainerEngine.CreateContainerCommand withDropCapability(String capabilityName); @Override ContainerEngine.CreateContainerCommand withSecurityOpt(String securityOpt); @Override ContainerEngine.CreateContainerCommand withDnsOption(String dnsOption); @Override ContainerEngine.CreateContainerCommand withPrivileged(boolean privileged); @Override ContainerEngine.CreateContainerCommand withUlimit(String name, int softLimit, int hardLimit); @Override ContainerEngine.CreateContainerCommand withEntrypoint(String... entrypoint); @Override ContainerEngine.CreateContainerCommand withEnvironment(String name, String value); @Override ContainerEngine.CreateContainerCommand withVolume(Path path, Path volumePath); @Override ContainerEngine.CreateContainerCommand withSharedVolume(Path path, Path volumePath); @Override ContainerEngine.CreateContainerCommand withNetworkMode(String mode); @Override ContainerEngine.CreateContainerCommand withIpAddress(InetAddress address); @Override void create(); @Override String toString();  }
@Test public void testToString() throws UnknownHostException { DockerImage dockerImage = DockerImage.fromString("docker.registry.domain.tld/my/image:1.2.3"); ContainerResources containerResources = new ContainerResources(2.5, 100, 1024); String hostname = "docker-1.region.domain.tld"; ContainerName containerName = ContainerName.fromHostname(hostname); ContainerEngine.CreateContainerCommand createContainerCommand = new CreateContainerCommandImpl( null, dockerImage, containerName) .withHostName(hostname) .withResources(containerResources) .withLabel("my-label", "test-label") .withUlimit("nofile", 1, 2) .withUlimit("nproc", 10, 20) .withEnvironment("env1", "val1") .withEnvironment("env2", "val2") .withVolume(Paths.get("vol1"), Paths.get("/host/vol1")) .withAddCapability("SYS_PTRACE") .withAddCapability("SYS_ADMIN") .withDropCapability("NET_ADMIN") .withNetworkMode("bridge") .withIpAddress(InetAddress.getByName("10.0.0.1")) .withIpAddress(InetAddress.getByName("::1")) .withEntrypoint("/path/to/program", "arg1", "arg2") .withPrivileged(true); assertEquals("--name docker-1 " + "--hostname docker-1.region.domain.tld " + "--cpu-shares 100 " + "--cpus 2.5 " + "--memory 1024 " + "--label my-label=test-label " + "--ulimit nofile=1:2 " + "--ulimit nproc=10:20 " + "--pids-limit -1 " + "--env env1=val1 " + "--env env2=val2 " + "--volume vol1:/host/vol1:Z " + "--cap-add SYS_ADMIN " + "--cap-add SYS_PTRACE " + "--cap-drop NET_ADMIN " + "--net bridge " + "--ip 10.0.0.1 " + "--ip6 0:0:0:0:0:0:0:1 " + "--entrypoint /path/to/program " + "--privileged docker.registry.domain.tld/my/image:1.2.3 " + "arg1 " + "arg2", createContainerCommand.toString()); }