Add openStdin option to Docker template options and set all port bindings explicitly

This commit is contained in:
Andrew Donald Kennedy 2016-04-25 22:25:40 +01:00 committed by Ignasi Barrera
parent 49f7bc37af
commit 42d0576aa9
2 changed files with 46 additions and 3 deletions

View File

@ -95,6 +95,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
protected Map<String, String> extraHosts = ImmutableMap.of(); protected Map<String, String> extraHosts = ImmutableMap.of();
protected List<String> volumesFrom = ImmutableList.of(); protected List<String> volumesFrom = ImmutableList.of();
protected boolean privileged; protected boolean privileged;
protected boolean openStdin;
protected Config.Builder configBuilder; protected Config.Builder configBuilder;
@Override @Override
@ -122,6 +123,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
eTo.extraHosts(extraHosts); eTo.extraHosts(extraHosts);
eTo.volumesFrom(volumesFrom); eTo.volumesFrom(volumesFrom);
eTo.privileged(privileged); eTo.privileged(privileged);
eTo.openStdin(openStdin);
eTo.configBuilder(configBuilder); eTo.configBuilder(configBuilder);
} }
} }
@ -147,6 +149,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
equal(this.extraHosts, that.extraHosts) && equal(this.extraHosts, that.extraHosts) &&
equal(this.volumesFrom, that.volumesFrom) && equal(this.volumesFrom, that.volumesFrom) &&
equal(this.privileged, that.privileged) && equal(this.privileged, that.privileged) &&
equal(this.openStdin, that.openStdin) &&
buildersEqual(this.configBuilder, that.configBuilder); buildersEqual(this.configBuilder, that.configBuilder);
} }
@ -161,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, configBuilder); portBindings, extraHosts, volumesFrom, privileged, openStdin, configBuilder);
} }
@Override @Override
@ -179,6 +182,8 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
.add("networkMode", networkMode) .add("networkMode", networkMode)
.add("extraHosts", extraHosts) .add("extraHosts", extraHosts)
.add("volumesFrom", volumesFrom) .add("volumesFrom", volumesFrom)
.add("privileged", privileged)
.add("openStdin", openStdin)
.add("configBuilder", configBuilder) .add("configBuilder", configBuilder)
.toString(); .toString();
} }
@ -304,6 +309,17 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
return this; return this;
} }
/**
* Keep {@code STDIN} open when running interactive workloads in the container.
*
* @param openStdin Whether the container should keep STDIN open
* @return this instance
*/
public DockerTemplateOptions openStdin(boolean openStdin) {
this.openStdin = openStdin;
return this;
}
/** /**
* This method sets Config.Builder configuration object, which can be used as * This method sets Config.Builder configuration object, which can be used as
* a replacement for all the other settings from this class. Some values in * a replacement for all the other settings from this class. Some values in
@ -347,6 +363,8 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
public boolean getPrivileged() { return privileged; } public boolean getPrivileged() { return privileged; }
public boolean getOpenStdin() { return openStdin; }
public Config.Builder getConfigBuilder() { return configBuilder; } public Config.Builder getConfigBuilder() { return configBuilder; }
public static class Builder { public static class Builder {
@ -479,6 +497,14 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
return options.privileged(privileged); return options.privileged(privileged);
} }
/**
* @see DockerTemplateOptions#openStdin(boolean)
*/
public static DockerTemplateOptions openStdin(boolean openStdin) {
DockerTemplateOptions options = new DockerTemplateOptions();
return options.openStdin(openStdin);
}
/** /**
* @see DockerTemplateOptions#configBuilder(Config.Builder) * @see DockerTemplateOptions#configBuilder(Config.Builder)
*/ */

View File

@ -98,6 +98,7 @@ public class DockerComputeServiceAdapter implements
containerConfigBuilder.memory(templateOptions.getMemory()); containerConfigBuilder.memory(templateOptions.getMemory());
containerConfigBuilder.hostname(templateOptions.getHostname()); containerConfigBuilder.hostname(templateOptions.getHostname());
containerConfigBuilder.cpuShares(templateOptions.getCpuShares()); containerConfigBuilder.cpuShares(templateOptions.getCpuShares());
containerConfigBuilder.openStdin(templateOptions.getOpenStdin());
containerConfigBuilder.env(templateOptions.getEnv()); containerConfigBuilder.env(templateOptions.getEnv());
if (!templateOptions.getVolumes().isEmpty()) { if (!templateOptions.getVolumes().isEmpty()) {
@ -110,13 +111,13 @@ public class DockerComputeServiceAdapter implements
HostConfig.Builder hostConfigBuilder = HostConfig.builder() HostConfig.Builder hostConfigBuilder = HostConfig.builder()
.publishAllPorts(true) .publishAllPorts(true)
.privileged( templateOptions.getPrivileged() ); .privileged(templateOptions.getPrivileged());
if (!templateOptions.getPortBindings().isEmpty()) { if (!templateOptions.getPortBindings().isEmpty()) {
Map<String, List<Map<String, String>>> portBindings = Maps.newHashMap(); Map<String, List<Map<String, String>>> portBindings = Maps.newHashMap();
for (Map.Entry<Integer, Integer> entry : templateOptions.getPortBindings().entrySet()) { for (Map.Entry<Integer, Integer> entry : templateOptions.getPortBindings().entrySet()) {
portBindings.put(entry.getValue() + "/tcp", portBindings.put(entry.getValue() + "/tcp",
Lists.<Map<String, String>>newArrayList(ImmutableMap.of("HostPort", Integer.toString(entry.getKey())))); Lists.<Map<String, String>>newArrayList(ImmutableMap.of("HostIp", "0.0.0.0", "HostPort", Integer.toString(entry.getKey()))));
} }
hostConfigBuilder.portBindings(portBindings); hostConfigBuilder.portBindings(portBindings);
} }
@ -167,6 +168,22 @@ public class DockerComputeServiceAdapter implements
// build once more after setting inboundPorts // build once more after setting inboundPorts
containerConfig = containerConfigBuilder.build(); containerConfig = containerConfigBuilder.build();
// finally update port bindings
Map<String, List<Map<String, String>>> portBindings = Maps.newHashMap();
Map<String, List<Map<String, String>>> existingBindings = containerConfig.hostConfig().portBindings();
if (existingBindings != null) {
portBindings.putAll(existingBindings);
}
for (String exposedPort : containerConfig.exposedPorts().keySet()) {
if (!portBindings.containsKey(exposedPort)) {
portBindings.put(exposedPort, Lists.<Map<String, String>>newArrayList(ImmutableMap.of("HostIp", "0.0.0.0")));
}
}
HostConfig.Builder hostConfigBuilder = HostConfig.builder().fromHostConfig(containerConfig.hostConfig());
hostConfigBuilder.portBindings(portBindings);
containerConfigBuilder.hostConfig(hostConfigBuilder.build());
containerConfig = containerConfigBuilder.build();
logger.debug(">> creating new container with containerConfig(%s)", containerConfig); logger.debug(">> creating new container with containerConfig(%s)", containerConfig);
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());