Skip to content

Commit 9ccbd8f

Browse files
Add Annotations option to HostConfig (#2422)
1 parent dfb9796 commit 9ccbd8f

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

docker-java-api/src/main/java/com/github/dockerjava/api/model/HostConfig.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public static HostConfig newHostConfig() {
7373
@JsonProperty("NanoCpus")
7474
private Long nanoCPUs;
7575

76+
@JsonProperty("Annotations")
77+
private Map<String, String> annotations;
78+
7679
@JsonProperty("CapAdd")
7780
private Capability[] capAdd;
7881

@@ -304,6 +307,11 @@ public Integer getBlkioWeight() {
304307
return blkioWeight;
305308
}
306309

310+
@CheckForNull
311+
public Map<String, String> getAnnotations() {
312+
return annotations;
313+
}
314+
307315
public Capability[] getCapAdd() {
308316
return capAdd;
309317
}
@@ -636,6 +644,11 @@ public HostConfig withBlkioWeightDevice(List<BlkioWeightDevice> blkioWeightDevic
636644
return this;
637645
}
638646

647+
public HostConfig withAnnotations(Map<String, String> annotations) {
648+
this.annotations = annotations;
649+
return this;
650+
}
651+
639652
/**
640653
* @see #capAdd
641654
*/

docker-java/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.github.dockerjava.api.command.InspectContainerResponse;
1111
import com.github.dockerjava.api.exception.ConflictException;
1212
import com.github.dockerjava.api.exception.DockerException;
13+
import com.github.dockerjava.api.DockerClient;
14+
import com.github.dockerjava.core.DefaultDockerClientConfig;
1315
import com.github.dockerjava.api.exception.InternalServerErrorException;
1416
import com.github.dockerjava.api.exception.NotFoundException;
1517
import com.github.dockerjava.api.model.AuthConfig;
@@ -58,6 +60,7 @@
5860
import static com.github.dockerjava.api.model.HostConfig.newHostConfig;
5961
import static com.github.dockerjava.core.RemoteApiVersion.VERSION_1_23;
6062
import static com.github.dockerjava.core.RemoteApiVersion.VERSION_1_24;
63+
import static com.github.dockerjava.core.RemoteApiVersion.VERSION_1_43;
6164
import static com.github.dockerjava.junit.DockerMatchers.isGreaterOrEqual;
6265
import static com.github.dockerjava.junit.DockerMatchers.mountedVolumes;
6366
import static com.github.dockerjava.core.DockerRule.DEFAULT_IMAGE;
@@ -1141,4 +1144,33 @@ public void shouldHandleANetworkAliasWithoutACustomNetworkGracefully() {
11411144
.withCmd("sleep", "9999")
11421145
.exec();
11431146
}
1147+
1148+
@Test
1149+
public void createContainerWithAnnotations() throws DockerException {
1150+
DefaultDockerClientConfig forcedConfig = DefaultDockerClientConfig.createDefaultConfigBuilder()
1151+
.withApiVersion(VERSION_1_43)
1152+
.withRegistryUrl("https://index.docker.io/v1/")
1153+
.build();
1154+
1155+
DockerClient forcedClient = CmdIT.createDockerClient(forcedConfig);
1156+
Map<String, String> annotations = new HashMap<>();
1157+
annotations.put("com.example.key1", "value1");
1158+
annotations.put("com.example.key2", "value2");
1159+
1160+
CreateContainerResponse container = forcedClient.createContainerCmd(DEFAULT_IMAGE)
1161+
.withCmd("sleep", "9999")
1162+
.withHostConfig(newHostConfig()
1163+
.withAnnotations(annotations))
1164+
.exec();
1165+
1166+
LOG.info("Created container {}", container.toString());
1167+
1168+
assertThat(container.getId(), not(is(emptyString())));
1169+
1170+
InspectContainerResponse inspectContainerResponse = forcedClient.inspectContainerCmd(container.getId()).exec();
1171+
1172+
assertThat(inspectContainerResponse.getHostConfig().getAnnotations(), equalTo(annotations));
1173+
assertThat(inspectContainerResponse.getHostConfig().getAnnotations().get("com.example.key1"), equalTo("value1"));
1174+
assertThat(inspectContainerResponse.getHostConfig().getAnnotations().get("com.example.key2"), equalTo("value2"));
1175+
}
11441176
}

0 commit comments

Comments
 (0)