Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add labels to create and inspect container
  • Loading branch information
carlossg committed Jul 6, 2015
1 parent 74a0048 commit 2d31745
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
@@ -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;
Expand Down Expand Up @@ -173,6 +175,8 @@ public static interface Exec extends DockerCmdExec<CreateContainerCmd, CreateCon

public CreateContainerCmd withImage(String image);

public CreateContainerCmd withLabels(Map<String, String> labels);

/**
* Add link to another container.
*/
Expand Down Expand Up @@ -238,4 +242,9 @@ public static interface Exec extends DockerCmdExec<CreateContainerCmd, CreateCon

public CreateContainerCmd withMacAddress(String macAddress);

/**
* @return
*/
Map<String, String> getLabels();

}
Expand Up @@ -51,6 +51,9 @@ public class ContainerConfig {
@JsonProperty("Image")
private String image;

@JsonProperty("Labels")
private Map<String, String> labels;

@JsonProperty("MacAddress")
private String macAddress;

Expand Down Expand Up @@ -184,6 +187,10 @@ public String[] getOnBuild() {
return onBuild;
}

public Map<String, String> getLabels() {
return labels;
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -221,6 +226,12 @@ public Link[] getLinks() {
return hostConfig.getLinks();
}

@Override
@JsonIgnore
public Map<String, String> getLabels() {
return labels;
}

@Override
@JsonIgnore
public LxcConf[] getLxcConf() {
Expand Down Expand Up @@ -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");
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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.