From 08539747c5f6e3b0d879e3388a044c27d014ef05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emirhan=20Durmu=C5=9F?= Date: Sat, 5 Apr 2025 15:35:36 +0300 Subject: [PATCH 1/3] annotations option added to hostconfig --- .../com/github/dockerjava/api/model/HostConfig.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docker-java-api/src/main/java/com/github/dockerjava/api/model/HostConfig.java b/docker-java-api/src/main/java/com/github/dockerjava/api/model/HostConfig.java index 2ad622ca6..603bc6347 100644 --- a/docker-java-api/src/main/java/com/github/dockerjava/api/model/HostConfig.java +++ b/docker-java-api/src/main/java/com/github/dockerjava/api/model/HostConfig.java @@ -73,6 +73,9 @@ public static HostConfig newHostConfig() { @JsonProperty("NanoCpus") private Long nanoCPUs; + @JsonProperty("Annotations") + private Map annotations; + @JsonProperty("CapAdd") private Capability[] capAdd; @@ -304,6 +307,11 @@ public Integer getBlkioWeight() { return blkioWeight; } + @CheckForNull + public Map getAnnotations() { + return annotations; + } + public Capability[] getCapAdd() { return capAdd; } @@ -636,6 +644,11 @@ public HostConfig withBlkioWeightDevice(List blkioWeightDevic return this; } + public HostConfig withAnnotations(Map annotations) { + this.annotations = annotations; + return this; + } + /** * @see #capAdd */ From 9e5e540fbf56aff38d371bc6c70fbdcd37abdfcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emirhan=20Durmu=C5=9F?= Date: Tue, 13 May 2025 11:28:30 +0300 Subject: [PATCH 2/3] test case added for annotations --- .../dockerjava/cmd/CreateContainerCmdIT.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java index de9f564e4..c2d2897ce 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java @@ -1136,4 +1136,27 @@ public void shouldHandleANetworkAliasWithoutACustomNetworkGracefully() { .withCmd("sleep", "9999") .exec(); } + + @Test + public void createContainerWithAnnotations() throws DockerException { + Map annotations = new HashMap<>(); + annotations.put("com.example.key1", "value1"); + annotations.put("com.example.key2", "value2"); + + CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) + .withCmd("sleep", "9999") + .withHostConfig(newHostConfig() + .withAnnotations(annotations)) + .exec(); + + LOG.info("Created container {}", container.toString()); + + assertThat(container.getId(), not(is(emptyString()))); + + InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId()).exec(); + + assertThat(inspectContainerResponse.getHostConfig().getAnnotations(), equalTo(annotations)); + assertThat(inspectContainerResponse.getHostConfig().getAnnotations().get("com.example.key1"), equalTo("value1")); + assertThat(inspectContainerResponse.getHostConfig().getAnnotations().get("com.example.key2"), equalTo("value2")); + } } From fc4b136176fe7b17e7028fce7349c734adea598a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emirhan=20Durmu=C5=9F?= Date: Thu, 3 Jul 2025 19:43:59 +0300 Subject: [PATCH 3/3] createContainerWithAnnotations fixed with forcing to use proper api version --- .../github/dockerjava/cmd/CreateContainerCmdIT.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java index c2d2897ce..5fcc966f5 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java @@ -10,6 +10,8 @@ import com.github.dockerjava.api.command.InspectContainerResponse; import com.github.dockerjava.api.exception.ConflictException; import com.github.dockerjava.api.exception.DockerException; +import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.core.DefaultDockerClientConfig; import com.github.dockerjava.api.exception.InternalServerErrorException; import com.github.dockerjava.api.exception.NotFoundException; import com.github.dockerjava.api.model.AuthConfig; @@ -56,6 +58,7 @@ import static com.github.dockerjava.api.model.HostConfig.newHostConfig; import static com.github.dockerjava.core.RemoteApiVersion.VERSION_1_23; import static com.github.dockerjava.core.RemoteApiVersion.VERSION_1_24; +import static com.github.dockerjava.core.RemoteApiVersion.VERSION_1_43; import static com.github.dockerjava.junit.DockerMatchers.isGreaterOrEqual; import static com.github.dockerjava.junit.DockerMatchers.mountedVolumes; import static com.github.dockerjava.core.DockerRule.DEFAULT_IMAGE; @@ -1139,11 +1142,17 @@ public void shouldHandleANetworkAliasWithoutACustomNetworkGracefully() { @Test public void createContainerWithAnnotations() throws DockerException { + DefaultDockerClientConfig forcedConfig = DefaultDockerClientConfig.createDefaultConfigBuilder() + .withApiVersion(VERSION_1_43) + .withRegistryUrl("https://index.docker.io/v1/") + .build(); + + DockerClient forcedClient = CmdIT.createDockerClient(forcedConfig); Map annotations = new HashMap<>(); annotations.put("com.example.key1", "value1"); annotations.put("com.example.key2", "value2"); - CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) + CreateContainerResponse container = forcedClient.createContainerCmd(DEFAULT_IMAGE) .withCmd("sleep", "9999") .withHostConfig(newHostConfig() .withAnnotations(annotations)) @@ -1153,7 +1162,7 @@ public void createContainerWithAnnotations() throws DockerException { assertThat(container.getId(), not(is(emptyString()))); - InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId()).exec(); + InspectContainerResponse inspectContainerResponse = forcedClient.inspectContainerCmd(container.getId()).exec(); assertThat(inspectContainerResponse.getHostConfig().getAnnotations(), equalTo(annotations)); assertThat(inspectContainerResponse.getHostConfig().getAnnotations().get("com.example.key1"), equalTo("value1"));