Skip to content

Commit 2d31745

Browse files
committed
Add labels to create and inspect container
1 parent 74a0048 commit 2d31745

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

src/main/java/com/github/dockerjava/api/command/CreateContainerCmd.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.dockerjava.api.command;
22

3+
import java.util.Map;
4+
35
import com.github.dockerjava.api.ConflictException;
46
import com.github.dockerjava.api.NotFoundException;
57
import com.github.dockerjava.api.model.Bind;
@@ -173,6 +175,8 @@ public static interface Exec extends DockerCmdExec<CreateContainerCmd, CreateCon
173175

174176
public CreateContainerCmd withImage(String image);
175177

178+
public CreateContainerCmd withLabels(Map<String, String> labels);
179+
176180
/**
177181
* Add link to another container.
178182
*/
@@ -238,4 +242,9 @@ public static interface Exec extends DockerCmdExec<CreateContainerCmd, CreateCon
238242

239243
public CreateContainerCmd withMacAddress(String macAddress);
240244

245+
/**
246+
* @return
247+
*/
248+
Map<String, String> getLabels();
249+
241250
}

src/main/java/com/github/dockerjava/api/model/ContainerConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public class ContainerConfig {
5151
@JsonProperty("Image")
5252
private String image;
5353

54+
@JsonProperty("Labels")
55+
private Map<String, String> labels;
56+
5457
@JsonProperty("MacAddress")
5558
private String macAddress;
5659

@@ -184,6 +187,10 @@ public String[] getOnBuild() {
184187
return onBuild;
185188
}
186189

190+
public Map<String, String> getLabels() {
191+
return labels;
192+
}
193+
187194
@Override
188195
public String toString() {
189196
return ToStringBuilder.reflectionToString(this);

src/main/java/com/github/dockerjava/core/command/CreateContainerCmdImpl.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import static com.google.common.base.Preconditions.checkNotNull;
44

5+
import java.util.Map;
6+
57
import org.apache.commons.lang.builder.ToStringBuilder;
68

79
import com.fasterxml.jackson.annotation.JsonIgnore;
@@ -107,6 +109,9 @@ public class CreateContainerCmdImpl extends AbstrDockerCmd<CreateContainerCmd, C
107109
@JsonProperty("HostConfig")
108110
private HostConfig hostConfig = new HostConfig();
109111

112+
@JsonProperty("Labels")
113+
private Map<String, String> labels;
114+
110115
public CreateContainerCmdImpl(CreateContainerCmd.Exec exec, String image) {
111116
super(exec);
112117
checkNotNull(image, "image was not specified");
@@ -221,6 +226,12 @@ public Link[] getLinks() {
221226
return hostConfig.getLinks();
222227
}
223228

229+
@Override
230+
@JsonIgnore
231+
public Map<String, String> getLabels() {
232+
return labels;
233+
}
234+
224235
@Override
225236
@JsonIgnore
226237
public LxcConf[] getLxcConf() {
@@ -471,6 +482,13 @@ public CreateContainerCmdImpl withImage(String image) {
471482
return this;
472483
}
473484

485+
@Override
486+
public CreateContainerCmdImpl withLabels(Map<String, String> labels) {
487+
checkNotNull(labels, "labels was not specified");
488+
this.labels = labels;
489+
return this;
490+
}
491+
474492
@Override
475493
public CreateContainerCmdImpl withLinks(Link... links) {
476494
checkNotNull(links, "links was not specified");

src/test/java/com/github/dockerjava/core/command/CreateContainerCmdImplTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.lang.reflect.Method;
1414
import java.security.SecureRandom;
1515
import java.util.Arrays;
16+
import java.util.HashMap;
17+
import java.util.Map;
1618
import java.util.UUID;
1719

1820
import static com.github.dockerjava.api.model.Capability.MKNOD;
@@ -490,4 +492,24 @@ public void createContainerWithULimits() throws DockerException {
490492

491493
}
492494

495+
@Test
496+
public void createContainerWithLabels() throws DockerException {
497+
498+
Map<String, String> labels = new HashMap<String, String>();
499+
labels.put("com.github.dockerjava.null", null);
500+
labels.put("com.github.dockerjava.boolean", "true");
501+
502+
CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("sleep", "9999")
503+
.withLabels(labels).exec();
504+
505+
LOG.info("Created container {}", container.toString());
506+
507+
assertThat(container.getId(), not(isEmptyString()));
508+
509+
InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(container.getId()).exec();
510+
511+
// null becomes empty string
512+
labels.put("com.github.dockerjava.null", "");
513+
assertThat(inspectContainerResponse.getConfig().getLabels(), is(equalTo(labels)));
514+
}
493515
}

0 commit comments

Comments
 (0)