What is a Docker volume? How do named volumes, bind mounts and tmpfs differ?
Short answer
Files written in a container live in the temporary writable layer and are lost when the container is removed; data you must keep goes in a volume, outside the container's lifecycle. Three kinds: named volume, bind mount, tmpfs.
3 points to say:
- Named volume (managed by Docker) for DB data, bind mount for code/config in dev, tmpfs for sensitive temp files.
- An empty named volume gets the image's content copied in; a bind mount hides whatever was at that path.
docker compose downkeeps volumes, onlydown -vdeletes them; use--mountso a wrong mount fails loudly.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.