com.github.dockerjava.api.model.VolumesFrom Java Examples

The following examples show how to use com.github.dockerjava.api.model.VolumesFrom. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StartContainerCmdIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@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 = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999")
            .withName(container1Name)
            .withHostConfig(newHostConfig()
                    .withBinds(new Bind("/tmp/webapp1", volume1), new Bind("/tmp/webapp2", volume2)))
            .exec();
    LOG.info("Created container1 {}", container1.toString());

    dockerRule.getClient().startContainerCmd(container1.getId()).exec();
    LOG.info("Started container1 {}", container1.toString());

    InspectContainerResponse inspectContainerResponse1 = dockerRule.getClient().inspectContainerCmd(container1.getId())
            .exec();

    assertThat(inspectContainerResponse1, mountedVolumes(containsInAnyOrder(volume1, volume2)));

    CreateContainerResponse container2 = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999")
            .withHostConfig(newHostConfig()
                    .withVolumesFrom(new VolumesFrom(container1Name)))
            .exec();
    LOG.info("Created container2 {}", container2.toString());

    dockerRule.getClient().startContainerCmd(container2.getId()).exec();
    LOG.info("Started container2 {}", container2.toString());

    InspectContainerResponse inspectContainerResponse2 = dockerRule.getClient().inspectContainerCmd(container2.getId())
            .exec();

    assertThat(inspectContainerResponse2, mountedVolumes(containsInAnyOrder(volume1, volume2)));
}
 
Example #2
Source File: CreateContainerCmd.java    From docker-java with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @deprecated see {@link #getHostConfig()}
 */
@Deprecated
@CheckForNull
@JsonIgnore
default VolumesFrom[] getVolumesFrom() {
    return getHostConfig().getVolumesFrom();
}
 
Example #3
Source File: CreateContainerCmd.java    From docker-java with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @deprecated see {@link #getHostConfig()}
 */
@Deprecated
default CreateContainerCmd withVolumesFrom(VolumesFrom... volumesFrom) {
    Objects.requireNonNull(volumesFrom, "volumesFrom was not specified");
    getHostConfig().withVolumesFrom(volumesFrom);
    return this;
}
 
Example #4
Source File: DockerTemplateBase.java    From docker-plugin with MIT License 5 votes vote down vote up
public FormValidation doCheckVolumesFromString(@QueryParameter String volumesFromString) {
    try {
        final String[] strings = splitAndFilterEmpty(volumesFromString, "\n");
        for (String volFrom : strings) {
            VolumesFrom.parse(volFrom);
        }
    } catch (Throwable t) {
        return FormValidation.error(t.getMessage());
    }
    return FormValidation.ok();
}
 
Example #5
Source File: GenericContainer.java    From testcontainers-java with MIT License 4 votes vote down vote up
private void addVolumesFrom(Container container, BindMode mode) {
    volumesFroms.add(new VolumesFrom(container.getContainerName(), mode.accessMode));
}
 
Example #6
Source File: CreateContainerCmdIT.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Test
public void createContainerWithVolumesFrom() throws DockerException {
    String container1Name = UUID.randomUUID().toString();
    CreateVolumeResponse volume1Info = dockerRule.getClient().createVolumeCmd().exec();
    CreateVolumeResponse volume2Info = dockerRule.getClient().createVolumeCmd().exec();

    Volume volume1 = new Volume("/src/webapp1");
    Volume volume2 = new Volume("/src/webapp2");
    Bind bind1 = new Bind(volume1Info.getName(), volume1);
    Bind bind2 = new Bind(volume2Info.getName(), volume2);

    // create a running container with bind mounts
    CreateContainerResponse container1 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE)
            .withCmd("sleep", "9999")
            .withName(container1Name)
            .withHostConfig(newHostConfig()
                    .withBinds(bind1, bind2))
            .exec();

    LOG.info("Created container1 {}", container1.toString());

    InspectContainerResponse inspectContainerResponse1 = dockerRule.getClient().inspectContainerCmd(container1.getId())
            .exec();

    assertThat(Arrays.asList(inspectContainerResponse1.getHostConfig().getBinds()), containsInAnyOrder(bind1, bind2));

    assertThat(inspectContainerResponse1, mountedVolumes(containsInAnyOrder(volume1, volume2)));

    // create a second container with volumes from first container
    CreateContainerResponse container2 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE)
            .withCmd("sleep", "9999")
            .withHostConfig(newHostConfig()
                    .withVolumesFrom(new VolumesFrom(container1Name)))
            .exec();

    LOG.info("Created container2 {}", container2.toString());

    InspectContainerResponse inspectContainerResponse2 = dockerRule.getClient().inspectContainerCmd(container2.getId())
            .exec();

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

    // 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.
    dockerRule.getClient().startContainerCmd(container2.getId()).exec();
    LOG.info("Started container2 {}", container2.toString());

    inspectContainerResponse2 = dockerRule.getClient().inspectContainerCmd(container2.getId()).exec();

    assertThat(inspectContainerResponse2.getHostConfig().getVolumesFrom(), hasItemInArray(new VolumesFrom(
            container1Name)));

    assertThat(inspectContainerResponse2, mountedVolumes(containsInAnyOrder(volume1, volume2)));
}
 
Example #7
Source File: CreateContainerCmd.java    From docker-java with Apache License 2.0 4 votes vote down vote up
/**
 *
 * @deprecated see {@link #getHostConfig()}
 */
@Deprecated
default CreateContainerCmd withVolumesFrom(List<VolumesFrom> volumesFrom) {
    requireNonNull(volumesFrom, "volumesFrom was not specified");
    return withVolumesFrom(volumesFrom.toArray(new VolumesFrom[volumesFrom.size()]));
}