Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/com/github/dockerjava/api/model/VolumeBind.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public String getContainerPath() {
public String getHostPath() {
return hostPath;
}

@Override
public String toString() {
return hostPath + ":" + containerPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class CreateContainerCmdImpl extends AbstrDockerCmd<CreateContainerCmd, C
@JsonProperty("Cmd") private String[] cmd;
@JsonProperty("Image") private String image;
@JsonProperty("Volumes") private Volumes volumes = new Volumes();
@JsonProperty("VolumesFrom") private String[] volumesFrom = new String[]{};
@JsonProperty("WorkingDir") private String workingDir = "";
@JsonProperty("DisableNetwork") private boolean disableNetwork = false;
@JsonProperty("ExposedPorts") private ExposedPorts exposedPorts = new ExposedPorts();
Expand Down Expand Up @@ -291,12 +290,12 @@ public CreateContainerCmdImpl withVolumes(Volume... volumes) {

@Override
public String[] getVolumesFrom() {
return volumesFrom;
return hostConfig.getVolumesFrom();
}

@Override
public CreateContainerCmdImpl withVolumesFrom(String... volumesFrom) {
this.volumesFrom = volumesFrom;
this.hostConfig.setVolumesFrom(volumesFrom);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package com.github.dockerjava.client;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.DockerException;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.model.Volume;
import com.github.dockerjava.api.model.VolumeBind;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.TestDockerCmdExecFactory;
import com.google.common.base.Joiner;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.slf4j.Logger;
Expand All @@ -18,6 +26,8 @@
import java.lang.reflect.Method;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;

public abstract class AbstractDockerClientTest extends Assert {

Expand Down Expand Up @@ -158,4 +168,20 @@ public static boolean available(int port) {
return false;
}

/**
* Asserts that {@link InspectContainerResponse#getVolumes()} (<code>.Volumes</code>)
* has {@link VolumeBind}s for the given {@link Volume}s
*/
public static void assertContainerHasVolumes(InspectContainerResponse inspectContainerResponse,
Volume ... expectedVolumes) {
VolumeBind[] volumeBinds = inspectContainerResponse.getVolumes();
LOG.info("Inspect .Volumes = [{}]", Joiner.on(", ").join(volumeBinds));

List<Volume> volumes = new ArrayList<Volume>();
for (VolumeBind bind : volumeBinds) {
volumes.add(new Volume(bind.getContainerPath()));
}
assertThat(volumes, contains(expectedVolumes));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItemInArray;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.Matchers.not;

import java.lang.reflect.Method;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.UUID;

import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
Expand All @@ -25,6 +27,7 @@
import com.github.dockerjava.api.DockerException;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.Link;
import com.github.dockerjava.api.model.Links;
Expand Down Expand Up @@ -96,6 +99,55 @@ public void createContainerWithVolume() throws DockerException {
contains("/var/log"));
}

@Test
public void createContainerWithVolumesFrom() throws DockerException {

Volume volume1 = new Volume("/opt/webapp1");
Volume volume2 = new Volume("/opt/webapp2");

String container1Name = UUID.randomUUID().toString();

// create a running container with bind mounts
CreateContainerResponse container1 = dockerClient
.createContainerCmd("busybox").withCmd("sleep", "9999")
.withName(container1Name).exec();
LOG.info("Created container1 {}", container1.toString());

dockerClient.startContainerCmd(container1.getId()).withBinds(
new Bind("/src/webapp1", volume1), new Bind("/src/webapp2", volume2)).exec();
LOG.info("Started container1 {}", container1.toString());

InspectContainerResponse inspectContainerResponse1 = dockerClient.inspectContainerCmd(
container1.getId()).exec();

assertContainerHasVolumes(inspectContainerResponse1, volume1, volume2);

// create a second container with volumes from first container
CreateContainerResponse container2 = dockerClient
.createContainerCmd("busybox").withCmd("sleep", "9999")
.withVolumesFrom(container1Name).exec();
LOG.info("Created container2 {}", container2.toString());

InspectContainerResponse inspectContainerResponse2 = dockerClient
.inspectContainerCmd(container2.getId()).exec();

// No volumes are created, the information is just stored in .HostConfig.VolumesFrom
assertThat(inspectContainerResponse2.getHostConfig().getVolumesFrom(), hasItemInArray(container1Name));

// To ensure that the information stored in VolumesFrom really is considered
// when starting the container, we start it and verify that it has the same
// bind mounts as the first container.
// This is somehow out of scope here, but it helped me to understand how the
// VolumesFrom feature really works.
dockerClient.startContainerCmd(container2.getId()).exec();
LOG.info("Started container2 {}", container2.toString());

inspectContainerResponse2 = dockerClient.inspectContainerCmd(container2.getId()).exec();

assertThat(inspectContainerResponse2.getHostConfig().getVolumesFrom(), hasItemInArray(container1Name));
assertContainerHasVolumes(inspectContainerResponse2, volume1, volume2);
}

@Test
public void createContainerWithEnv() throws DockerException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,49 @@ public void startContainerWithVolumes() throws DockerException {
inspectContainerResponse = dockerClient.inspectContainerCmd(container
.getId()).exec();

VolumeBind[] volumeBinds = inspectContainerResponse.getVolumes();
List<String> volumes = new ArrayList<String>();
for(VolumeBind bind :volumeBinds){
volumes.add(bind.getContainerPath());
}
assertThat(volumes, contains(volume1.getPath(), volume2.getPath()));
assertContainerHasVolumes(inspectContainerResponse, volume1, volume2);

assertThat(Arrays.asList(inspectContainerResponse.getVolumesRW()),
contains(volume1, volume2));

}

@Test
public void startContainerWithVolumesFrom() throws DockerException {

Volume volume1 = new Volume("/opt/webapp1");
Volume volume2 = new Volume("/opt/webapp2");

String container1Name = UUID.randomUUID().toString();

CreateContainerResponse container1 = dockerClient
.createContainerCmd("busybox").withCmd("sleep", "9999")
.withName(container1Name).exec();
LOG.info("Created container1 {}", container1.toString());

dockerClient.startContainerCmd(container1.getId()).withBinds(
new Bind("/src/webapp1", volume1), new Bind("/src/webapp2", volume2)).exec();
LOG.info("Started container1 {}", container1.toString());

InspectContainerResponse inspectContainerResponse1 = dockerClient.inspectContainerCmd(
container1.getId()).exec();

assertContainerHasVolumes(inspectContainerResponse1, volume1, volume2);


CreateContainerResponse container2 = dockerClient
.createContainerCmd("busybox").withCmd("sleep", "9999").exec();
LOG.info("Created container2 {}", container2.toString());

dockerClient.startContainerCmd(container2.getId()).withVolumesFrom(container1Name).exec();
LOG.info("Started container2 {}", container2.toString());

InspectContainerResponse inspectContainerResponse2 = dockerClient
.inspectContainerCmd(container2.getId()).exec();

assertContainerHasVolumes(inspectContainerResponse2, volume1, volume2);
}

@Test
public void startContainerWithDns() throws DockerException {

Expand Down