Skip to content

Commit 2b2ca9b

Browse files
committed
docs: document CopyFromContainer
1 parent 4c51899 commit 2b2ca9b

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

docs/features/files_and_mounts.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ It's also possible to copy an entire directory to a container, and that can happ
3535

3636
It's important to notice that, when copying the directory to the container, the container path must exist in the Docker image. And this is a strong requirement for files to be copied _before_ the container is started, as we cannot create the full path at that time.
3737

38-
You can leverage the very same mechanism used for copying files to a container, but for directories.:
38+
You can leverage the very same mechanism used for copying files to a container, but for directories:
3939

4040
1. The first way is using the `Files` field in the `ContainerRequest` struct, as shown in the previous section, but using the path of a directory as `HostFilePath`. Like so:
4141

@@ -55,6 +55,16 @@ You can leverage the very same mechanism used for copying files to a container,
5555
[Copying a directory to a running container](../../docker_files_test.go) inside_block:copyDirectoryToRunningContainerAsDir
5656
<!--/codeinclude-->
5757

58+
## Copying files from a container
59+
60+
It's also possible to copy files from a container to the host machine. This can be done by using the `CopyFileFromContainer` method on the `Container` type, which will return an error and an `io.ReadCloser` that you can use to read the file content.
61+
62+
<!--codeinclude-->
63+
[Copying a file from a container](../../examples_test.go) inside_block:copyFileFromContainer
64+
<!--/codeinclude-->
65+
66+
In the above example, we previously copied the file `/tmp/file.txt` to the container, and then we copied it back to the host machine, reading the content of the file.
67+
5868
## Volume mapping
5969

6070
It is possible to map a Docker volume into the container using the `Mounts` attribute at the `ContainerRequest` struct. For that, please pass an instance of the `GenericVolumeMountSource` type, which allows you to specify the name of the volume to be mapped, and the path inside the container where it should be mounted:

examples_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ func ExampleRun() {
103103
fmt.Println(ctrResp.Config.Labels["testcontainers.label"])
104104

105105
// files
106-
f, err := ctr.CopyFileFromContainer(ctx, "/tmp/file.txt")
106+
// copyFileFromContainer {
107+
f, err := ctr.CopyFileFromContainer(context.Background(), "/tmp/file.txt")
107108
if err != nil {
108109
log.Printf("failed to copy file from container: %s", err)
109110
return
@@ -114,6 +115,7 @@ func ExampleRun() {
114115
log.Printf("failed to read file: %s", err)
115116
return
116117
}
118+
// }
117119
fmt.Println(string(content))
118120

119121
// Output:

0 commit comments

Comments
 (0)