use of com.spotify.docker.client.messages.swarm.NetworkAttachmentConfig 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");
}
}
use of com.spotify.docker.client.messages.swarm.NetworkAttachmentConfig in project docker-client by spotify.
the class DefaultDockerClientTest method testCreateServiceWithNetwork.
@Test
public void testCreateServiceWithNetwork() throws Exception {
requireDockerApiVersionAtLeast("1.24", "swarm support");
final String networkName = randomName();
final String serviceName = randomName();
NetworkConfig.Builder networkConfigBuilder = NetworkConfig.builder().driver("overlay").name(networkName);
if (dockerApiVersionEquals("1.24")) {
// workaround for https://github.com/docker/docker/issues/25735
networkConfigBuilder = networkConfigBuilder.ipam(Ipam.create("default", Collections.<IpamConfig>emptyList()));
}
final NetworkCreation networkCreation = sut.createNetwork(networkConfigBuilder.build());
final String networkId = networkCreation.id();
assertThat(networkId, is(notNullValue()));
final TaskSpec taskSpec = TaskSpec.builder().containerSpec(ContainerSpec.builder().image("alpine").command(new String[] { "ping", "-c1000", "localhost" }).build()).build();
final ServiceSpec spec = ServiceSpec.builder().name(serviceName).taskTemplate(taskSpec).mode(ServiceMode.withReplicas(1L)).networks(NetworkAttachmentConfig.builder().target(networkName).build()).build();
final ServiceCreateResponse response = sut.createService(spec);
assertThat(response.id(), is(notNullValue()));
final Service inspectService = sut.inspectService(serviceName);
assertThat(inspectService.spec().networks().size(), is(1));
assertThat(Iterables.find(inspectService.spec().networks(), new Predicate<NetworkAttachmentConfig>() {
@Override
public boolean apply(NetworkAttachmentConfig config) {
return networkId.equals(config.target());
}
}, null), is(notNullValue()));
sut.removeService(serviceName);
sut.removeNetwork(networkName);
}
Aggregations