Add support for copying directories from containers #9476
+217
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 methodContainerState#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 genericInputStream
. Internally, however, this input stream is aTarArchiveInputStream
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 fromdockerClient
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 thecopyFileFromContainer
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 specifiedhostPath
directory. All required directories on the host system are created automatically.This would also provide a real solution to #1647.