Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for copying directories from containers #9476

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Jozott00
Copy link

This PR attempts to improve the API for transferring content from the container to the host system.

The problem with the current API

Currently testcontainers-java has the method ContainerState#copyFileFromContainer(String containerPath, String hostPath) which only supports copying a single file. If you try to copy a directory, the method will create an empty file in the file system of the host.

The second method is the copyFileFromContainer(String containerPath, ThrowingFunction<InputStream, T> function) method, which allows you to use a generic InputStream. Internally, however, this input stream is a TarArchiveInputStream that has been advanced to the first entry. This means that when reading from the input stream, only this first entry is read. It is therefore not possible to read the entire tar archive with this API. If you try to copy a directory, it will read 0 bytes.

The added API

This PR adds two new methods and doesn’t change the signature of the two existing ones.

The first method is copyArchiveFromContainer(String containerPath, ThrowingFunction<InputStream, T> function), which simply passes the raw archive stream from dockerClient to the consumer function. This allows users to implement their own consumers for handling the tar archive.

The second method is copyPathFromContainer(String containerPath, String hostPath), which copies all files and directories from the specified container path to the specified host path.
If the containerPath points to a file, the behaviour is identical to that of the copyFileFromContainer method. However, if the file points to a directory, the file/directory structure within this directory is mirrored to the host file system within the specified hostPath directory. All required directories on the host system are created automatically.

This would also provide a real solution to #1647.

…FileFromContainer

While the copyFileFromContainer allows consuming a generic InputStream of the Archives' first entry, it does not support directories.
The new copyArchiveFromContainer method allows users to process the raw TAR archive InputStream.
Introduces a new `copyPathFromContainer` method in `ContainerState.java` that enables copying files or directories from a Docker container to the host. This method handles both file and directory copying, ensuring proper directory structure on the host. It also includes error handling for various states such as existing files conflicting with directories.
These tests cover various scenarios such as copying single files, nested directories, and handling conflicts and errors.
@Jozott00 Jozott00 requested a review from a team as a code owner October 29, 2024 20:17
@Jozott00 Jozott00 changed the title Add support for coping directories from container Add support for copying directories from containers Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant