From f2ce5679cc2dd61faf5d43484e0b638f5c496db5 Mon Sep 17 00:00:00 2001 From: Andrew Donald Kennedy Date: Sat, 2 Jul 2016 14:20:00 +0100 Subject: [PATCH] Connect container to list of networks in options --- .../compute/options/DockerTemplateOptions.java | 8 ++++++-- .../strategy/DockerComputeServiceAdapter.java | 13 ++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/apis/docker/src/main/java/org/jclouds/docker/compute/options/DockerTemplateOptions.java b/apis/docker/src/main/java/org/jclouds/docker/compute/options/DockerTemplateOptions.java index 1ff4b44b51..109d469500 100644 --- a/apis/docker/src/main/java/org/jclouds/docker/compute/options/DockerTemplateOptions.java +++ b/apis/docker/src/main/java/org/jclouds/docker/compute/options/DockerTemplateOptions.java @@ -164,7 +164,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable @Override public int hashCode() { return Objects.hashCode(super.hashCode(), volumes, hostname, dns, memory, entrypoint, commands, cpuShares, env, - portBindings, extraHosts, volumesFrom, privileged, openStdin, configBuilder); + portBindings, networkMode, extraHosts, volumesFrom, privileged, openStdin, configBuilder); } @Override @@ -264,7 +264,11 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable } /** - * Sets the networking mode for the container. Supported values are: bridge, host, and container:[name|id] + * Sets the networking mode for the container. + *

+ * Supported values are: {@code bridge}, {@code none}, {@code host}, + * {@code networkname}, {@code networkid} or {@code container:[name|id]} + * * @param networkMode * @return this instance */ diff --git a/apis/docker/src/main/java/org/jclouds/docker/compute/strategy/DockerComputeServiceAdapter.java b/apis/docker/src/main/java/org/jclouds/docker/compute/strategy/DockerComputeServiceAdapter.java index 9ce63e5de7..8130838821 100644 --- a/apis/docker/src/main/java/org/jclouds/docker/compute/strategy/DockerComputeServiceAdapter.java +++ b/apis/docker/src/main/java/org/jclouds/docker/compute/strategy/DockerComputeServiceAdapter.java @@ -32,6 +32,7 @@ import com.google.common.base.Predicate; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; @@ -182,7 +183,6 @@ public class DockerComputeServiceAdapter implements hostConfigBuilder = HostConfig.builder().fromHostConfig(containerConfig.hostConfig()); hostConfigBuilder.portBindings(portBindings); containerConfigBuilder.hostConfig(hostConfigBuilder.build()); - } else { containerConfigBuilder.image(imageId); } @@ -193,9 +193,20 @@ public class DockerComputeServiceAdapter implements Container container = api.getContainerApi().createContainer(name, containerConfig); logger.trace("<< container(%s)", container.id()); + if (templateOptions.getNetworks() != null) { + logger.debug(">> connecting container(%s) to networks(%s)", container.id(), Iterables.toString(templateOptions.getNetworks())); + for (String networkIdOrName : templateOptions.getNetworks()) { + api.getNetworkApi().connectContainerToNetwork(networkIdOrName, container.id()); + } + logger.trace("<< connected(%s)", container.id()); + } + HostConfig hostConfig = containerConfig.hostConfig(); + logger.debug(">> starting container(%s) with hostConfig(%s)", container.id(), hostConfig); api.getContainerApi().startContainer(container.id(), hostConfig); + logger.trace("<< started(%s)", container.id()); + container = api.getContainerApi().inspectContainer(container.id()); if (container.state().exitCode() != 0) { destroyNode(container.id());