Skip to content
This repository was archived by the owner on Mar 21, 2022. It is now read-only.

Commit 73692c0

Browse files
authored
Merge pull request #538 from spotify/dxia/autoremove
Add "autoRemove" field for HostConfig
2 parents b9cfbd3 + d8ecac6 commit 73692c0

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/main/java/com/spotify/docker/client/messages/HostConfig.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public class HostConfig {
9999
private Boolean oomKillDisable;
100100
@JsonProperty("OomScoreAdj")
101101
private Integer oomScoreAdj;
102+
@JsonProperty("AutoRemove")
103+
private Boolean autoRemove;
102104

103105
private HostConfig() {
104106
}
@@ -135,6 +137,7 @@ private HostConfig(final Builder builder) {
135137
this.shmSize = builder.shmSize;
136138
this.oomKillDisable = builder.oomKillDisable;
137139
this.oomScoreAdj = builder.oomScoreAdj;
140+
this.autoRemove = builder.autoRemove;
138141
}
139142

140143
public List<String> binds() {
@@ -257,6 +260,10 @@ public Integer oomScoreAdj() {
257260
return oomScoreAdj;
258261
}
259262

263+
public Boolean autoRemove() {
264+
return autoRemove;
265+
}
266+
260267
@Override
261268
public boolean equals(final Object o) {
262269
if (this == o) {
@@ -296,7 +303,8 @@ public boolean equals(final Object o) {
296303
Objects.equals(this.ipcMode, that.ipcMode) &&
297304
Objects.equals(this.ulimits, that.ulimits) &&
298305
Objects.equals(this.oomKillDisable, that.oomKillDisable) &&
299-
Objects.equals(this.oomScoreAdj, that.oomScoreAdj);
306+
Objects.equals(this.oomScoreAdj, that.oomScoreAdj) &&
307+
Objects.equals(this.autoRemove, that.autoRemove);
300308
}
301309

302310
@Override
@@ -306,7 +314,7 @@ public int hashCode() {
306314
capDrop, networkMode, securityOpt, devices, memory, memorySwap,
307315
memoryReservation, cpuShares, cpusetCpus, cpuQuota, cgroupParent,
308316
restartPolicy, logConfig, ipcMode, ulimits, pidMode, shmSize,
309-
oomKillDisable, oomScoreAdj);
317+
oomKillDisable, oomScoreAdj, autoRemove);
310318
}
311319

312320
@Override
@@ -343,6 +351,7 @@ public String toString() {
343351
.add("shmSize", shmSize)
344352
.add("oomKillDisable", oomKillDisable)
345353
.add("oomScoreAdj", oomScoreAdj)
354+
.add("autoRemove", autoRemove)
346355
.toString();
347356
}
348357

@@ -501,6 +510,7 @@ public static class Builder {
501510
private Long shmSize;
502511
private Boolean oomKillDisable;
503512
private Integer oomScoreAdj;
513+
private Boolean autoRemove;
504514

505515
private Builder() {
506516
}
@@ -537,6 +547,7 @@ private Builder(final HostConfig hostConfig) {
537547
this.shmSize = hostConfig.shmSize;
538548
this.oomKillDisable = hostConfig.oomKillDisable;
539549
this.oomScoreAdj = hostConfig.oomScoreAdj;
550+
this.autoRemove = hostConfig.autoRemove;
540551
}
541552

542553
/**
@@ -1042,6 +1053,18 @@ public Integer oomScoreAdj() {
10421053
return oomScoreAdj;
10431054
}
10441055

1056+
public Boolean autoRemove() {
1057+
return autoRemove;
1058+
}
1059+
1060+
/**
1061+
* Only works for Docker API version >= 1.25.
1062+
*/
1063+
public Builder autoRemove(final Boolean autoRemove) {
1064+
this.autoRemove = autoRemove;
1065+
return this;
1066+
}
1067+
10451068
public HostConfig build() {
10461069
return new HostConfig(this);
10471070
}

src/test/java/com/spotify/docker/client/DefaultDockerClientTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3358,6 +3358,35 @@ public void testOomScoreAdj() throws Exception {
33583358
assertThat(info.hostConfig().oomScoreAdj(), is(500));
33593359
}
33603360

3361+
@Test(expected = ContainerNotFoundException.class)
3362+
public void testAutoRemoveWhenSetToTrue() throws Exception {
3363+
requireDockerApiVersionAtLeast("1.25", "AutoRemove");
3364+
3365+
// Container should be removed after it is stopped (new since API v.1.25)
3366+
// Pull image
3367+
sut.pull(BUSYBOX_LATEST);
3368+
3369+
final ContainerConfig config = ContainerConfig.builder()
3370+
.image(BUSYBOX_LATEST)
3371+
.hostConfig(HostConfig.builder()
3372+
.autoRemove(true) // Default is false
3373+
.build())
3374+
.build();
3375+
3376+
final ContainerCreation container = sut.createContainer(config, randomName());
3377+
3378+
sut.startContainer(container.id());
3379+
3380+
final ContainerInfo info = sut.inspectContainer(container.id());
3381+
assertThat(info.hostConfig().autoRemove(), is(true));
3382+
assertThat(info.state().running(), equalTo(true));
3383+
3384+
sut.stopContainer(container.id(), 5);
3385+
3386+
// A ContainerNotFoundException should be thrown since the container is removed when it stops
3387+
sut.inspectContainer(container.id());
3388+
}
3389+
33613390
@Test
33623391
public void testInspectSwarm() throws Exception {
33633392
requireDockerApiVersionAtLeast("1.24", "swarm support");

0 commit comments

Comments
 (0)