use of com.spotify.docker.client.messages.swarm.DnsConfig in project shinyproxy by openanalytics.
the class DockerSwarmBackend method doStartProxy.
@Override
protected void doStartProxy(DockerContainerProxy proxy) throws Exception {
Mount[] mounts = getBindVolumes(proxy.getApp()).stream().map(b -> b.split(":")).map(fromTo -> Mount.builder().source(fromTo[0]).target(fromTo[1]).type("bind").build()).toArray(i -> new Mount[i]);
ContainerSpec containerSpec = ContainerSpec.builder().image(proxy.getApp().getDockerImage()).command(proxy.getApp().getDockerCmd()).env(buildEnv(proxy.getUserId(), proxy.getApp())).dnsConfig(DnsConfig.builder().nameServers(proxy.getApp().getDockerDns()).build()).mounts(mounts).build();
NetworkAttachmentConfig[] networks = Arrays.stream(Optional.ofNullable(proxy.getApp().getDockerNetworkConnections()).orElse(new String[0])).map(n -> NetworkAttachmentConfig.builder().target(n).build()).toArray(i -> new NetworkAttachmentConfig[i]);
ServiceSpec.Builder serviceSpecBuilder = ServiceSpec.builder().networks(networks).name(proxy.getName()).taskTemplate(TaskSpec.builder().containerSpec(containerSpec).build());
if (proxy.getPort() > 0) {
serviceSpecBuilder.endpointSpec(EndpointSpec.builder().ports(PortConfig.builder().publishedPort(proxy.getPort()).targetPort(getAppPort(proxy)).build()).build());
}
proxy.setServiceId(dockerClient.createService(serviceSpecBuilder.build()).id());
boolean containerFound = Utils.retry(i -> {
try {
Task serviceTask = dockerClient.listTasks(Task.Criteria.builder().serviceName(proxy.getName()).build()).stream().findAny().orElseThrow(() -> new IllegalStateException("Swarm service has no tasks"));
proxy.setContainerId(serviceTask.status().containerStatus().containerId());
} catch (Exception e) {
throw new RuntimeException("Failed to inspect swarm service tasks", e);
}
return (proxy.getContainerId() != null);
}, 10, 2000, true);
if (!containerFound) {
dockerClient.removeService(proxy.getServiceId());
throw new IllegalStateException("Swarm container did not start in time");
}
}
Aggregations