Skip to 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

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.

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.

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

container.copyFileFromContainer(directoryInContainer + fileName, destinationOnHost);