You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/features/files_and_mounts.md
+44-33Lines changed: 44 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,38 +2,6 @@
2
2
3
3
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_.
4
4
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:
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:
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
-
37
5
## Copying files to a container
38
6
39
7
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
67
35
68
36
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.
69
37
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:
71
39
72
40
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:
73
41
@@ -86,3 +54,46 @@ You can leverage the very same mechanism used for copying files to a container,
86
54
<!--codeinclude-->
87
55
[Copying a directory to a running container](../../docker_files_test.go) inside_block:copyDirectoryToRunningContainerAsDir
88
56
<!--/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:
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:
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.
0 commit comments