X Tutup
Skip to content

Commit af8ff78

Browse files
authored
Merge pull request docker-java#795 from KostyaSha/pr/791
Add ability to add labels when running commit
2 parents f646e1f + acb7ee4 commit af8ff78

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import javax.annotation.CheckForNull;
44
import javax.annotation.Nonnull;
55

6+
import java.util.Map;
7+
68
import com.github.dockerjava.api.exception.NotFoundException;
79
import com.github.dockerjava.api.model.ExposedPorts;
810
import com.github.dockerjava.api.model.Volumes;
@@ -29,6 +31,9 @@ public interface CommitCmd extends SyncDockerCmd<String> {
2931
@CheckForNull
3032
String getHostname();
3133

34+
@CheckForNull
35+
Map<String, String> getLabels();
36+
3237
@CheckForNull
3338
Integer getMemory();
3439

@@ -88,6 +93,11 @@ public interface CommitCmd extends SyncDockerCmd<String> {
8893

8994
CommitCmd withHostname(String hostname);
9095

96+
/**
97+
* @since 1.19
98+
*/
99+
CommitCmd withLabels(Map<String, String> labels);
100+
91101
CommitCmd withMemory(Integer memory);
92102

93103
CommitCmd withMemorySwap(Integer memorySwap);

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

Lines changed: 19 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 com.fasterxml.jackson.annotation.JsonProperty;
68
import com.github.dockerjava.api.command.CommitCmd;
79
import com.github.dockerjava.api.exception.NotFoundException;
@@ -43,6 +45,12 @@ public class CommitCmdImpl extends AbstrDockerCmd<CommitCmd, String> implements
4345
@JsonProperty("Hostname")
4446
private String hostname;
4547

48+
/**
49+
* @since 1.19
50+
*/
51+
@JsonProperty("Labels")
52+
private Map<String, String> labels;
53+
4654
@JsonProperty("Memory")
4755
private Integer memory;
4856

@@ -189,6 +197,17 @@ public CommitCmdImpl withEnv(String... env) {
189197
return this;
190198
}
191199

200+
@Override
201+
public Map<String, String> getLabels() {
202+
return labels;
203+
}
204+
205+
@Override
206+
public CommitCmdImpl withLabels(Map<String, String> labels) {
207+
this.labels = labels;
208+
return this;
209+
}
210+
192211
@Override
193212
public ExposedPorts getExposedPorts() {
194213
return exposedPorts;

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import static org.testinfected.hamcrest.jpa.HasFieldWithValue.hasField;
99

1010
import java.lang.reflect.Method;
11+
import java.util.Map;
1112

13+
import com.google.common.collect.ImmutableMap;
1214
import org.testng.ITestResult;
1315
import org.testng.annotations.AfterMethod;
1416
import org.testng.annotations.AfterTest;
@@ -54,7 +56,7 @@ public void commit() throws DockerException {
5456
assertThat(container.getId(), not(isEmptyString()));
5557
dockerClient.startContainerCmd(container.getId()).exec();
5658

57-
LOG.info("Commiting container: {}", container.toString());
59+
LOG.info("Committing container: {}", container.toString());
5860
String imageId = dockerClient.commitCmd(container.getId()).exec();
5961

6062
InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec();
@@ -68,6 +70,26 @@ public void commit() throws DockerException {
6870
assertThat(inspectImageResponse.getParent(), equalTo(busyboxImg.getId()));
6971
}
7072

73+
@Test
74+
public void commitWithLabels() throws DockerException {
75+
76+
CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("touch", "/test").exec();
77+
78+
LOG.info("Created container: {}", container.toString());
79+
assertThat(container.getId(), not(isEmptyString()));
80+
dockerClient.startContainerCmd(container.getId()).exec();
81+
82+
LOG.info("Committing container: {}", container.toString());
83+
Map<String, String> labels = ImmutableMap.of("label1", "abc", "label2", "123");
84+
String imageId = dockerClient.commitCmd(container.getId()).withLabels(labels).exec();
85+
86+
InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec();
87+
LOG.info("Image Inspect: {}", inspectImageResponse.toString());
88+
Map<String, String> responseLabels = inspectImageResponse.getContainerConfig().getLabels();
89+
assertThat(responseLabels.get("label1"), equalTo("abc"));
90+
assertThat(responseLabels.get("label2"), equalTo("123"));
91+
}
92+
7193
@Test(expectedExceptions = NotFoundException.class)
7294
public void commitNonExistingContainer() throws DockerException {
7395

src/test/java/com/github/dockerjava/netty/exec/CommitCmdExecTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import static org.testinfected.hamcrest.jpa.HasFieldWithValue.hasField;
99

1010
import java.lang.reflect.Method;
11+
import java.util.Map;
1112

13+
import com.google.common.collect.ImmutableMap;
1214
import org.testng.ITestResult;
1315
import org.testng.annotations.AfterMethod;
1416
import org.testng.annotations.AfterTest;
@@ -54,7 +56,7 @@ public void commit() throws DockerException {
5456
assertThat(container.getId(), not(isEmptyString()));
5557
dockerClient.startContainerCmd(container.getId()).exec();
5658

57-
LOG.info("Commiting container: {}", container.toString());
59+
LOG.info("Committing container: {}", container.toString());
5860
String imageId = dockerClient.commitCmd(container.getId()).exec();
5961

6062
InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec();
@@ -68,6 +70,26 @@ public void commit() throws DockerException {
6870
assertThat(inspectImageResponse.getParent(), equalTo(busyboxImg.getId()));
6971
}
7072

73+
@Test
74+
public void commitWithLabels() throws DockerException {
75+
76+
CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("touch", "/test").exec();
77+
78+
LOG.info("Created container: {}", container.toString());
79+
assertThat(container.getId(), not(isEmptyString()));
80+
dockerClient.startContainerCmd(container.getId()).exec();
81+
82+
LOG.info("Committing container: {}", container.toString());
83+
Map<String, String> labels = ImmutableMap.of("label1", "abc", "label2", "123");
84+
String imageId = dockerClient.commitCmd(container.getId()).withLabels(labels).exec();
85+
86+
InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec();
87+
LOG.info("Image Inspect: {}", inspectImageResponse.toString());
88+
Map<String, String> responseLabels = inspectImageResponse.getContainerConfig().getLabels();
89+
assertThat(responseLabels.get("label1"), equalTo("abc"));
90+
assertThat(responseLabels.get("label2"), equalTo("123"));
91+
}
92+
7193
@Test(expectedExceptions = NotFoundException.class)
7294
public void commitNonExistingContainer() throws DockerException {
7395

0 commit comments

Comments
 (0)
X Tutup