[ Web Proxy ]
URL:
Viewing: https://java.testcontainers.org/supported_docker_environment/../../features/files/ [Back]  [Original]

Files and volumes - Testcontainers for Java
Skip to content
Testcontainers [Testcontainers] Menu
testcontainers-java
Content

Files and volumes

Copying files

Files can be copied into the container before startup, or can be copied from the container after the container has started.

Note

This is the recommended approach for portability cross-docker environments.

Copying to a container before startup

Copying files using MountableFile
GenericContainer<?> container = new GenericContainer<>(TestImages.TINY_IMAGE)
    .withCommand("sleep", "3000")
    .withCopyFileToContainer(
        MountableFile.forClasspathResource("/mappable-resource/"),
        directoryInContainer
    )

Using Transferable, file content will be placed in the specified location.

Copying files using Transferable
GenericContainer<?> container = new GenericContainer<>(TestImages.TINY_IMAGE)
    .withStartupCheckStrategy(new NoopStartupCheckStrategy())
    .withCopyToContainer(Transferable.of("test"), "/tmp/test")
    .waitingFor(new WaitForExitedState(state -> state.getExitCodeLong() > 0))
    .withCommand("sh", "-c", "grep -q test /tmp/test && exit 100")

Setting file mode is also possible.

Copying files using Transferable with file mode
GenericContainer<?> container = new GenericContainer<>(TestImages.TINY_IMAGE)
    .withStartupCheckStrategy(new NoopStartupCheckStrategy())
    .withCopyToContainer(Transferable.of("test", 0777), "/tmp/test")
    .waitingFor(new WaitForExitedState(state -> state.getExitCodeLong() > 0))
    .withCommand("sh", "-c", "ls -ll /tmp | grep '\\-rwxrwxrwx\\|test' && exit 100")

Copying a file from a running container

Copying files from a container
container.copyFileFromContainer(directoryInContainer + fileName, destinationOnHost);

Web Proxy Viewer  |  New URL  |  Original Page