diff --git a/src/main/java/com/github/dockerjava/api/command/CreateContainerCmd.java b/src/main/java/com/github/dockerjava/api/command/CreateContainerCmd.java index 4fc7f5f32..63d53563f 100644 --- a/src/main/java/com/github/dockerjava/api/command/CreateContainerCmd.java +++ b/src/main/java/com/github/dockerjava/api/command/CreateContainerCmd.java @@ -3,20 +3,9 @@ import com.github.dockerjava.api.exception.ConflictException; import com.github.dockerjava.api.exception.NotFoundException; import com.github.dockerjava.api.model.AuthConfig; -import com.github.dockerjava.api.model.Bind; -import com.github.dockerjava.api.model.Capability; -import com.github.dockerjava.api.model.Device; import com.github.dockerjava.api.model.ExposedPort; import com.github.dockerjava.api.model.HostConfig; -import com.github.dockerjava.api.model.Link; -import com.github.dockerjava.api.model.LogConfig; -import com.github.dockerjava.api.model.LxcConf; -import com.github.dockerjava.api.model.PortBinding; -import com.github.dockerjava.api.model.Ports; -import com.github.dockerjava.api.model.RestartPolicy; -import com.github.dockerjava.api.model.Ulimit; import com.github.dockerjava.api.model.Volume; -import com.github.dockerjava.api.model.VolumesFrom; import javax.annotation.CheckForNull; import java.util.List; @@ -30,51 +19,9 @@ public interface CreateContainerCmd extends SyncDockerCmd getAliases(); - @CheckForNull - Bind[] getBinds(); - - /** - * @since 1.19 - */ - @CheckForNull - Integer getBlkioWeight(); - - @CheckForNull - Capability[] getCapAdd(); - - @CheckForNull - Capability[] getCapDrop(); - @CheckForNull String[] getCmd(); - /** - * @since 1.19 - */ - @CheckForNull - Integer getCpuPeriod(); - - @CheckForNull - String getCpusetCpus(); - - /** - * @since 1.19 - */ - @CheckForNull - String getCpusetMems(); - - @CheckForNull - Integer getCpuShares(); - - @CheckForNull - Device[] getDevices(); - - @CheckForNull - String[] getDns(); - - @CheckForNull - String[] getDnsSearch(); - @CheckForNull String getDomainName(); @@ -90,9 +37,6 @@ public interface CreateContainerCmd extends SyncDockerCmd getLabels(); - @CheckForNull - Link[] getLinks(); - - @CheckForNull - LogConfig getLogConfig(); - - @CheckForNull - LxcConf[] getLxcConf(); - @CheckForNull String getMacAddress(); - @CheckForNull - Long getMemory(); - - @CheckForNull - Long getMemorySwap(); - - @CheckForNull - Integer getMemorySwappiness(); - @CheckForNull String getName(); - @CheckForNull - String getNetworkMode(); - - @CheckForNull - Ports getPortBindings(); - @CheckForNull String[] getPortSpecs(); - @CheckForNull - RestartPolicy getRestartPolicy(); - - @CheckForNull - Ulimit[] getUlimits(); - @CheckForNull String getUser(); @CheckForNull Volume[] getVolumes(); - @CheckForNull - VolumesFrom[] getVolumesFrom(); - @CheckForNull String getWorkingDir(); @@ -171,36 +82,15 @@ public interface CreateContainerCmd extends SyncDockerCmd binds); - - /** - * @since 1.19 - */ - CreateContainerCmd withBlkioWeight(Integer blkioWeight); - - /** - * Add linux kernel capability to the container. For example: - * adding {@link Capability#MKNOD} allows the container to create special files using the 'mknod' command. - */ - CreateContainerCmd withCapAdd(Capability... capAdd); - - /** - * Add linux kernel capability to the container. For example: - * adding {@link Capability#MKNOD} allows the container to create special files using the 'mknod' command. - */ - CreateContainerCmd withCapAdd(List capAdd); - - /** - * Drop linux kernel capability from the container. For example: - * dropping {@link Capability#CHOWN} prevents the container from changing the owner of any files. - */ - CreateContainerCmd withCapDrop(Capability... capDrop); - - /** - * Drop linux kernel capability from the container. For example: - * dropping {@link Capability#CHOWN} prevents the container from changing the owner of any files. - */ - CreateContainerCmd withCapDrop(List capDrop); - CreateContainerCmd withCmd(String... cmd); CreateContainerCmd withCmd(List cmd); - CreateContainerCmd withContainerIDFile(String containerIDFile); - - /** - * @since 1.19 - */ - CreateContainerCmd withCpuPeriod(Integer cpuPeriod); - - CreateContainerCmd withCpusetCpus(String cpusetCpus); - - /** - * @since 1.19 - */ - CreateContainerCmd withCpusetMems(String cpusetMems); - - CreateContainerCmd withCpuShares(Integer cpuShares); - - /** - * Add host devices to the container - */ - CreateContainerCmd withDevices(Device... devices); - - /** - * Add host devices to the container - */ - CreateContainerCmd withDevices(List devices); - - /** - * Set custom DNS servers - */ - CreateContainerCmd withDns(String... dns); - - /** - * Set custom DNS servers - */ - CreateContainerCmd withDns(List dns); - - /** - * Set custom DNS search domains - */ - CreateContainerCmd withDnsSearch(String... dnsSearch); - - /** - * Set custom DNS search domains - */ - CreateContainerCmd withDnsSearch(List dnsSearch); - CreateContainerCmd withDomainName(String domainName); CreateContainerCmd withEntrypoint(String... entrypoint); @@ -329,16 +140,6 @@ public interface CreateContainerCmd extends SyncDockerCmd exposedPorts); - /** - * Add hostnames to /etc/hosts in the container - */ - CreateContainerCmd withExtraHosts(String... extraHosts); - - /** - * Add hostnames to /etc/hosts in the container - */ - CreateContainerCmd withExtraHosts(List extraHosts); - CreateContainerCmd withHostName(String hostName); CreateContainerCmd withImage(String image); @@ -349,114 +150,30 @@ public interface CreateContainerCmd extends SyncDockerCmd labels); - /** - * Add link to another container. - */ - CreateContainerCmd withLinks(Link... links); - - /** - * Add link to another container. - */ - CreateContainerCmd withLinks(List links); - - CreateContainerCmd withLogConfig(LogConfig logConfig); - - CreateContainerCmd withLxcConf(LxcConf... lxcConf); - - CreateContainerCmd withLxcConf(List lxcConf); - CreateContainerCmd withMacAddress(String macAddress); - CreateContainerCmd withMemory(Long memory); - - CreateContainerCmd withMemorySwap(Long memorySwap); - - CreateContainerCmd withMemorySwappiness(Integer memorySwappiness); - CreateContainerCmd withName(String name); CreateContainerCmd withNetworkDisabled(Boolean disableNetwork); - /** - * Set the Network mode for the container - *
    - *
  • 'bridge': creates a new network stack for the container on the docker bridge
  • - *
  • 'none': no networking for this container
  • - *
  • 'container:': reuses another container network stack
  • - *
  • 'host': use the host network stack inside the container. Note: the host mode gives the container full access to local system - * services such as D-bus and is therefore considered insecure.
  • - *
- */ - CreateContainerCmd withNetworkMode(String networkMode); - - /** - * @since 1.19 - */ - CreateContainerCmd withOomKillDisable(Boolean oomKillDisable); - - /** - * Add one or more {@link PortBinding}s. This corresponds to the --publish (-p) option of the - * docker run CLI command. - */ - CreateContainerCmd withPortBindings(PortBinding... portBindings); - - /** - * Add one or more {@link PortBinding}s. This corresponds to the --publish (-p) option of the - * docker run CLI command. - */ - CreateContainerCmd withPortBindings(List portBindings); - - /** - * Add the port bindings that are contained in the given {@link Ports} object. - * - * @see #withPortBindings(PortBinding...) - */ - CreateContainerCmd withPortBindings(Ports portBindings); - CreateContainerCmd withPortSpecs(String... portSpecs); CreateContainerCmd withPortSpecs(List portSpecs); - CreateContainerCmd withPrivileged(Boolean privileged); - - CreateContainerCmd withPublishAllPorts(Boolean publishAllPorts); - - CreateContainerCmd withReadonlyRootfs(Boolean readonlyRootfs); - - /** - * Set custom {@link RestartPolicy} for the container. Defaults to {@link RestartPolicy#noRestart()} - */ - CreateContainerCmd withRestartPolicy(RestartPolicy restartPolicy); - CreateContainerCmd withStdInOnce(Boolean stdInOnce); CreateContainerCmd withStdinOpen(Boolean stdinOpen); CreateContainerCmd withTty(Boolean tty); - CreateContainerCmd withUlimits(Ulimit... ulimits); - - CreateContainerCmd withUlimits(List ulimits); - CreateContainerCmd withUser(String user); CreateContainerCmd withVolumes(Volume... volumes); CreateContainerCmd withVolumes(List volumes); - CreateContainerCmd withVolumesFrom(VolumesFrom... volumesFrom); - - CreateContainerCmd withVolumesFrom(List volumesFrom); - CreateContainerCmd withWorkingDir(String workingDir); - CreateContainerCmd withCgroupParent(String cgroupParent); - - /** - * Set the PID (Process) Namespace mode for the container, 'host': use the host's PID namespace inside the container - */ - CreateContainerCmd withPidMode(String pidMode); - CreateContainerCmd withHostConfig(HostConfig hostConfig); /** diff --git a/src/main/java/com/github/dockerjava/api/model/HostConfig.java b/src/main/java/com/github/dockerjava/api/model/HostConfig.java index b857725e3..fbb30f187 100644 --- a/src/main/java/com/github/dockerjava/api/model/HostConfig.java +++ b/src/main/java/com/github/dockerjava/api/model/HostConfig.java @@ -16,6 +16,8 @@ import java.util.List; import java.util.Map; +import static com.google.common.base.Preconditions.checkNotNull; + /** * Used in `/containers/create`, and in inspect container. * TODO exclude usage for 2 different models. @@ -30,6 +32,9 @@ public class HostConfig implements Serializable { @JsonProperty("Binds") private Binds binds; + /** + * @since 1.19 + */ @JsonProperty("BlkioWeight") private Integer blkioWeight; @@ -78,6 +83,9 @@ public class HostConfig implements Serializable { @JsonProperty("ContainerIDFile") private String containerIDFile; + /** + * @since 1.19 + */ @JsonProperty("CpuPeriod") private Integer cpuPeriod; @@ -93,6 +101,9 @@ public class HostConfig implements Serializable { @JsonProperty("CpusetCpus") private String cpusetCpus; + /** + * @since 1.19 + */ @JsonProperty("CpusetMems") private String cpusetMems; @@ -135,7 +146,7 @@ public class HostConfig implements Serializable { @JsonProperty("MemoryReservation") private Long memoryReservation; - /** + /** * @since {@link RemoteApiVersion#VERSION_1_21} */ @JsonProperty("KernelMemory") @@ -231,6 +242,10 @@ public class HostConfig implements Serializable { @JsonProperty("Isolation") private String isolation; + public static HostConfig newHostConfig() { + return new HostConfig(); + } + @JsonIgnore public Bind[] getBinds() { return (binds == null) ? new Bind[0] : binds.getBinds(); @@ -430,12 +445,13 @@ public Integer getMemorySwappiness() { public Boolean getOomKillDisable() { return oomKillDisable; } + /** * @see #autoRemove */ @CheckForNull public Boolean getAutoRemove() { - return autoRemove; + return autoRemove; } /** @@ -510,6 +526,7 @@ public void setLinks(Link... links) { } // auto-generated builder setters + /** * @see #binds */ @@ -518,6 +535,12 @@ public HostConfig withBinds(Binds binds) { return this; } + public HostConfig withBinds(Bind... binds) { + checkNotNull(binds, "binds was not specified"); + setBinds(binds); + return this; + } + /** * @see #blkioDeviceReadBps */ @@ -567,9 +590,10 @@ public HostConfig withBlkioWeightDevice(List blkioWeightDevice) { } /** - * @see #capAdd + * Add linux kernel capability to the container. For example: + * adding {@link Capability#MKNOD} allows the container to create special files using the 'mknod' command. */ - public HostConfig withCapAdd(Capability[] capAdd) { + public HostConfig withCapAdd(Capability... capAdd) { this.capAdd = capAdd; return this; } @@ -577,7 +601,7 @@ public HostConfig withCapAdd(Capability[] capAdd) { /** * @see #capDrop */ - public HostConfig withCapDrop(Capability[] capDrop) { + public HostConfig withCapDrop(Capability... capDrop) { this.capDrop = capDrop; return this; } @@ -638,10 +662,15 @@ public HostConfig withCpuShares(Integer cpuShares) { return this; } + public HostConfig withDevices(List devices) { + checkNotNull(devices, "devices was not specified"); + return withDevices(devices.toArray(new Device[devices.size()])); + } + /** * @see #devices */ - public HostConfig withDevices(Device[] devices) { + public HostConfig withDevices(Device... devices) { this.devices = devices; return this; } @@ -657,27 +686,42 @@ public HostConfig withDiskQuota(Long diskQuota) { /** * @see #dns */ - public HostConfig withDns(String[] dns) { + public HostConfig withDns(String... dns) { this.dns = dns; return this; } + public HostConfig withDns(List dns) { + checkNotNull(dns, "dns was not specified"); + return withDns(dns.toArray(new String[dns.size()])); + } + /** * @see #dnsSearch */ - public HostConfig withDnsSearch(String[] dnsSearch) { + public HostConfig withDnsSearch(String... dnsSearch) { this.dnsSearch = dnsSearch; return this; } + public HostConfig withDnsSearch(List dnsSearch) { + checkNotNull(dnsSearch, "dnsSearch was not specified"); + return withDnsSearch(dnsSearch.toArray(new String[0])); + } + /** * @see #extraHosts */ - public HostConfig withExtraHosts(String[] extraHosts) { + public HostConfig withExtraHosts(String... extraHosts) { this.extraHosts = extraHosts; return this; } + public HostConfig withExtraHosts(List extraHosts) { + checkNotNull(extraHosts, "extraHosts was not specified"); + return withExtraHosts(extraHosts.toArray(new String[extraHosts.size()])); + } + /** * @see #kernelMemory */ @@ -694,6 +738,18 @@ public HostConfig withLinks(Links links) { return this; } + public HostConfig withLinks(List links) { + checkNotNull(links, "links was not specified"); + setLinks(links.toArray(new Link[links.size()])); + return this; + } + + public HostConfig withLinks(Link... links) { + checkNotNull(links, "links was not specified"); + setLinks(links); + return this; + } + /** * @see #logConfig */ @@ -710,6 +766,11 @@ public HostConfig withLxcConf(LxcConf[] lxcConf) { return this; } + public HostConfig withLxcConf(List lxcConf) { + checkNotNull(lxcConf, "lxcConf was not specified"); + return withLxcConf(lxcConf.toArray(new LxcConf[0])); + } + /** * @see #memory */ @@ -743,6 +804,15 @@ public HostConfig withMemorySwappiness(Integer memorySwappiness) { } /** + * Set the Network mode for the container + *
    + *
  • 'bridge': creates a new network stack for the container on the docker bridge
  • + *
  • 'none': no networking for this container
  • + *
  • 'container:': reuses another container network stack
  • + *
  • 'host': use the host network stack inside the container. Note: the host mode gives the container full access to local system + * services such as D-bus and is therefore considered insecure.
  • + *
+ * * @see #networkMode */ public HostConfig withNetworkMode(String networkMode) { @@ -757,12 +827,13 @@ public HostConfig withOomKillDisable(Boolean oomKillDisable) { this.oomKillDisable = oomKillDisable; return this; } + /** * @see #autoRemove */ public HostConfig withAutoRemove(Boolean autoRemove) { - this.autoRemove = autoRemove; - return this; + this.autoRemove = autoRemove; + return this; } /** @@ -781,6 +852,19 @@ public HostConfig withPidMode(String pidMode) { return this; } + /** + * @see #portBindings + */ + public HostConfig withPortBindings(PortBinding... portBindings) { + checkNotNull(portBindings, "portBindings was not specified"); + return withPortBindings(new Ports(portBindings)); + } + + public HostConfig withPortBindings(List portBindings) { + checkNotNull(portBindings, "portBindings was not specified"); + return withPortBindings(portBindings.toArray(new PortBinding[0])); + } + /** * @see #portBindings */ @@ -869,6 +953,11 @@ public HostConfig withUlimits(Ulimit[] ulimits) { return this; } + public HostConfig withUlimits(List ulimits) { + checkNotNull(ulimits, "no ulimits was specified"); + return withUlimits(ulimits.toArray(new Ulimit[ulimits.size()])); + } + /** * @see #volumeDriver */ @@ -880,11 +969,17 @@ public HostConfig withVolumeDriver(String volumeDriver) { /** * @see #volumesFrom */ - public HostConfig withVolumesFrom(VolumesFrom[] volumesFrom) { + public HostConfig withVolumesFrom(VolumesFrom... volumesFrom) { this.volumesFrom = volumesFrom; return this; } + public HostConfig withVolumesFrom(List volumesFrom) { + checkNotNull(volumesFrom, "volumesFrom was not specified"); + return withVolumesFrom(volumesFrom.toArray(new VolumesFrom[volumesFrom.size()])); + } + + /** * @see #pidsLimit */ @@ -914,8 +1009,6 @@ public HostConfig withIsolation(String isolation) { return this; } - // end of auto-generated - @Override public String toString() { return ToStringBuilder.reflectionToString(this); diff --git a/src/main/java/com/github/dockerjava/core/command/CreateContainerCmdImpl.java b/src/main/java/com/github/dockerjava/core/command/CreateContainerCmdImpl.java index a1f4090ba..bb1dc1573 100644 --- a/src/main/java/com/github/dockerjava/core/command/CreateContainerCmdImpl.java +++ b/src/main/java/com/github/dockerjava/core/command/CreateContainerCmdImpl.java @@ -9,23 +9,12 @@ import com.github.dockerjava.api.exception.ConflictException; import com.github.dockerjava.api.exception.NotFoundException; import com.github.dockerjava.api.model.AuthConfig; -import com.github.dockerjava.api.model.Bind; -import com.github.dockerjava.api.model.Capability; import com.github.dockerjava.api.model.ContainerNetwork; -import com.github.dockerjava.api.model.Device; import com.github.dockerjava.api.model.ExposedPort; import com.github.dockerjava.api.model.ExposedPorts; import com.github.dockerjava.api.model.HostConfig; -import com.github.dockerjava.api.model.Link; -import com.github.dockerjava.api.model.LogConfig; -import com.github.dockerjava.api.model.LxcConf; -import com.github.dockerjava.api.model.PortBinding; -import com.github.dockerjava.api.model.Ports; -import com.github.dockerjava.api.model.RestartPolicy; -import com.github.dockerjava.api.model.Ulimit; import com.github.dockerjava.api.model.Volume; import com.github.dockerjava.api.model.Volumes; -import com.github.dockerjava.api.model.VolumesFrom; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ToStringBuilder; @@ -195,77 +184,11 @@ public List getAliases() { return aliases; } - @Override - @JsonIgnore - public Bind[] getBinds() { - return hostConfig.getBinds(); - } - - @Override - @JsonIgnore - public Integer getBlkioWeight() { - return hostConfig.getBlkioWeight(); - } - - @Override - @JsonIgnore - public Capability[] getCapAdd() { - return hostConfig.getCapAdd(); - } - - @Override - @JsonIgnore - public Capability[] getCapDrop() { - return hostConfig.getCapDrop(); - } - @Override public String[] getCmd() { return cmd; } - @Override - @JsonIgnore - public Integer getCpuPeriod() { - return hostConfig.getCpuPeriod(); - } - - @Override - @JsonIgnore - public String getCpusetCpus() { - return hostConfig.getCpusetCpus(); - } - - @Override - @JsonIgnore - public String getCpusetMems() { - return hostConfig.getCpusetMems(); - } - - @Override - @JsonIgnore - public Integer getCpuShares() { - return hostConfig.getCpuShares(); - } - - @Override - @JsonIgnore - public Device[] getDevices() { - return hostConfig.getDevices(); - } - - @Override - @JsonIgnore - public String[] getDns() { - return hostConfig.getDns(); - } - - @Override - @JsonIgnore - public String[] getDnsSearch() { - return hostConfig.getDnsSearch(); - } - @Override public String getDomainName() { return domainName; @@ -296,12 +219,6 @@ public String getStopSignal() { return stopSignal; } - @Override - @JsonIgnore - public String[] getExtraHosts() { - return hostConfig.getExtraHosts(); - } - @Override public String getHostName() { return hostName; @@ -328,81 +245,21 @@ public Map getLabels() { return labels; } - @Override - @JsonIgnore - public Link[] getLinks() { - return hostConfig.getLinks(); - } - - @Override - @JsonIgnore - public LxcConf[] getLxcConf() { - return hostConfig.getLxcConf(); - } - - @Override - @JsonIgnore - public LogConfig getLogConfig() { - return hostConfig.getLogConfig(); - } - @Override public String getMacAddress() { return macAddress; } - @Override - @JsonIgnore - public Long getMemory() { - return hostConfig.getMemory(); - } - - @Override - @JsonIgnore - public Long getMemorySwap() { - return hostConfig.getMemorySwap(); - } - - @Override - @JsonIgnore - public Integer getMemorySwappiness() { - return hostConfig.getMemorySwappiness(); - } - @Override public String getName() { return name; } - @Override - @JsonIgnore - public String getNetworkMode() { - return hostConfig.getNetworkMode(); - } - - @Override - @JsonIgnore - public Ports getPortBindings() { - return hostConfig.getPortBindings(); - } - @Override public String[] getPortSpecs() { return portSpecs; } - @Override - @JsonIgnore - public RestartPolicy getRestartPolicy() { - return hostConfig.getRestartPolicy(); - } - - @Override - @JsonIgnore - public Ulimit[] getUlimits() { - return hostConfig.getUlimits(); - } - @Override public String getUser() { return user; @@ -414,12 +271,6 @@ public Volume[] getVolumes() { return volumes.getVolumes(); } - @Override - @JsonIgnore - public VolumesFrom[] getVolumesFrom() { - return hostConfig.getVolumesFrom(); - } - @Override public String getWorkingDir() { return workingDir; @@ -445,30 +296,6 @@ public Boolean isNetworkDisabled() { return networkDisabled; } - @Override - @JsonIgnore - public Boolean getOomKillDisable() { - return hostConfig.getOomKillDisable(); - } - - @Override - @JsonIgnore - public Boolean getPrivileged() { - return hostConfig.getPrivileged(); - } - - @Override - @JsonIgnore - public Boolean getPublishAllPorts() { - return hostConfig.getPublishAllPorts(); - } - - @Override - @JsonIgnore - public Boolean getReadonlyRootfs() { - return hostConfig.getReadonlyRootfs(); - } - @Override public Boolean isStdInOnce() { return stdInOnce; @@ -484,22 +311,11 @@ public Boolean isTty() { return tty; } - @Override - @JsonIgnore - public String getPidMode() { - return hostConfig.getPidMode(); - } - @Override public HostConfig getHostConfig() { return hostConfig; } - @Override - public String getCgroupParent() { - return hostConfig.getCgroupParent(); - } - @Override public CreateContainerCmd withAliases(String... aliases) { this.aliases = Arrays.asList(aliases); @@ -534,51 +350,6 @@ public CreateContainerCmd withAttachStdout(Boolean attachStdout) { return this; } - @Override - public CreateContainerCmd withBinds(Bind... binds) { - checkNotNull(binds, "binds was not specified"); - hostConfig.setBinds(binds); - return this; - } - - @Override - public CreateContainerCmd withBinds(List binds) { - checkNotNull(binds, "binds was not specified"); - return withBinds(binds.toArray(new Bind[binds.size()])); - } - - @Override - public CreateContainerCmd withBlkioWeight(Integer blkioWeight) { - checkNotNull(blkioWeight, "blkioWeight was not specified"); - hostConfig.withBlkioWeight(blkioWeight); - return this; - } - - @Override - public CreateContainerCmd withCapAdd(Capability... capAdd) { - checkNotNull(capAdd, "capAdd was not specified"); - hostConfig.withCapAdd(capAdd); - return this; - } - - @Override - public CreateContainerCmd withCapAdd(List capAdd) { - checkNotNull(capAdd, "capAdd was not specified"); - return withCapAdd(capAdd.toArray(new Capability[capAdd.size()])); - } - - @Override - public CreateContainerCmd withCapDrop(Capability... capDrop) { - checkNotNull(capDrop, "capDrop was not specified"); - hostConfig.withCapDrop(capDrop); - return this; - } - - @Override - public CreateContainerCmd withCapDrop(List capDrop) { - checkNotNull(capDrop, "capDrop was not specified"); - return withCapDrop(capDrop.toArray(new Capability[capDrop.size()])); - } @Override public CreateContainerCmd withCmd(String... cmd) { @@ -593,80 +364,6 @@ public CreateContainerCmd withCmd(List cmd) { return withCmd(cmd.toArray(new String[cmd.size()])); } - @Override - public CreateContainerCmd withContainerIDFile(String containerIDFile) { - checkNotNull(containerIDFile, "no containerIDFile was specified"); - hostConfig.withContainerIDFile(containerIDFile); - return this; - } - - @Override - public CreateContainerCmd withCpuPeriod(Integer cpuPeriod) { - checkNotNull(cpuPeriod, "cpuPeriod was not specified"); - hostConfig.withCpuPeriod(cpuPeriod); - return this; - } - - @Override - public CreateContainerCmd withCpusetCpus(String cpusetCpus) { - checkNotNull(cpusetCpus, "cpusetCpus was not specified"); - hostConfig.withCpusetCpus(cpusetCpus); - return this; - } - - @Override - public CreateContainerCmd withCpusetMems(String cpusetMems) { - checkNotNull(cpusetMems, "cpusetMems was not specified"); - hostConfig.withCpusetMems(cpusetMems); - return this; - } - - @Override - public CreateContainerCmd withCpuShares(Integer cpuShares) { - checkNotNull(cpuShares, "cpuShares was not specified"); - hostConfig.withCpuShares(cpuShares); - return this; - } - - @Override - public CreateContainerCmd withDevices(Device... devices) { - checkNotNull(devices, "devices was not specified"); - this.hostConfig.withDevices(devices); - return this; - } - - @Override - public CreateContainerCmd withDevices(List devices) { - checkNotNull(devices, "devices was not specified"); - return withDevices(devices.toArray(new Device[devices.size()])); - } - - @Override - public CreateContainerCmd withDns(String... dns) { - checkNotNull(dns, "dns was not specified"); - this.hostConfig.withDns(dns); - return this; - } - - @Override - public CreateContainerCmd withDns(List dns) { - checkNotNull(dns, "dns was not specified"); - return withDns(dns.toArray(new String[dns.size()])); - } - - @Override - public CreateContainerCmd withDnsSearch(String... dnsSearch) { - checkNotNull(dnsSearch, "dnsSearch was not specified"); - this.hostConfig.withDnsSearch(dnsSearch); - return this; - } - - @Override - public CreateContainerCmd withDnsSearch(List dnsSearch) { - checkNotNull(dnsSearch, "dnsSearch was not specified"); - return withDnsSearch(dnsSearch.toArray(new String[0])); - } - @Override public CreateContainerCmd withDomainName(String domainName) { checkNotNull(domainName, "no domainName was specified"); @@ -720,19 +417,6 @@ public CreateContainerCmd withExposedPorts(List exposedPorts) { return withExposedPorts(exposedPorts.toArray(new ExposedPort[exposedPorts.size()])); } - @Override - public CreateContainerCmd withExtraHosts(String... extraHosts) { - checkNotNull(extraHosts, "extraHosts was not specified"); - this.hostConfig.withExtraHosts(extraHosts); - return this; - } - - @Override - public CreateContainerCmd withExtraHosts(List extraHosts) { - checkNotNull(extraHosts, "extraHosts was not specified"); - return withExtraHosts(extraHosts.toArray(new String[extraHosts.size()])); - } - @Override public CreateContainerCmd withHostName(String hostName) { checkNotNull(hostConfig, "no hostName was specified"); @@ -768,39 +452,6 @@ public CreateContainerCmd withLabels(Map labels) { return this; } - @Override - public CreateContainerCmd withLinks(Link... links) { - checkNotNull(links, "links was not specified"); - this.hostConfig.setLinks(links); - return this; - } - - @Override - public CreateContainerCmd withLinks(List links) { - checkNotNull(links, "links was not specified"); - return withLinks(links.toArray(new Link[links.size()])); - } - - @Override - public CreateContainerCmd withLxcConf(LxcConf... lxcConf) { - checkNotNull(lxcConf, "lxcConf was not specified"); - this.hostConfig.withLxcConf(lxcConf); - return this; - } - - @Override - public CreateContainerCmd withLxcConf(List lxcConf) { - checkNotNull(lxcConf, "lxcConf was not specified"); - return withLxcConf(lxcConf.toArray(new LxcConf[0])); - } - - @Override - public CreateContainerCmd withLogConfig(LogConfig logConfig) { - checkNotNull(logConfig, "logConfig was not specified"); - this.hostConfig.withLogConfig(logConfig); - return this; - } - @Override public CreateContainerCmd withMacAddress(String macAddress) { checkNotNull(macAddress, "macAddress was not specified"); @@ -808,27 +459,6 @@ public CreateContainerCmd withMacAddress(String macAddress) { return this; } - @Override - public CreateContainerCmd withMemory(Long memory) { - checkNotNull(memory, "memory was not specified"); - hostConfig.withMemory(memory); - return this; - } - - @Override - public CreateContainerCmd withMemorySwap(Long memorySwap) { - checkNotNull(memorySwap, "memorySwap was not specified"); - hostConfig.withMemorySwap(memorySwap); - return this; - } - - @Override - public CreateContainerCmd withMemorySwappiness(Integer memorySwappiness) { - checkNotNull(memorySwappiness, "memorySwappiness was not specified"); - hostConfig.withMemorySwappiness(memorySwappiness); - return this; - } - @Override public CreateContainerCmd withName(String name) { checkNotNull(name, "name was not specified"); @@ -843,40 +473,6 @@ public CreateContainerCmd withNetworkDisabled(Boolean disableNetwork) { return this; } - @Override - public CreateContainerCmd withNetworkMode(String networkMode) { - checkNotNull(networkMode, "networkMode was not specified"); - this.hostConfig.withNetworkMode(networkMode); - return this; - } - - @Override - public CreateContainerCmd withOomKillDisable(Boolean oomKillDisable) { - checkNotNull(oomKillDisable, "oomKillDisable was not specified"); - hostConfig.withOomKillDisable(oomKillDisable); - return this; - } - - @Override - public CreateContainerCmd withPortBindings(PortBinding... portBindings) { - checkNotNull(portBindings, "portBindings was not specified"); - this.hostConfig.withPortBindings(new Ports(portBindings)); - return this; - } - - @Override - public CreateContainerCmd withPortBindings(List portBindings) { - checkNotNull(portBindings, "portBindings was not specified"); - return withPortBindings(portBindings.toArray(new PortBinding[0])); - } - - @Override - public CreateContainerCmd withPortBindings(Ports portBindings) { - checkNotNull(portBindings, "portBindings was not specified"); - this.hostConfig.withPortBindings(portBindings); - return this; - } - @Override public CreateContainerCmd withPortSpecs(String... portSpecs) { checkNotNull(portSpecs, "portSpecs was not specified"); @@ -890,34 +486,6 @@ public CreateContainerCmd withPortSpecs(List portSpecs) { return withPortSpecs(portSpecs.toArray(new String[portSpecs.size()])); } - @Override - public CreateContainerCmd withPrivileged(Boolean privileged) { - checkNotNull(privileged, "no privileged was specified"); - this.hostConfig.withPrivileged(privileged); - return this; - } - - @Override - public CreateContainerCmd withPublishAllPorts(Boolean publishAllPorts) { - checkNotNull(publishAllPorts, "no publishAllPorts was specified"); - this.hostConfig.withPublishAllPorts(publishAllPorts); - return this; - } - - @Override - public CreateContainerCmd withReadonlyRootfs(Boolean readonlyRootfs) { - checkNotNull(readonlyRootfs, "no readonlyRootfs was specified"); - hostConfig.withReadonlyRootfs(readonlyRootfs); - return this; - } - - @Override - public CreateContainerCmd withRestartPolicy(RestartPolicy restartPolicy) { - checkNotNull(restartPolicy, "restartPolicy was not specified"); - this.hostConfig.withRestartPolicy(restartPolicy); - return this; - } - @Override public CreateContainerCmd withStdInOnce(Boolean stdInOnce) { checkNotNull(stdInOnce, "no stdInOnce was specified"); @@ -939,18 +507,6 @@ public CreateContainerCmd withTty(Boolean tty) { return this; } - @Override - public CreateContainerCmd withUlimits(Ulimit... ulimits) { - checkNotNull(ulimits, "no ulimits was specified"); - hostConfig.withUlimits(ulimits); - return this; - } - - @Override - public CreateContainerCmd withUlimits(List ulimits) { - checkNotNull(ulimits, "no ulimits was specified"); - return withUlimits(ulimits.toArray(new Ulimit[ulimits.size()])); - } @Override public CreateContainerCmd withUser(String user) { @@ -972,19 +528,6 @@ public CreateContainerCmd withVolumes(List volumes) { return withVolumes(volumes.toArray(new Volume[volumes.size()])); } - @Override - public CreateContainerCmd withVolumesFrom(VolumesFrom... volumesFrom) { - checkNotNull(volumesFrom, "volumesFrom was not specified"); - this.hostConfig.withVolumesFrom(volumesFrom); - return this; - } - - @Override - public CreateContainerCmd withVolumesFrom(List volumesFrom) { - checkNotNull(volumesFrom, "volumesFrom was not specified"); - return withVolumesFrom(volumesFrom.toArray(new VolumesFrom[volumesFrom.size()])); - } - @Override public CreateContainerCmd withWorkingDir(String workingDir) { checkNotNull(workingDir, "workingDir was not specified"); @@ -992,19 +535,6 @@ public CreateContainerCmd withWorkingDir(String workingDir) { return this; } - @Override - public CreateContainerCmd withCgroupParent(final String cgroupParent) { - checkNotNull(cgroupParent, "cgroupParent was not specified"); - this.hostConfig.withCgroupParent(cgroupParent); - return this; - } - - @Override - public CreateContainerCmd withPidMode(String pidMode) { - checkNotNull(pidMode, "pidMode was not specified"); - this.hostConfig.withPidMode(pidMode); - return this; - } @Override public CreateContainerCmd withHostConfig(HostConfig hostConfig) { diff --git a/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java b/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java index c49cbd8d4..f01e60019 100644 --- a/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java +++ b/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java @@ -48,6 +48,7 @@ import static com.github.dockerjava.api.model.Capability.MKNOD; import static com.github.dockerjava.api.model.Capability.NET_ADMIN; +import static com.github.dockerjava.api.model.HostConfig.newHostConfig; import static com.github.dockerjava.cmd.CmdIT.FactoryType.JERSEY; import static com.github.dockerjava.core.RemoteApiVersion.VERSION_1_23; import static com.github.dockerjava.core.RemoteApiVersion.VERSION_1_24; @@ -160,7 +161,8 @@ public void createContainerWithVolumesFrom() throws DockerException { CreateContainerResponse container1 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withCmd("sleep", "9999") .withName(container1Name) - .withBinds(bind1, bind2) + .withHostConfig(newHostConfig() + .withBinds(bind1, bind2)) .exec(); LOG.info("Created container1 {}", container1.toString()); @@ -175,7 +177,8 @@ public void createContainerWithVolumesFrom() throws DockerException { // create a second container with volumes from first container CreateContainerResponse container2 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withCmd("sleep", "9999") - .withVolumesFrom(new VolumesFrom(container1Name)) + .withHostConfig(newHostConfig() + .withVolumesFrom(new VolumesFrom(container1Name))) .exec(); LOG.info("Created container2 {}", container2.toString()); @@ -285,7 +288,10 @@ public void createContainerWithLink() throws DockerException { assertThat(inspectContainerResponse1.getState().getRunning(), is(true)); CreateContainerResponse container2 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withName(containerName2) - .withCmd("env").withLinks(new Link(containerName1, "container1Link")).exec(); + .withCmd("env") + .withHostConfig(newHostConfig() + .withLinks(new Link(containerName1, "container1Link"))) + .exec(); LOG.info("Created container {}", container2.toString()); assertThat(container2.getId(), not(isEmptyString())); @@ -299,7 +305,8 @@ public void createContainerWithLink() throws DockerException { public void createContainerWithMemorySwappiness() throws DockerException { CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withCmd("sleep", "9999") - .withMemorySwappiness(42) + .withHostConfig(newHostConfig() + .withMemorySwappiness(42)) .exec(); assertThat(container.getId(), not(isEmptyString())); LOG.info("Created container {}", container.toString()); @@ -328,8 +335,9 @@ public void createContainerWithLinkInCustomNetwork() throws DockerException { assertNotNull(createNetworkResponse.getId()); CreateContainerResponse container1 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) - .withNetworkMode(networkName) .withCmd("sleep", "9999") + .withHostConfig(newHostConfig() + .withNetworkMode(networkName)) .withName(containerName1) .exec(); @@ -343,10 +351,12 @@ public void createContainerWithLinkInCustomNetwork() throws DockerException { assertThat(inspectContainerResponse1.getState().getRunning(), is(true)); CreateContainerResponse container2 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) - .withNetworkMode(networkName) + .withHostConfig(newHostConfig() + .withNetworkMode(networkName) + .withLinks(new Link(containerName1, containerName1 + "Link")) + ) .withName(containerName2) .withCmd("env") - .withLinks(new Link(containerName1, containerName1 + "Link")) .exec(); LOG.info("Created container {}", container2.toString()); @@ -377,10 +387,11 @@ public void createContainerWithCustomIp() throws DockerException { assertNotNull(createNetworkResponse.getId()); CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) - .withNetworkMode(networkName) .withCmd("sleep", "9999") + .withHostConfig(newHostConfig() + .withNetworkMode(networkName)) .withName(containerName1) - .withIpv4Address(subnetPrefix +".100") + .withIpv4Address(subnetPrefix + ".100") .exec(); assertThat(container.getId(), not(isEmptyString())); @@ -410,8 +421,9 @@ public void createContainerWithAlias() throws DockerException { assertNotNull(createNetworkResponse.getId()); CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) - .withNetworkMode(networkName) .withCmd("sleep", "9999") + .withHostConfig(newHostConfig() + .withNetworkMode(networkName)) .withName(containerName1) .withAliases("server" + dockerRule.getKind()) .exec(); @@ -430,8 +442,11 @@ public void createContainerWithAlias() throws DockerException { @Test public void createContainerWithCapAddAndCapDrop() throws DockerException { - CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withCapAdd(NET_ADMIN) - .withCapDrop(MKNOD).exec(); + CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) + .withHostConfig(newHostConfig() + .withCapAdd(NET_ADMIN) + .withCapDrop(MKNOD)) + .exec(); LOG.info("Created container {}", container.toString()); @@ -451,7 +466,9 @@ public void createContainerWithDns() throws DockerException { String anotherDnsServer = "8.8.4.4"; CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withCmd("true") - .withDns(aDnsServer, anotherDnsServer).exec(); + .withHostConfig(newHostConfig() + .withDns(aDnsServer, anotherDnsServer)) + .exec(); LOG.info("Created container {}", container.toString()); @@ -487,7 +504,9 @@ public void createContainerWithExtraHosts() throws DockerException { CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withName("containerextrahosts" + dockerRule.getKind()) - .withExtraHosts(extraHosts).exec(); + .withHostConfig(newHostConfig() + .withExtraHosts(extraHosts)) + .exec(); LOG.info("Created container {}", container.toString()); @@ -503,7 +522,8 @@ public void createContainerWithExtraHosts() throws DockerException { public void createContainerWithDevices() throws DockerException { CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withCmd("sleep", "9999") - .withDevices(new Device("rwm", "/dev/nulo", "/dev/zero")).exec(); + .withHostConfig(newHostConfig() + .withDevices(new Device("rwm", "/dev/nulo", "/dev/zero"))).exec(); LOG.info("Created container {}", container.toString()); @@ -517,7 +537,7 @@ public void createContainerWithDevices() throws DockerException { @Test public void createContainerWithPortBindings() throws DockerException { - int baseport = getFactoryType() == FactoryType.JERSEY? 11000: 12000; + int baseport = getFactoryType() == FactoryType.JERSEY ? 11000 : 12000; ExposedPort tcp22 = ExposedPort.tcp(22); ExposedPort tcp23 = ExposedPort.tcp(23); @@ -528,7 +548,8 @@ public void createContainerWithPortBindings() throws DockerException { portBindings.bind(tcp23, Binding.bindPort(baseport + 24)); CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withCmd("true") - .withExposedPorts(tcp22, tcp23).withPortBindings(portBindings).exec(); + .withExposedPorts(tcp22, tcp23).withHostConfig(newHostConfig() + .withPortBindings(portBindings)).exec(); LOG.info("Created container {}", container.toString()); @@ -581,7 +602,8 @@ public void createContainerWithLinking() throws DockerException { CreateContainerResponse container2 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withCmd("sleep", "9999") .withName(containerName2) - .withLinks(new Link(containerName1, containerName1 + "Link")).exec(); + .withHostConfig(newHostConfig() + .withLinks(new Link(containerName1, containerName1 + "Link"))).exec(); LOG.info("Created container2 {}", container2.toString()); assertThat(container2.getId(), not(isEmptyString())); @@ -608,7 +630,8 @@ public void createContainerWithRestartPolicy() throws DockerException { RestartPolicy restartPolicy = RestartPolicy.onFailureRestart(5); CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withCmd("sleep", "9999") - .withRestartPolicy(restartPolicy).exec(); + .withHostConfig(newHostConfig() + .withRestartPolicy(restartPolicy)).exec(); LOG.info("Created container {}", container.toString()); @@ -623,7 +646,8 @@ public void createContainerWithRestartPolicy() throws DockerException { public void createContainerWithPidMode() throws DockerException { CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withCmd("true") - .withPidMode("host").exec(); + .withHostConfig(newHostConfig() + .withPidMode("host")).exec(); LOG.info("Created container {}", container.toString()); @@ -644,7 +668,8 @@ public void createContainerWithPidMode() throws DockerException { public void createContainerWithNetworkMode() throws DockerException { CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withCmd("true") - .withNetworkMode("host").exec(); + .withHostConfig(newHostConfig() + .withNetworkMode("host")).exec(); LOG.info("Created container {}", container.toString()); @@ -677,7 +702,8 @@ public void createContainerWithULimits() throws DockerException { CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withName(containerName) - .withUlimits(ulimits).exec(); + .withHostConfig(newHostConfig() + .withUlimits(ulimits)).exec(); LOG.info("Created container {}", container.toString()); @@ -720,7 +746,10 @@ public void createContainerWithLabels() throws DockerException { public void createContainerWithLogConfig() throws DockerException { LogConfig logConfig = new LogConfig(LogConfig.LoggingType.NONE, null); - CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withLogConfig(logConfig).exec(); + CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) + .withHostConfig(newHostConfig() + .withLogConfig(logConfig)) + .exec(); LOG.info("Created container {}", container.toString()); @@ -789,7 +818,8 @@ public void onNext(Frame item) { @Test public void createContainerWithCgroupParent() throws DockerException { CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") - .withCgroupParent("/parent").exec(); + .withHostConfig(newHostConfig() + .withCgroupParent("/parent")).exec(); LOG.info("Created container {}", container.toString()); diff --git a/src/test/java/com/github/dockerjava/cmd/DisconnectFromNetworkCmdIT.java b/src/test/java/com/github/dockerjava/cmd/DisconnectFromNetworkCmdIT.java index 51faa2ea8..220ad89ef 100644 --- a/src/test/java/com/github/dockerjava/cmd/DisconnectFromNetworkCmdIT.java +++ b/src/test/java/com/github/dockerjava/cmd/DisconnectFromNetworkCmdIT.java @@ -5,6 +5,7 @@ import com.github.dockerjava.api.model.Network; import org.junit.Test; +import static com.github.dockerjava.api.model.HostConfig.newHostConfig; import static com.github.dockerjava.junit.DockerAssume.assumeNotSwarm; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -40,8 +41,9 @@ public void forceDisconnectFromNetwork() throws InterruptedException { CreateNetworkResponse network = dockerRule.getClient().createNetworkCmd().withName("testNetwork2" + dockerRule.getKind()).exec(); CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") - .withNetworkMode("testNetwork2" + dockerRule.getKind()) .withCmd("sleep", "9999") + .withHostConfig(newHostConfig() + .withNetworkMode("testNetwork2" + dockerRule.getKind())) .exec(); dockerRule.getClient().startContainerCmd(container.getId()).exec(); diff --git a/src/test/java/com/github/dockerjava/cmd/ListContainersCmdIT.java b/src/test/java/com/github/dockerjava/cmd/ListContainersCmdIT.java index b5210c171..098c592b8 100644 --- a/src/test/java/com/github/dockerjava/cmd/ListContainersCmdIT.java +++ b/src/test/java/com/github/dockerjava/cmd/ListContainersCmdIT.java @@ -4,6 +4,7 @@ import com.github.dockerjava.api.command.InspectContainerResponse; import com.github.dockerjava.api.model.Bind; import com.github.dockerjava.api.model.Container; +import com.github.dockerjava.api.model.HostConfig; import com.github.dockerjava.api.model.Volume; import com.github.dockerjava.core.command.PullImageResultCallback; import com.github.dockerjava.core.command.WaitContainerResultCallback; @@ -257,7 +258,9 @@ public void testVolumeFilter() throws Exception { id = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withLabels(testLabel) - .withBinds(new Bind("TestFilterVolume", new Volume("/test"))) + .withHostConfig(HostConfig.newHostConfig() + .withBinds(new Bind("TestFilterVolume", new Volume("/test"))) + ) .exec() .getId(); @@ -285,7 +288,9 @@ public void testNetworkFilter() throws Exception { id = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withLabels(testLabel) - .withNetworkMode("TestFilterNetwork") + .withHostConfig(HostConfig.newHostConfig() + .withNetworkMode("TestFilterNetwork") + ) .exec() .getId(); diff --git a/src/test/java/com/github/dockerjava/cmd/StartContainerCmdIT.java b/src/test/java/com/github/dockerjava/cmd/StartContainerCmdIT.java index f55e43247..bd0a53f50 100644 --- a/src/test/java/com/github/dockerjava/cmd/StartContainerCmdIT.java +++ b/src/test/java/com/github/dockerjava/cmd/StartContainerCmdIT.java @@ -10,6 +10,7 @@ import com.github.dockerjava.api.model.Bind; import com.github.dockerjava.api.model.Device; import com.github.dockerjava.api.model.ExposedPort; +import com.github.dockerjava.api.model.HostConfig; import com.github.dockerjava.api.model.Link; import com.github.dockerjava.api.model.Ports; import com.github.dockerjava.api.model.Ports.Binding; @@ -29,6 +30,7 @@ import static com.github.dockerjava.api.model.AccessMode.ro; import static com.github.dockerjava.api.model.Capability.MKNOD; import static com.github.dockerjava.api.model.Capability.NET_ADMIN; +import static com.github.dockerjava.api.model.HostConfig.newHostConfig; import static com.github.dockerjava.junit.DockerMatchers.mountedVolumes; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; @@ -53,8 +55,12 @@ public void startContainerWithVolumes() throws DockerException { Volume volume2 = new Volume("/opt/webapp2"); - CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withVolumes(volume1, volume2) - .withCmd("true").withBinds(new Bind("/src/webapp1", volume1, ro), new Bind("/src/webapp2", volume2)) + CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") + .withVolumes(volume1, volume2) + .withCmd("true") + .withHostConfig(newHostConfig() + .withBinds(new Bind("/src/webapp1", volume1, ro), new Bind("/src/webapp2", volume2)) + ) .exec(); LOG.info("Created container {}", container.toString()); @@ -95,7 +101,9 @@ public void startContainerWithVolumesFrom() throws DockerException { CreateContainerResponse container1 = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999") .withName(container1Name) - .withBinds(new Bind("/src/webapp1", volume1), new Bind("/src/webapp2", volume2)).exec(); + .withHostConfig(newHostConfig() + .withBinds(new Bind("/src/webapp1", volume1), new Bind("/src/webapp2", volume2))) + .exec(); LOG.info("Created container1 {}", container1.toString()); dockerRule.getClient().startContainerCmd(container1.getId()).exec(); @@ -107,7 +115,9 @@ public void startContainerWithVolumesFrom() throws DockerException { assertThat(inspectContainerResponse1, mountedVolumes(containsInAnyOrder(volume1, volume2))); CreateContainerResponse container2 = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999") - .withVolumesFrom(new VolumesFrom(container1Name)).exec(); + .withHostConfig(newHostConfig() + .withVolumesFrom(new VolumesFrom(container1Name))) + .exec(); LOG.info("Created container2 {}", container2.toString()); dockerRule.getClient().startContainerCmd(container2.getId()).exec(); @@ -125,8 +135,11 @@ public void startContainerWithDns() throws DockerException { String aDnsServer = "8.8.8.8"; String anotherDnsServer = "8.8.4.4"; - CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("true") - .withDns(aDnsServer, anotherDnsServer).exec(); + CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") + .withCmd("true") + .withHostConfig(newHostConfig() + .withDns(aDnsServer, anotherDnsServer)) + .exec(); LOG.info("Created container {}", container.toString()); @@ -145,8 +158,11 @@ public void startContainerWithDnsSearch() throws DockerException { String dnsSearch = "example.com"; - CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("true") - .withDnsSearch(dnsSearch).exec(); + CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") + .withCmd("true") + .withHostConfig(newHostConfig() + .withDnsSearch(dnsSearch)) + .exec(); LOG.info("Created container {}", container.toString()); @@ -163,7 +179,7 @@ public void startContainerWithDnsSearch() throws DockerException { @Test public void startContainerWithPortBindings() throws DockerException { - int baseport = getFactoryType() == FactoryType.JERSEY? 13000: 14000; + int baseport = getFactoryType() == FactoryType.JERSEY ? 13000 : 14000; ExposedPort tcp22 = ExposedPort.tcp(22); ExposedPort tcp23 = ExposedPort.tcp(23); @@ -173,8 +189,12 @@ public void startContainerWithPortBindings() throws DockerException { portBindings.bind(tcp23, Binding.bindPort(baseport + 23)); portBindings.bind(tcp23, Binding.bindPort(baseport + 24)); - CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("true") - .withExposedPorts(tcp22, tcp23).withPortBindings(portBindings).exec(); + CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") + .withCmd("true") + .withExposedPorts(tcp22, tcp23) + .withHostConfig(newHostConfig() + .withPortBindings(portBindings)) + .exec(); LOG.info("Created container {}", container.toString()); @@ -210,7 +230,11 @@ public void startContainerWithRandomPortBindings() throws DockerException { portBindings.bind(tcp23, Binding.empty()); CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999") - .withExposedPorts(tcp22, tcp23).withPortBindings(portBindings).withPublishAllPorts(true).exec(); + .withExposedPorts(tcp22, tcp23) + .withHostConfig(newHostConfig() + .withPortBindings(portBindings) + .withPublishAllPorts(true)) + .exec(); LOG.info("Created container {}", container.toString()); @@ -241,7 +265,10 @@ public void startContainerWithConflictingPortBindings() throws DockerException { portBindings.bind(tcp23, Binding.bindPort(11022)); CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("true") - .withExposedPorts(tcp22, tcp23).withPortBindings(portBindings).exec(); + .withExposedPorts(tcp22, tcp23) + .withHostConfig(newHostConfig() + .withPortBindings(portBindings)) + .exec(); LOG.info("Created container {}", container.toString()); @@ -283,8 +310,12 @@ public void startContainerWithLinkingDeprecated() throws DockerException { assertThat(inspectContainerResponse1.getState().getExitCode(), is(equalTo(0))); } - CreateContainerResponse container2 = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999") - .withName(container2Name).withLinks(new Link(container1Name, container1Name + "Link")).exec(); + CreateContainerResponse container2 = dockerRule.getClient().createContainerCmd("busybox") + .withCmd("sleep", "9999") + .withName(container2Name) + .withHostConfig(newHostConfig() + .withLinks(new Link(container1Name, container1Name + "Link"))) + .exec(); LOG.info("Created container2 {}", container2.toString()); assertThat(container2.getId(), not(isEmptyString())); @@ -299,7 +330,7 @@ public void startContainerWithLinkingDeprecated() throws DockerException { assertThat(inspectContainerResponse2.getId(), not(isEmptyString())); assertThat(inspectContainerResponse2.getHostConfig(), is(notNullValue())); assertThat(inspectContainerResponse2.getHostConfig().getLinks(), is(notNullValue())); - assertThat(inspectContainerResponse2.getHostConfig().getLinks(), equalTo(new Link[] {new Link(container1Name, + assertThat(inspectContainerResponse2.getHostConfig().getLinks(), equalTo(new Link[]{new Link(container1Name, container1Name + "Link")})); assertThat(inspectContainerResponse2.getId(), startsWith(container2.getId())); assertThat(inspectContainerResponse2.getName(), equalTo("/" + container2Name)); @@ -343,7 +374,10 @@ public void startContainerWithLinking() throws DockerException { } CreateContainerResponse container2 = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999") - .withName(container2Name).withLinks(new Link(container1Name, container1Name + "Link")).exec(); + .withName(container2Name) + .withHostConfig(newHostConfig() + .withLinks(new Link(container1Name, container1Name + "Link"))) + .exec(); LOG.info("Created container2 {}", container2.toString()); assertThat(container2.getId(), not(isEmptyString())); @@ -358,7 +392,7 @@ public void startContainerWithLinking() throws DockerException { assertThat(inspectContainerResponse2.getId(), not(isEmptyString())); assertThat(inspectContainerResponse2.getHostConfig(), is(notNullValue())); assertThat(inspectContainerResponse2.getHostConfig().getLinks(), is(notNullValue())); - assertThat(inspectContainerResponse2.getHostConfig().getLinks(), equalTo(new Link[] {new Link(container1Name, + assertThat(inspectContainerResponse2.getHostConfig().getLinks(), equalTo(new Link[]{new Link(container1Name, container1Name + "Link")})); assertThat(inspectContainerResponse2.getId(), startsWith(container2.getId())); assertThat(inspectContainerResponse2.getName(), equalTo("/" + container2Name)); @@ -371,7 +405,7 @@ public void startContainerWithLinking() throws DockerException { @Test public void startContainer() throws DockerException { - CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd(new String[] {"top"}) + CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd(new String[]{"top"}) .exec(); LOG.info("Created container {}", container.toString()); @@ -400,7 +434,7 @@ public void startContainer() throws DockerException { @Test(expected = NotFoundException.class) public void testStartNonExistingContainer() throws DockerException { - dockerRule.getClient().startContainerCmd("non-existing").exec(); + dockerRule.getClient().startContainerCmd("non-existing").exec(); } /** @@ -412,8 +446,11 @@ public void testStartNonExistingContainer() throws DockerException { @Test public void startContainerWithNetworkMode() throws DockerException { - CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("true") - .withNetworkMode("host").exec(); + CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") + .withCmd("true") + .withHostConfig(newHostConfig() + .withNetworkMode("host")) + .exec(); LOG.info("Created container {}", container.toString()); @@ -431,8 +468,12 @@ public void startContainerWithNetworkMode() throws DockerException { @Test public void startContainerWithCapAddAndCapDrop() throws DockerException { - CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999") - .withCapAdd(NET_ADMIN).withCapDrop(MKNOD).exec(); + CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") + .withCmd("sleep", "9999") + .withHostConfig(newHostConfig() + .withCapAdd(NET_ADMIN) + .withCapDrop(MKNOD)) + .exec(); LOG.info("Created container {}", container.toString()); @@ -453,7 +494,9 @@ public void startContainerWithCapAddAndCapDrop() throws DockerException { public void startContainerWithDevices() throws DockerException { CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999") - .withDevices(new Device("rwm", "/dev/nulo", "/dev/zero")).exec(); + .withHostConfig(newHostConfig() + .withDevices(new Device("rwm", "/dev/nulo", "/dev/zero"))) + .exec(); LOG.info("Created container {}", container.toString()); @@ -473,7 +516,9 @@ public void startContainerWithDevices() throws DockerException { public void startContainerWithExtraHosts() throws DockerException { CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999") - .withExtraHosts("dockerhost:127.0.0.1").exec(); + .withHostConfig(newHostConfig() + .withExtraHosts("dockerhost:127.0.0.1")) + .exec(); LOG.info("Created container {}", container.toString()); @@ -495,7 +540,8 @@ public void startContainerWithRestartPolicy() throws DockerException { RestartPolicy restartPolicy = RestartPolicy.onFailureRestart(5); CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999") - .withRestartPolicy(restartPolicy).exec(); + .withHostConfig(newHostConfig() + .withRestartPolicy(restartPolicy)).exec(); LOG.info("Created container {}", container.toString()); @@ -516,7 +562,9 @@ public void existingHostConfigIsPreservedByBlankStartCmd() throws DockerExceptio String dnsServer = "8.8.8.8"; // prepare a container with custom DNS - CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withDns(dnsServer) + CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") + .withHostConfig(newHostConfig() + .withDns(dnsServer)) .withCmd("true").exec(); LOG.info("Created container {}", container.toString()); diff --git a/src/test/java/com/github/dockerjava/cmd/swarm/SwarmCmdIT.java b/src/test/java/com/github/dockerjava/cmd/swarm/SwarmCmdIT.java index 112a8e689..79759cdd2 100644 --- a/src/test/java/com/github/dockerjava/cmd/swarm/SwarmCmdIT.java +++ b/src/test/java/com/github/dockerjava/cmd/swarm/SwarmCmdIT.java @@ -7,6 +7,7 @@ import com.github.dockerjava.api.exception.NotAcceptableException; import com.github.dockerjava.api.exception.NotFoundException; import com.github.dockerjava.api.model.ExposedPort; +import com.github.dockerjava.api.model.HostConfig; import com.github.dockerjava.api.model.PortBinding; import com.github.dockerjava.api.model.Ports; import com.github.dockerjava.cmd.CmdIT; @@ -22,6 +23,7 @@ import org.junit.BeforeClass; import org.junit.experimental.categories.Category; +import static com.github.dockerjava.api.model.HostConfig.newHostConfig; import static com.github.dockerjava.core.RemoteApiVersion.VERSION_1_24; import static com.github.dockerjava.junit.DockerMatchers.isGreaterOrEqual; import static org.junit.Assume.assumeThat; @@ -102,14 +104,17 @@ protected DockerClient startDockerInDocker() { int port = PORT_START + (numberOfDockersInDocker - 1); CreateContainerResponse response = dockerRule.getClient() .createContainerCmd(DOCKER_IN_DOCKER_IMAGE_REPOSITORY + ":" + DOCKER_IN_DOCKER_IMAGE_TAG) - .withPrivileged(true) + .withHostConfig(newHostConfig() + .withPrivileged(true) + .withNetworkMode(NETWORK_NAME) + .withPortBindings(new PortBinding( + Ports.Binding.bindIpAndPort("127.0.0.1", port), + ExposedPort.tcp(2375) + ))) .withName(name) - .withNetworkMode(NETWORK_NAME) .withAliases(name) - .withPortBindings(new PortBinding( - Ports.Binding.bindIpAndPort("127.0.0.1", port), - ExposedPort.tcp(2375))) .exec(); + dockerRule.getClient().startContainerCmd(response.getId()).exec(); return initializeDockerClient(port); diff --git a/src/test/java/com/github/dockerjava/utils/RegistryUtils.java b/src/test/java/com/github/dockerjava/utils/RegistryUtils.java index ea78795a2..e0251023c 100644 --- a/src/test/java/com/github/dockerjava/utils/RegistryUtils.java +++ b/src/test/java/com/github/dockerjava/utils/RegistryUtils.java @@ -15,6 +15,7 @@ import java.io.File; import java.util.concurrent.TimeUnit; +import static com.github.dockerjava.api.model.HostConfig.newHostConfig; import static com.github.dockerjava.junit.DockerRule.DEFAULT_IMAGE; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.not; @@ -64,7 +65,8 @@ public static synchronized AuthConfig runPrivateRegistry(DockerClient dockerClie CreateContainerResponse testregistry = dockerClient .createContainerCmd(imageName + ":2") .withName(containerName) - .withPortBindings(new PortBinding(Ports.Binding.bindPort(port), ExposedPort.tcp(5000))) + .withHostConfig(newHostConfig() + .withPortBindings(new PortBinding(Ports.Binding.bindPort(port), ExposedPort.tcp(5000)))) .withEnv("REGISTRY_AUTH=htpasswd", "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm", "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd", "REGISTRY_LOG_LEVEL=debug", "REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt", "REGISTRY_HTTP_TLS_KEY=/certs/domain.key")