Sub topic 2
Three ways to store data: volumes, bind mounts, tmpfs
This chapter assumes you can run docker run, docker ps, and docker rm, and that you know the difference between an image and a container. It gives you the overview. Later chapters cover each mount type in detail.
The problem these mounts solve
A container's own filesystem is a writable layer that is deleted along with the container. Docker offers three ways to attach storage that lives outside that layer. Each one is a different kind of mount, meaning a path inside the container that is backed by something else.
The three mount types
A volume is storage created and managed by Docker. On Linux it normally lives under Docker's own data directory, and you refer to it by name instead of by path.
A bind mount maps an existing file or directory on the host into the container. You choose the host path, and Docker does not manage it.
A tmpfs mount is storage held in the host's memory. Nothing is written to disk, and the contents vanish when the container stops.
| Property | Volume | Bind mount | tmpfs mount |
|---|---|---|---|
| Where data lives | Docker-managed area on the host | Any host path you choose | Host memory |
| Survives container removal | Yes | Yes (it is a host path) | No |
| Created by | Docker, on first use or with `docker volume create` | You, before the container starts | Docker, at container start |
| Typical use | Databases, application state | Source code in development, host config files | Secrets, scratch space, caches |
Seeing each one in a command
The --mount flag takes a type that selects the mount kind. Each of these commands starts a short-lived container with one mount type.
# Volume: Docker creates "appdata" if it does not exist yet
docker run --rm --mount type=volume,source=appdata,target=/data alpine \
sh -c 'echo saved > /data/note.txt'
# Bind mount: the host directory must already exist
mkdir -p app
docker run --rm --mount type=bind,source="$(pwd)/app",target=/work alpine \
sh -c 'echo from-container > /work/note.txt'
cat app/note.txt
# tmpfs: memory-backed, discarded on exit
docker run --rm --mount type=tmpfs,target=/scratch,tmpfs-size=64m alpine \
sh -c 'echo temp > /scratch/note.txt && cat /scratch/note.txt'After these commands, note.txt exists in the appdata volume and in ./app on your host. The tmpfs copy no longer exists anywhere.
Choosing between them
Use these defaults:
- Prefer a named volume for data the application owns, such as database files. Docker handles the location, and the mount behaves the same across hosts.
- Use a bind mount when the host and container must see the same files, as in a development workflow where you edit code in your editor and the container runs it.
- Use tmpfs for data that should never touch disk or only matters while the container runs.
Common errors
- With
--mount type=bind, a missing source path stops the container with an error. The older-vflag silently creates the missing path as a root-owned directory, which often hides a typo. - Mounting over a non-empty path in the image hides the files that were already there. The container sees only the mount's contents. Volumes behave differently here, which a later chapter covers.
- tmpfs mounts work on Linux containers only, and their size counts against host memory.
Exercise
A team runs a web app container and wants to edit its templates in an editor on their laptop and see changes immediately. The same app stores uploads that must survive redeployments, but the team never needs to open the upload files by hand. Which mount type fits each need?
Show answer
Use a bind mount for the templates, because the host and container must share the same files and you edit them directly on the host. Use a named volume for the uploads, because Docker manages the data, it survives container removal, and nobody needs to touch it from the host.