Repository: docker-java/docker-java
Pull request #333 (2015-09-28 15:07:06)

Adding support for withPidMode #332


Patch:
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 63833124..46e09990 100644
--- a/src/main/java/com/github/dockerjava/api/command/CreateContainerCmd.java
+++ b/src/main/java/com/github/dockerjava/api/command/CreateContainerCmd.java
@@ -18,6 +18,8 @@ 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;
+
 public interface CreateContainerCmd extends SyncDockerCmd<CreateContainerResponse> {
 
     public static interface Exec extends DockerCmdSyncExec<CreateContainerCmd, CreateContainerResponse> {
@@ -134,6 +136,9 @@ public interface CreateContainerCmd extends SyncDockerCmd<CreateContainerRespons
 
     public boolean isTty();
 
+    @CheckForNull
+    public String getPidMode();
+
     public CreateContainerCmd withAttachStderr(boolean attachStderr);
 
     public CreateContainerCmd withAttachStdin(boolean attachStdin);
@@ -291,6 +296,7 @@ public interface CreateContainerCmd extends SyncDockerCmd<CreateContainerRespons
 
     public CreateContainerCmd withContainerIDFile(String containerIDFile);
 
+    public CreateContainerCmd withPid(String pidMode);
     /**
      * @return
      */
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 9b31e05a..cbb6e84d 100644
--- a/src/main/java/com/github/dockerjava/api/model/HostConfig.java
+++ b/src/main/java/com/github/dockerjava/api/model/HostConfig.java
@@ -6,6 +6,8 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
 import com.fasterxml.jackson.annotation.JsonProperty;
 
+import javax.annotation.CheckForNull;
+
 @JsonIgnoreProperties(ignoreUnknown = true)
 public class HostConfig {
 
@@ -75,13 +77,17 @@ public class HostConfig {
     @JsonProperty("CpuShares")
     private int cpuShares = 0;
 
+    @JsonProperty("PidMode")
+    private String pidMode;
+
     public HostConfig() {
     }
 
     public HostConfig(Bind[] binds, Link[] links, LxcConf[] lxcConf, LogConfig logConfig, Ports portBindings,
             boolean publishAllPorts, boolean privileged, boolean readonlyRootfs, String[] dns, String[] dnsSearch,
             VolumesFrom[] volumesFrom, String containerIDFile, Capability[] capAdd, Capability[] capDrop,
-            RestartPolicy restartPolicy, String networkMode, Device[] devices, String[] extraHosts, Ulimit[] ulimits) {
+            RestartPolicy restartPolicy, String networkMode, Device[] devices, String[] extraHosts, Ulimit[] ulimits,
+            String pidMode) {
         this.binds = new Binds(binds);
         this.links = new Links(links);
         this.lxcConf = lxcConf;
@@ -101,6 +107,7 @@ public class HostConfig {
         this.devices = devices;
         this.extraHosts = extraHosts;
         this.ulimits = ulimits;
+        this.pidMode = pidMode;
     }
 
     @JsonIgnore
@@ -194,6 +201,11 @@ public class HostConfig {
         return cpuShares;
     }
 
+    @CheckForNull
+    public String getPidMode() {
+        return pidMode;
+    }
+
     @JsonIgnore
     public void setBinds(Bind... binds) {
         this.binds = new Binds(binds);
@@ -273,6 +285,10 @@ public class HostConfig {
         this.ulimits = ulimits;
     }
 
+    public void setPidMode(String pidMode) {
+        this.pidMode = pidMode;
+    }
+
     @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 f29c5c05..d07b4388 100644
--- a/src/main/java/com/github/dockerjava/core/command/CreateContainerCmdImpl.java
+++ b/src/main/java/com/github/dockerjava/core/command/CreateContainerCmdImpl.java
@@ -1,11 +1,5 @@
 package com.github.dockerjava.core.command;
 
-import static com.google.common.base.Preconditions.checkNotNull;
-
-import java.util.Map;
-
-import org.apache.commons.lang.builder.ToStringBuilder;
-
 import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.github.dockerjava.api.ConflictException;
@@ -28,6 +22,11 @@ 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.ToStringBuilder;
+
+import java.util.Map;
+
+import static com.google.common.base.Preconditions.checkNotNull;
 
 /**
  *
@@ -397,6 +396,12 @@ public class CreateContainerCmdImpl extends AbstrDockerCmd<CreateContainerCmd, C
         return tty;
     }
 
+    @Override
+    @JsonIgnore
+    public String getPidMode() {
+        return hostConfig.getPidMode();
+    }
+
     @Override
     public String toString() {
         return new ToStringBuilder(this).append("create container ").append(name != null ? "name=" + name + " " : "")
@@ -733,4 +738,11 @@ public class CreateContainerCmdImpl extends AbstrDockerCmd<CreateContainerCmd, C
         return this;
     }
 
+    @Override
+    public CreateContainerCmd withPid(String pidMode) {
+        checkNotNull(pidMode, "pidMode was not specified");
+        this.hostConfig.setPidMode(pidMode);
+        return this;
+    }
+
 }
diff --git a/src/main/java/com/github/dockerjava/jaxrs/EventsCmdExec.java b/src/main/java/com/github/dockerjava/jaxrs/EventsCmdExec.java
index 6720604d..7936ef15 100644
--- a/src/main/java/com/github/dockerjava/jaxrs/EventsCmdExec.java
+++ b/src/main/java/com/github/dockerjava/jaxrs/EventsCmdExec.java
@@ -1,7 +1,5 @@
 package com.github.dockerjava.jaxrs;
 
-import static com.google.common.net.UrlEscapers.*;
-
 import javax.ws.rs.client.WebTarget;
 
 import org.slf4j.Logger;
@@ -14,6 +12,8 @@ import com.github.dockerjava.core.async.JsonStreamProcessor;
 import com.github.dockerjava.jaxrs.async.AbstractCallbackNotifier;
 import com.github.dockerjava.jaxrs.async.GETCallbackNotifier;
 
+import static com.google.common.net.UrlEscapers.urlPathSegmentEscaper;
+
 public class EventsCmdExec extends AbstrAsyncDockerCmdExec<EventsCmd, Event> implements EventsCmd.Exec {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(EventsCmdExec.class);


Test patch:
diff --git a/src/test/java/com/github/dockerjava/core/command/CreateContainerCmdImplTest.java b/src/test/java/com/github/dockerjava/core/command/CreateContainerCmdImplTest.java
index 1aeeb547..222a4a1e 100644
--- a/src/test/java/com/github/dockerjava/core/command/CreateContainerCmdImplTest.java
+++ b/src/test/java/com/github/dockerjava/core/command/CreateContainerCmdImplTest.java
@@ -1,5 +1,29 @@
 package com.github.dockerjava.core.command;
 
+import com.github.dockerjava.api.ConflictException;
+import com.github.dockerjava.api.DockerException;
+import com.github.dockerjava.api.command.CreateContainerResponse;
+import com.github.dockerjava.api.command.InspectContainerResponse;
+import com.github.dockerjava.api.model.AccessMode;
+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.Link;
+import com.github.dockerjava.api.model.LogConfig;
+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.VolumeRW;
+import com.github.dockerjava.api.model.VolumesFrom;
+import com.github.dockerjava.client.AbstractDockerClientTest;
+import org.testng.ITestResult;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
 import static com.github.dockerjava.api.model.Capability.MKNOD;
 import static com.github.dockerjava.api.model.Capability.NET_ADMIN;
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -21,31 +45,9 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.UUID;
 
-import org.testng.ITestResult;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.AfterTest;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.BeforeTest;
-import org.testng.annotations.Test;
-
-import com.github.dockerjava.api.ConflictException;
-import com.github.dockerjava.api.DockerException;
-import com.github.dockerjava.api.command.CreateContainerResponse;
-import com.github.dockerjava.api.command.InspectContainerResponse;
-import com.github.dockerjava.api.model.AccessMode;
-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.LogConfig;
-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.VolumeRW;
-import com.github.dockerjava.api.model.VolumesFrom;
-import com.github.dockerjava.client.AbstractDockerClientTest;
+import static com.github.dockerjava.api.model.Capability.MKNOD;
+import static com.github.dockerjava.api.model.Capability.NET_ADMIN;
+import static org.hamcrest.MatcherAssert.assertThat;
 
 @Test(groups = "integration")
 public class CreateContainerCmdImplTest extends AbstractDockerClientTest {
@@ -451,6 +453,21 @@ public class CreateContainerCmdImplTest extends AbstractDockerClientTest {
         assertThat(inspectContainerResponse.getHostConfig().getRestartPolicy(), is(equalTo(restartPolicy)));
     }
 
+    @Test
+    public void createContainerWithPidMode() throws DockerException {
+
+        CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("true")
+                .withPid("host").exec();
+
+        LOG.info("Created container {}", container.toString());
+
+        assertThat(container.getId(), not(isEmptyString()));
+
+        InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(container.getId()).exec();
+
+        assertThat(inspectContainerResponse.getHostConfig().getPidMode(), is(equalTo("host")));
+    }
+
     /**
      * This tests support for --net option for the docker run command: --net="bridge" Set the Network mode for the
      * container 'bridge': creates a new network stack for the container on the docker bridge 'none': no networking for