Connect container to list of networks in options

This commit is contained in:
Andrew Donald Kennedy 2016-07-02 14:20:00 +01:00 committed by Ignasi Barrera
parent 7a979ba87b
commit f2ce5679cc
2 changed files with 18 additions and 3 deletions

View File

@ -164,7 +164,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hashCode(super.hashCode(), volumes, hostname, dns, memory, entrypoint, commands, cpuShares, env, 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 @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.
* <p>
* Supported values are: {@code bridge}, {@code none}, {@code host},
* {@code networkname}, {@code networkid} or {@code container:[name|id]}
*
* @param networkMode * @param networkMode
* @return this instance * @return this instance
*/ */

View File

@ -32,6 +32,7 @@ import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
@ -182,7 +183,6 @@ public class DockerComputeServiceAdapter implements
hostConfigBuilder = HostConfig.builder().fromHostConfig(containerConfig.hostConfig()); hostConfigBuilder = HostConfig.builder().fromHostConfig(containerConfig.hostConfig());
hostConfigBuilder.portBindings(portBindings); hostConfigBuilder.portBindings(portBindings);
containerConfigBuilder.hostConfig(hostConfigBuilder.build()); containerConfigBuilder.hostConfig(hostConfigBuilder.build());
} else { } else {
containerConfigBuilder.image(imageId); containerConfigBuilder.image(imageId);
} }
@ -193,9 +193,20 @@ public class DockerComputeServiceAdapter implements
Container container = api.getContainerApi().createContainer(name, containerConfig); Container container = api.getContainerApi().createContainer(name, containerConfig);
logger.trace("<< container(%s)", container.id()); 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(); HostConfig hostConfig = containerConfig.hostConfig();
logger.debug(">> starting container(%s) with hostConfig(%s)", container.id(), hostConfig);
api.getContainerApi().startContainer(container.id(), hostConfig); api.getContainerApi().startContainer(container.id(), hostConfig);
logger.trace("<< started(%s)", container.id());
container = api.getContainerApi().inspectContainer(container.id()); container = api.getContainerApi().inspectContainer(container.id());
if (container.state().exitCode() != 0) { if (container.state().exitCode() != 0) {
destroyNode(container.id()); destroyNode(container.id());