X Tutup
Skip to content

Commit 91a36fe

Browse files
committed
Fix CreateContainerCmdImpl.withVolumesFrom()
Another method that did not work until Docker Remote API 1.15, see CreateContainerCmd.withDns(). To fix it, the parameter had to be moved to nested HostConfig.
1 parent f592918 commit 91a36fe

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public class CreateContainerCmdImpl extends AbstrDockerCmd<CreateContainerCmd, C
4141
@JsonProperty("Cmd") private String[] cmd;
4242
@JsonProperty("Image") private String image;
4343
@JsonProperty("Volumes") private Volumes volumes = new Volumes();
44-
@JsonProperty("VolumesFrom") private String[] volumesFrom = new String[]{};
4544
@JsonProperty("WorkingDir") private String workingDir = "";
4645
@JsonProperty("DisableNetwork") private boolean disableNetwork = false;
4746
@JsonProperty("ExposedPorts") private ExposedPorts exposedPorts = new ExposedPorts();
@@ -291,12 +290,12 @@ public CreateContainerCmdImpl withVolumes(Volume... volumes) {
291290

292291
@Override
293292
public String[] getVolumesFrom() {
294-
return volumesFrom;
293+
return hostConfig.getVolumesFrom();
295294
}
296295

297296
@Override
298297
public CreateContainerCmdImpl withVolumesFrom(String... volumesFrom) {
299-
this.volumesFrom = volumesFrom;
298+
this.hostConfig.setVolumesFrom(volumesFrom);
300299
return this;
301300
}
302301

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
import static org.hamcrest.Matchers.containsInAnyOrder;
77
import static org.hamcrest.Matchers.containsString;
88
import static org.hamcrest.Matchers.equalTo;
9+
import static org.hamcrest.Matchers.hasItemInArray;
910
import static org.hamcrest.Matchers.is;
1011
import static org.hamcrest.Matchers.isEmptyString;
1112
import static org.hamcrest.Matchers.not;
1213

1314
import java.lang.reflect.Method;
1415
import java.security.SecureRandom;
1516
import java.util.Arrays;
17+
import java.util.UUID;
1618

1719
import org.testng.ITestResult;
1820
import org.testng.annotations.AfterMethod;
@@ -25,6 +27,7 @@
2527
import com.github.dockerjava.api.DockerException;
2628
import com.github.dockerjava.api.command.CreateContainerResponse;
2729
import com.github.dockerjava.api.command.InspectContainerResponse;
30+
import com.github.dockerjava.api.model.Bind;
2831
import com.github.dockerjava.api.model.HostConfig;
2932
import com.github.dockerjava.api.model.Link;
3033
import com.github.dockerjava.api.model.Links;
@@ -96,6 +99,55 @@ public void createContainerWithVolume() throws DockerException {
9699
contains("/var/log"));
97100
}
98101

102+
@Test
103+
public void createContainerWithVolumesFrom() throws DockerException {
104+
105+
Volume volume1 = new Volume("/opt/webapp1");
106+
Volume volume2 = new Volume("/opt/webapp2");
107+
108+
String container1Name = UUID.randomUUID().toString();
109+
110+
// create a running container with bind mounts
111+
CreateContainerResponse container1 = dockerClient
112+
.createContainerCmd("busybox").withCmd("sleep", "9999")
113+
.withName(container1Name).exec();
114+
LOG.info("Created container1 {}", container1.toString());
115+
116+
dockerClient.startContainerCmd(container1.getId()).withBinds(
117+
new Bind("/src/webapp1", volume1), new Bind("/src/webapp2", volume2)).exec();
118+
LOG.info("Started container1 {}", container1.toString());
119+
120+
InspectContainerResponse inspectContainerResponse1 = dockerClient.inspectContainerCmd(
121+
container1.getId()).exec();
122+
123+
assertContainerHasVolumes(inspectContainerResponse1, volume1, volume2);
124+
125+
// create a second container with volumes from first container
126+
CreateContainerResponse container2 = dockerClient
127+
.createContainerCmd("busybox").withCmd("sleep", "9999")
128+
.withVolumesFrom(container1Name).exec();
129+
LOG.info("Created container2 {}", container2.toString());
130+
131+
InspectContainerResponse inspectContainerResponse2 = dockerClient
132+
.inspectContainerCmd(container2.getId()).exec();
133+
134+
// No volumes are created, the information is just stored in .HostConfig.VolumesFrom
135+
assertThat(inspectContainerResponse2.getHostConfig().getVolumesFrom(), hasItemInArray(container1Name));
136+
137+
// To ensure that the information stored in VolumesFrom really is considered
138+
// when starting the container, we start it and verify that it has the same
139+
// bind mounts as the first container.
140+
// This is somehow out of scope here, but it helped me to understand how the
141+
// VolumesFrom feature really works.
142+
dockerClient.startContainerCmd(container2.getId()).exec();
143+
LOG.info("Started container2 {}", container2.toString());
144+
145+
inspectContainerResponse2 = dockerClient.inspectContainerCmd(container2.getId()).exec();
146+
147+
assertThat(inspectContainerResponse2.getHostConfig().getVolumesFrom(), hasItemInArray(container1Name));
148+
assertContainerHasVolumes(inspectContainerResponse2, volume1, volume2);
149+
}
150+
99151
@Test
100152
public void createContainerWithEnv() throws DockerException {
101153

0 commit comments

Comments
 (0)
X Tutup