Permalink
Browse files

Add labels to create and inspect container

  • Loading branch information...
carlossg committed Jul 6, 2015
1 parent 74a0048 commit 2d3174528c59d2ebe2255fc3596fc1d1adac2cdb
@@ -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 CreateContainerCmd withImage(String image);
public CreateContainerCmd withLabels(Map<String, String> labels);
/**
* Add link to another container.
*/
@@ -238,4 +242,9 @@
public CreateContainerCmd withMacAddress(String macAddress);
/**
* @return
*/
Map<String, String> getLabels();
}
@@ -51,6 +51,9 @@
@JsonProperty("Image")
private String image;
@JsonProperty("Labels")
private Map<String, String> labels;
@JsonProperty("MacAddress")
private String macAddress;
@@ -184,6 +187,10 @@ public String getImage() {
return onBuild;
}
public Map<String, String> getLabels() {
return labels;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
@@ -2,6 +2,8 @@
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 @@
@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 String getImage() {
return hostConfig.getLinks();
}
@Override
@JsonIgnore
public Map<String, String> getLabels() {
return labels;
}
@Override
@JsonIgnore
public LxcConf[] getLxcConf() {
@@ -471,6 +482,13 @@ public CreateContainerCmdImpl withImage(String image) {
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");
@@ -13,6 +13,8 @@
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 void createContainerWithULimits() throws DockerException {
}
@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)));
}
}

0 comments on commit 2d31745

Please sign in to comment.