Skip to content

Commit 28e9388

Browse files
committed
engine: freshness updates for storage mounts (bind, volume, tmpfs)
Signed-off-by: David Karlsson <[email protected]>
1 parent f44f322 commit 28e9388

File tree

4 files changed

+423
-430
lines changed

4 files changed

+423
-430
lines changed

content/manuals/engine/storage/_index.md

+45-181
Original file line numberDiff line numberDiff line change
@@ -9,206 +9,70 @@ aliases:
99
---
1010

1111
By default all files created inside a container are stored on a writable
12-
container layer. This means that:
12+
container layer that sits on top of the read-only, immutable image layers.
1313

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.
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.
2317

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.
18+
The writable layer is unique per container. You can't easily extract the data
19+
from the writeable layer to the host, or to another container.
2720

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.
21+
## Storage mount options
3122

32-
## Choose the right type of mount
23+
Docker supports the following types of storage mounts for storing data outside
24+
of the writable layer of the container:
25+
26+
- [Volume mounts](#volume-mounts)
27+
- [Bind mounts](#bind-mounts)
28+
- [tmpfs mounts](#tmpfs-mounts)
29+
- [Named pipes](#named-pipes)
3330

3431
No matter which type of mount you choose to use, the data looks the same from
3532
within the container. It is exposed as either a directory or an individual file
3633
in the container's filesystem.
3734

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-
![Types of mounts and where they live on the Docker host](images/types-of-mounts.webp?w=450&h=300)
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.
92-
93-
### Bind mounts
94-
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
120-
121-
A `tmpfs` mount isn't persisted on disk, either on the Docker host or within a
122-
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.
126-
127-
### Named pipes
128-
129-
[Named pipes](https://docs.microsoft.com/en-us/windows/desktop/ipc/named-pipes)
130-
can be used for communication between the Docker host and a container. Common
131-
use case is to run a third-party tool inside of a container and connect to the
132-
Docker Engine API using a named pipe.
133-
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.
35+
### Volume mounts
14436

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.
37+
Volumes are persistent storage mechanisms managed by the Docker daemon. They
38+
retain data even after the containers using them are removed. Volume data is
39+
not directly accessible from the host filesystem; it can only be accessed by
40+
mounting the volume to a container.
14841

149-
- When you want to store your container's data on a remote host or a cloud
150-
provider, rather than locally.
42+
Volumes are ideal for performance-critical data processing and long-term
43+
storage needs. Since the storage location is managed on the daemon host,
44+
volumes provide the same raw file performance as accessing the host filesystem
45+
directly.
15146

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.
47+
### Bind mounts
18448

185-
- When the file or directory structure of the Docker host is guaranteed to be
186-
consistent with the bind mounts the containers require.
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.
18753

188-
## Good use cases for tmpfs mounts
54+
Use bind mounts when you need to be able to access files from both the
55+
container and the host.
18956

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.
57+
### tmpfs mounts
19458

195-
## Tips for using bind mounts or volumes
59+
A tmpfs mount stores files directly in the host machine's memory, ensuring the
60+
data is not written to disk. This storage is ephemeral: the data is lost when
61+
the container is stopped or restarted, or when the host is rebooted. tmpfs
62+
mounts do not persist data either on the Docker host or within the container's
63+
filesystem.
19664

197-
If you use either bind mounts or volumes, keep the following in mind:
65+
These mounts are suitable for scenarios requiring temporary, in-memory storage,
66+
such as caching intermediate data, handling sensitive information like
67+
credentials, or reducing disk I/O. Use tmpfs mounts only when the data does not
68+
need to persist beyond the current container session.
19869

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.
70+
### Named pipes
20471

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
211-
bind mount or volume is mounted.
72+
[Named pipes](https://docs.microsoft.com/en-us/windows/desktop/ipc/named-pipes)
73+
can be used for communication between the Docker host and a container. Common
74+
use case is to run a third-party tool inside of a container and connect to the
75+
Docker Engine API using a named pipe.
21276

21377
## Next steps
21478

0 commit comments

Comments
 (0)