
Manage data in Docker
Master data management in Docker: Use volumes and bind mounts to store, persist, and share data across containers.
- The data doesn’t persist when that container no longer exists, and it can be difficult to get the data out of the container if another process needs it.
- A container’s writable layer is tightly coupled to the host machine where the container is running. You can’t easily move the data somewhere else.
- Writing into a container’s writable layer requires a storage driver to manage the filesystem. The storage driver provides a union filesystem, using the Linux kernel. This extra abstraction reduces performance as compared to using data volumes, which write directly to the host filesystem.
tmpfs
mount is used to store files in the host's system memory. If you're running Docker on Windows, named pipe is used to store files in the host's system memory.tmpfs
mounts is to think about where the data lives on the Docker host.- Volumes are stored in a part of the host filesystem which is managed by Docker (
/var/lib/docker/volumes/
on Linux). Non-Docker processes should not modify this part of the filesystem. Volumes are the best way to persist data in Docker. - Bind mounts may be stored anywhere on the host system. They may even be important system files or directories. Non-Docker processes on the Docker host or a Docker container can modify them at any time.
tmpfs
mounts are stored in the host system's memory only, and are never written to the host system's filesystem.
-v
or --volume
flag, but the syntax for each is slightly different. For tmpfs
mounts, you can use the --tmpfs
flag. We recommend using the --mount
flag for both containers and services, for bind mounts, volumes, or tmpfs
mounts, as the syntax is more clear.docker volume create
command, or Docker can create a volume during container or service creation.docker volume prune
.--rm
flag when creating the container, in which case the anonymous volume is destroyed.If you create multiple containers after each other that use anonymous volumes, each container creates its own volume. Anonymous volumes aren’t reused or shared between containers automatically. To share an anonymous volume between two or more containers, you must mount the anonymous volume using the random volume ID.tmpfs
mount isn't persisted on disk, either on the Docker host or within a container. It can be used by a container during the lifetime of the container, to store non-persistent state or sensitive information. For instance, internally, Swarm services use tmpfs
mounts to mount secret into a service's containers.