Home

Files and volumes

qianmoQqianmoQ· 更新于 2026-10-13· 阅读 4 分钟· 0 次阅读

登录后可跨设备保存划线和私人笔记登录

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);

评论

登录后参与评论

正在加载评论…