Repository: docker-java/docker-java
Pull request #260 (2015-07-06 18:38:07)

Add labels to create and inspect container


Patch:
diff --git a/pom.xml b/pom.xml
index 7618263a..30ece10a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,4 +1,5 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 	<modelVersion>4.0.0</modelVersion>
 
 	<parent>
@@ -10,7 +11,7 @@
 	<groupId>com.github.docker-java</groupId>
 	<artifactId>docker-java</artifactId>
 	<packaging>jar</packaging>
-	<version>1.4.1-SNAPSHOT</version>
+	<version>1.4.0-SNAPSHOT</version>
 
 	<name>docker-java</name>
 	<url>https://github.com/docker-java/docker-java</url>
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 3b558b6d..3e31321e 100644
--- a/src/main/java/com/github/dockerjava/api/command/CreateContainerCmd.java
+++ b/src/main/java/com/github/dockerjava/api/command/CreateContainerCmd.java
@@ -1,5 +1,7 @@
 package com.github.dockerjava.api.command;
 
+import java.util.Map;
+
 import com.github.dockerjava.api.ConflictException;
 import com.github.dockerjava.api.NotFoundException;
 import com.github.dockerjava.api.model.Bind;
@@ -173,6 +175,8 @@ public interface CreateContainerCmd extends DockerCmd<CreateContainerResponse> {
 
     public CreateContainerCmd withImage(String image);
 
+    public CreateContainerCmd withLabels(Map<String, String> labels);
+
     /**
      * Add link to another container.
      */
@@ -238,4 +242,9 @@ public interface CreateContainerCmd extends DockerCmd<CreateContainerResponse> {
 
     public CreateContainerCmd withMacAddress(String macAddress);
 
+    /**
+     * @return
+     */
+    Map<String, String> getLabels();
+
 }
diff --git a/src/main/java/com/github/dockerjava/api/model/ContainerConfig.java b/src/main/java/com/github/dockerjava/api/model/ContainerConfig.java
index 8ea673a3..0478e5ef 100644
--- a/src/main/java/com/github/dockerjava/api/model/ContainerConfig.java
+++ b/src/main/java/com/github/dockerjava/api/model/ContainerConfig.java
@@ -51,6 +51,9 @@ public class ContainerConfig {
     @JsonProperty("Image")
     private String image;
 
+    @JsonProperty("Labels")
+    private Map<String, String> labels;
+
     @JsonProperty("MacAddress")
     private String macAddress;
 
@@ -184,6 +187,10 @@ public class ContainerConfig {
         return onBuild;
     }
 
+    public Map<String, String> getLabels() {
+        return labels;
+    }
+
     @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 cc082b61..3caf3c1b 100644
--- a/src/main/java/com/github/dockerjava/core/command/CreateContainerCmdImpl.java
+++ b/src/main/java/com/github/dockerjava/core/command/CreateContainerCmdImpl.java
@@ -2,6 +2,8 @@ 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;
@@ -107,6 +109,9 @@ public class CreateContainerCmdImpl extends AbstrDockerCmd<CreateContainerCmd, C
     @JsonProperty("HostConfig")
     private HostConfig hostConfig = new HostConfig();
 
+    @JsonProperty("Labels")
+    private Map<String, String> labels;
+
     public CreateContainerCmdImpl(CreateContainerCmd.Exec exec, String image) {
         super(exec);
         checkNotNull(image, "image was not specified");
@@ -221,6 +226,12 @@ public class CreateContainerCmdImpl extends AbstrDockerCmd<CreateContainerCmd, C
         return hostConfig.getLinks();
     }
 
+    @Override
+    @JsonIgnore
+    public Map<String, String> getLabels() {
+        return labels;
+    }
+
     @Override
     @JsonIgnore
     public LxcConf[] getLxcConf() {
@@ -471,6 +482,13 @@ public class CreateContainerCmdImpl extends AbstrDockerCmd<CreateContainerCmd, C
         return this;
     }
 
+    @Override
+    public CreateContainerCmdImpl withLabels(Map<String, String> labels) {
+        checkNotNull(labels, "labels was not specified");
+        this.labels = labels;
+        return this;
+    }
+
     @Override
     public CreateContainerCmdImpl withLinks(Link... links) {
         checkNotNull(links, "links was not specified");


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 a33cfddf..efeed530 100644
--- a/src/test/java/com/github/dockerjava/core/command/CreateContainerCmdImplTest.java
+++ b/src/test/java/com/github/dockerjava/core/command/CreateContainerCmdImplTest.java
@@ -13,6 +13,8 @@ import org.testng.annotations.*;
 import java.lang.reflect.Method;
 import java.security.SecureRandom;
 import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.UUID;
 
 import static com.github.dockerjava.api.model.Capability.MKNOD;
@@ -490,4 +492,24 @@ public class CreateContainerCmdImplTest extends AbstractDockerClientTest {
 
     }
 
+    @Test
+    public void createContainerWithLabels() throws DockerException {
+
+        Map<String, String> labels = new HashMap<String, String>();
+        labels.put("com.github.dockerjava.null", null);
+        labels.put("com.github.dockerjava.boolean", "true");
+
+        CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("sleep", "9999")
+                .withLabels(labels).exec();
+
+        LOG.info("Created container {}", container.toString());
+
+        assertThat(container.getId(), not(isEmptyString()));
+
+        InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(container.getId()).exec();
+
+        // null becomes empty string
+        labels.put("com.github.dockerjava.null", "");
+        assertThat(inspectContainerResponse.getConfig().getLabels(), is(equalTo(labels)));
+    }
 }