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
By default all files created inside a container are stored on a writable
12
-
container layer. This means that:
13
-
14
-
- The data doesn't persist when that container no longer exists, and it can be
15
-
difficult to get the data out of the container if another process needs it.
16
-
- A container's writable layer is tightly coupled to the host machine
17
-
where the container is running. You can't easily move the data somewhere else.
18
-
- Writing into a container's writable layer requires a
19
-
[storage driver](/engine/storage/drivers/) to manage the
20
-
filesystem. The storage driver provides a union filesystem, using the Linux
21
-
kernel. This extra abstraction reduces performance as compared to using
22
-
_data volumes_, which write directly to the host filesystem.
23
-
24
-
Docker has two options for containers to store files on the host machine, so
25
-
that the files are persisted even after the container stops: volumes, and
26
-
bind mounts.
27
-
28
-
Docker also supports containers storing files in-memory on the host machine. Such files are not persisted.
29
-
If you're running Docker on Linux, `tmpfs` mount is used to store files in the host's system memory.
30
-
If you're running Docker on Windows, named pipe is used to store files in the host's system memory.
12
+
container layer that sits on top of the read-only, immutable image layers.
13
+
14
+
Data written to the container layer doesn't persist when the container is
15
+
destroyed. This means that it can be difficult to get the data out of the
16
+
container if another process needs it.
17
+
18
+
The writable layer is tightly coupled to the specific container, and the host
19
+
machine where the container is running. You can't easily extract the data from
20
+
the writeable layer to the host, or to another container.
21
+
22
+
## Storage mount options
31
23
32
-
## Choose the right type of mount
24
+
Docker supports four types of storage mounts for storing data outside of the
25
+
writable layer of the container:
26
+
27
+
-[Volume mounts](#volume-mounts)
28
+
-[Bind mounts](#bind-mounts)
29
+
-[tmpfs mounts](#tmpfs-mounts)
30
+
-[Named pipes](#named-pipes)
33
31
34
32
No matter which type of mount you choose to use, the data looks the same from
35
33
within the container. It is exposed as either a directory or an individual file
36
34
in the container's filesystem.
37
35
38
-
An easy way to visualize the difference among volumes, bind mounts, and `tmpfs`
39
-
mounts is to think about where the data lives on the Docker host.
40
-
41
-

42
-
43
-
- Volumes are stored in a part of the host filesystem which is _managed by
44
-
Docker_ (`/var/lib/docker/volumes/` on Linux). Non-Docker processes should not
45
-
modify this part of the filesystem. Volumes are the best way to persist data
46
-
in Docker.
47
-
48
-
- Bind mounts may be stored anywhere on the host system. They may even be
49
-
important system files or directories. Non-Docker processes on the Docker host
50
-
or a Docker container can modify them at any time.
51
-
52
-
-`tmpfs` mounts are stored in the host system's memory only, and are never
53
-
written to the host system's filesystem.
54
-
55
-
Bind mounts and volumes can both be mounted into containers using the `-v` or
56
-
`--volume` flag, but the syntax for each is slightly different. For `tmpfs`
57
-
mounts, you can use the `--tmpfs` flag. We recommend using the `--mount` flag
58
-
for both containers and services, for bind mounts, volumes, or `tmpfs` mounts,
59
-
as the syntax is more clear.
60
-
61
-
### Volumes
62
-
63
-
Volumes are created and managed by Docker. You can create a volume explicitly
64
-
using the `docker volume create` command, or Docker can create a volume during
65
-
container or service creation.
66
-
67
-
When you create a volume, it's stored within a directory on the Docker
68
-
host. When you mount the volume into a container, this directory is what's
69
-
mounted into the container. This is similar to the way that bind mounts work,
70
-
except that volumes are managed by Docker and are isolated from the core
71
-
functionality of the host machine.
72
-
73
-
A given volume can be mounted into multiple containers simultaneously. When no
74
-
running container is using a volume, the volume is still available to Docker
75
-
and isn't removed automatically. You can remove unused volumes using `docker
76
-
volume prune`.
77
-
78
-
When you mount a volume, it may be named or anonymous. Anonymous volumes are
79
-
given a random name that's guaranteed to be unique within a given Docker host.
80
-
Just like named volumes, anonymous volumes persist even if you remove the
81
-
container that uses them, except if you use the `--rm` flag when creating the
82
-
container, in which case the anonymous volume is destroyed.
83
-
See [Remove anonymous volumes](volumes.md#remove-anonymous-volumes).
84
-
If you create multiple containers after each other that use anonymous volumes,
85
-
each container creates its own volume.
86
-
Anonymous volumes aren't reused or shared between containers automatically.
87
-
To share an anonymous volume between two or more containers,
88
-
you must mount the anonymous volume using the random volume ID.
89
-
90
-
Volumes also support the use of volume drivers, which allow you to store
91
-
your data on remote hosts or cloud providers, among other possibilities.
36
+
### Volume mounts
37
+
38
+
Volumes are persistent data stores that survive when containers that mount
39
+
the volumes are destroyed. Volumes store data in an opaque way that's
40
+
completely managed by Docker. Data stored in volumes is not directly accessible
41
+
from the host; you can only access data inside a volume by mounting it to a
42
+
container.
43
+
44
+
For performance-critical data processing, Volumes are the best way to persist
45
+
data in Docker, as they're generally speaking faster than bind mounts.
92
46
93
47
### Bind mounts
94
48
95
-
Bind mounts have limited functionality compared to volumes. When you use a bind
96
-
mount, a file or directory on the host machine is mounted into a container. The
97
-
file or directory is referenced by its full path on the host machine. The file
98
-
or directory doesn't need to exist on the Docker host already. It is created on
99
-
demand if it doesn't yet exist. Bind mounts are fast, but they rely on the host
100
-
machine's filesystem having a specific directory structure available. If you
101
-
are developing new Docker applications, consider using named volumes instead.
102
-
You can't use Docker CLI commands to directly manage bind mounts.
103
-
104
-
> [!IMPORTANT]
105
-
>
106
-
> Bind mounts allow write access to files on the host by default.
107
-
>
108
-
> One side effect of using bind mounts is that you can change the host
109
-
> filesystem via processes running in a container, including creating,
110
-
> modifying, or deleting important system files or directories. This is a
111
-
> powerful ability which can have security implications, including impacting
112
-
> non-Docker processes on the host system.
113
-
114
-
> [!TIP]
115
-
>
116
-
> Working with large repositories or monorepos, or with virtual file systems that are no longer scaling with your codebase?
117
-
> Check out [Synchronized file shares](/manuals/desktop/features/synchronized-file-sharing.md). It provides fast and flexible host-to-VM file sharing by enhancing bind mount performance through the use of synchronized filesystem caches.
118
-
119
-
### tmpfs
49
+
Bind mounts create a direct link between a host system path and a container,
50
+
allowing access to files or directories stored anywhere on the host. Since they
51
+
aren't isolated by Docker, both non-Docker processes on the host and container
52
+
processes can modify the mounted files simultaneously.
53
+
54
+
Use bind mounts when you need to be able to access files from both the
55
+
container and the host.
56
+
57
+
### tmpfs mounts
58
+
59
+
A `tmpfs` mount stores files in-memory on the host machine. Such files are not persisted.
60
+
If you're running Docker on Linux, `tmpfs` mount is used to store files in the host's system memory.
61
+
If you're running Docker on Windows, named pipe is used to store files in the host's system memory.
120
62
121
63
A `tmpfs` mount isn't persisted on disk, either on the Docker host or within a
122
64
container. It can be used by a container during the lifetime of the container,
123
-
to store non-persistent state or sensitive information. For instance,
124
-
internally, Swarm services use `tmpfs` mounts to mount
125
-
[secrets](/manuals/engine/swarm/secrets.md) into a service's containers.
65
+
to store non-persistent state or sensitive information.
66
+
67
+
Use tmpfs in specific scenarios where you need temporary, in-memory storage
68
+
that doesn't persist data.
126
69
127
70
### Named pipes
128
71
@@ -131,85 +74,6 @@ can be used for communication between the Docker host and a container. Common
131
74
use case is to run a third-party tool inside of a container and connect to the
132
75
Docker Engine API using a named pipe.
133
76
134
-
## Good use cases for volumes
135
-
136
-
Volumes are the preferred way to persist data in Docker containers and services.
137
-
Some use cases for volumes include:
138
-
139
-
- Sharing data among multiple running containers. If you don't explicitly create
140
-
it, a volume is created the first time it is mounted into a container. When
141
-
that container stops or is removed, the volume still exists. Multiple
142
-
containers can mount the same volume simultaneously, either read-write or
143
-
read-only. Volumes are only removed when you explicitly remove them.
144
-
145
-
- When the Docker host is not guaranteed to have a given directory or file
146
-
structure. Volumes help you decouple the configuration of the Docker host
147
-
from the container runtime.
148
-
149
-
- When you want to store your container's data on a remote host or a cloud
150
-
provider, rather than locally.
151
-
152
-
- When you need to back up, restore, or migrate data from one Docker
153
-
host to another, volumes are a better choice. You can stop containers using
154
-
the volume, then back up the volume's directory
155
-
(such as `/var/lib/docker/volumes/<volume-name>`).
156
-
157
-
- When your application requires high-performance I/O on Docker Desktop. Volumes
158
-
are stored in the Linux VM rather than the host, which means that the reads and writes
159
-
have much lower latency and higher throughput.
160
-
161
-
- When your application requires fully native file system behavior on Docker
162
-
Desktop. For example, a database engine requires precise control over disk
163
-
flushing to guarantee transaction durability. Volumes are stored in the Linux
164
-
VM and can make these guarantees, whereas bind mounts are remoted to macOS or
165
-
Windows, where the file systems behave slightly differently.
166
-
167
-
## Good use cases for bind mounts
168
-
169
-
In general, you should use volumes where possible. Bind mounts are appropriate
170
-
for the following types of use case:
171
-
172
-
- Sharing configuration files from the host machine to containers. This is how
173
-
Docker provides DNS resolution to containers by default, by mounting
174
-
`/etc/resolv.conf` from the host machine into each container.
175
-
176
-
- Sharing source code or build artifacts between a development environment on
177
-
the Docker host and a container. For instance, you may mount a Maven `target/`
178
-
directory into a container, and each time you build the Maven project on the
179
-
Docker host, the container gets access to the rebuilt artifacts.
180
-
181
-
If you use Docker for development this way, your production Dockerfile would
182
-
copy the production-ready artifacts directly into the image, rather than
183
-
relying on a bind mount.
184
-
185
-
- When the file or directory structure of the Docker host is guaranteed to be
186
-
consistent with the bind mounts the containers require.
187
-
188
-
## Good use cases for tmpfs mounts
189
-
190
-
`tmpfs` mounts are best used for cases when you do not want the data to persist
191
-
either on the host machine or within the container. This may be for security
192
-
reasons or to protect the performance of the container when your application
193
-
needs to write a large volume of non-persistent state data.
194
-
195
-
## Tips for using bind mounts or volumes
196
-
197
-
If you use either bind mounts or volumes, keep the following in mind:
198
-
199
-
- If you mount an **empty volume** into a directory in the container in which files
200
-
or directories exist, these files or directories are propagated (copied)
201
-
into the volume. Similarly, if you start a container and specify a volume which
202
-
does not already exist, an empty volume is created for you.
203
-
This is a good way to pre-populate data that another container needs.
204
-
205
-
- If you mount a **bind mount or non-empty volume** into a directory in the container
206
-
in which some files or directories exist, these files or directories are
207
-
obscured by the mount, just as if you saved files into `/mnt` on a Linux host
208
-
and then mounted a USB drive into `/mnt`. The contents of `/mnt` would be
209
-
obscured by the contents of the USB drive until the USB drive was unmounted.
210
-
The obscured files are not removed or altered, but are not accessible while the
0 commit comments