Skip to content

Commit 41da3a7

Browse files
authored
docs: document copy from container (#3215)
* docs: reorder to have more visibility in copy APIs * docs: document CopyFromContainer
1 parent 321f4f9 commit 41da3a7

File tree

2 files changed

+47
-34
lines changed

2 files changed

+47
-34
lines changed

docs/features/files_and_mounts.md

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,6 @@
22

33
Copying data of any type into a container is a very common practice when working with containers. This section will show you how to do it using _Testcontainers for Go_.
44

5-
## Volume mapping
6-
7-
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:
8-
9-
<!--codeinclude-->
10-
[Volume mounts](../../mounts_test.go) inside_block:volumeMounts
11-
<!--/codeinclude-->
12-
13-
!!!tip
14-
This ability of creating volumes is also available for remote Docker hosts.
15-
16-
!!!warning
17-
Bind mounts are not supported, as it could not work with remote Docker hosts.
18-
19-
!!!tip
20-
It is recommended to copy data from your local host machine to a test container using the file copy API
21-
described below, as it is much more portable.
22-
23-
## Mounting images
24-
25-
Since Docker v28, it is possible to mount the file system of an image into a container using the `Mounts` attribute at the `ContainerRequest` struct. For that, use the `DockerImageMountSource` type, which allows you to specify the name of the image to be mounted, and the subpath inside the container where it should be mounted, or simply call the `ImageMount` function, which does exactly that:
26-
27-
<!--codeinclude-->
28-
[Image mounts](../../lifecycle_test.go) inside_block:imageMounts
29-
<!--/codeinclude-->
30-
31-
!!!warning
32-
If the subpath is not a relative path, the creation of the container will fail.
33-
34-
!!!info
35-
Mounting images fails the creation of the container if the underlying container runtime does not support the `image mount` feature, which is available since Docker v28.
36-
375
## Copying files to a container
386

397
If you would like to copy a file to a container, you can do it in two different manners:
@@ -67,7 +35,7 @@ It's also possible to copy an entire directory to a container, and that can happ
6735

6836
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.
6937

70-
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:
7139

7240
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:
7341

@@ -86,3 +54,46 @@ You can leverage the very same mechanism used for copying files to a container,
8654
<!--codeinclude-->
8755
[Copying a directory to a running container](../../docker_files_test.go) inside_block:copyDirectoryToRunningContainerAsDir
8856
<!--/codeinclude-->
57+
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+
68+
## Volume mapping
69+
70+
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:
71+
72+
<!--codeinclude-->
73+
[Volume mounts](../../mounts_test.go) inside_block:volumeMounts
74+
<!--/codeinclude-->
75+
76+
!!!tip
77+
This ability of creating volumes is also available for remote Docker hosts.
78+
79+
!!!warning
80+
Bind mounts are not supported, as it could not work with remote Docker hosts.
81+
82+
!!!tip
83+
It is recommended to copy data from your local host machine to a test container using the file copy API
84+
described below, as it is much more portable.
85+
86+
## Mounting images
87+
88+
Since Docker v28, it is possible to mount the file system of an image into a container using the `Mounts` attribute at the `ContainerRequest` struct. For that, use the `DockerImageMountSource` type, which allows you to specify the name of the image to be mounted, and the subpath inside the container where it should be mounted, or simply call the `ImageMount` function, which does exactly that:
89+
90+
<!--codeinclude-->
91+
[Image mounts](../../lifecycle_test.go) inside_block:imageMounts
92+
<!--/codeinclude-->
93+
94+
!!!warning
95+
If the subpath is not a relative path, the creation of the container will fail.
96+
97+
!!!info
98+
Mounting images fails the creation of the container if the underlying container runtime does not support the `image mount` feature, which is available since Docker v28.
99+

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)