Docker Data Management: Volumes, Bind Mounts, and tmpfs
The Manage Data in Docker exercise teaches three methods for persisting and sharing data between Docker containers and the host. This exercise appears across 5 courses. This article covers the most common failure modes and how to fix them.
Table of Contents
- Docker Volumes: Persistent Data
- Bind Mounts: Host-Container File Sharing
- tmpfs: In-Memory Temporary Storage
- Bind Mount File vs Directory
- Volume Not Found Error
- Common Questions
Docker Volumes: Persistent Data
Creating and Using Named Volumes
Docker volumes are stored on the host at /var/lib/docker/volumes/<volume_name>/_data/ and persist even after containers are removed:
docker volume create demo
docker run --name ubuntu -d -v demo:/opt -it ubuntu:24.04
Inside the container, write to /opt. After docker rm -f ubuntu, the data still exists on the host:
ls /var/lib/docker/volumes/demo/_data/
Sharing a Volume Between Containers
You can mount the same volume to different containers at different paths:
docker run --name ubuntu1 -d -v demo:/tmp -it ubuntu:24.04
docker exec -it ubuntu1 ls /tmp
The files written by the first container to /opt will appear in the second container at /tmp.
Key concept: Volumes are the recommended way to persist data in production environments.
Bind Mounts: Host-Container File Sharing
Bidirectional File Access
Bind mounts share a specific host path with the container. Changes made inside the container are immediately visible on the host, and vice versa:
docker run --name ubuntu2 -d -v /opt:/opt -it ubuntu:24.04
Key difference from volumes: Bind mounts use an absolute host path, not a named volume. They don't show up in docker volume ls.
Deleting Files Affects Both Sides
If you delete a file inside a bind-mounted container, it's also deleted on the host:
# Inside container:
rm /opt/welcome.txt
# On host - file is gone too:
ls /opt/welcome.txt
# ls: cannot access '/opt/welcome.txt': No such file or directory
Common mistake: Thinking bind mounts are one-directional. They are bidirectional — any change on either side is reflected on the other.
tmpfs: In-Memory Temporary Storage
tmpfs Is Not Persistent
tmpfs mounts use the host's RAM for storage. Data is fast but is lost when the container is removed:
docker run --tmpfs /tmp:rw,noexec,nosuid mycontainer
Key concept: Use tmpfs for sensitive temporary data (like credentials or tokens) that should never be written to disk.
Bind Mount File vs Directory
Mounting a Specific File
The exercise challenge asks you to mount a specific file (/opt/hello.txt) to a specific path inside the container (/src/hello.txt):
docker run --name bindmount -v /opt/hello.txt:/src/hello.txt ubuntu:24.04
Common mistake: Mounting the directory instead of the file:
# WRONG - mounts the /opt directory, not the specific file:
docker run --name bindmount -v /opt:/src ubuntu:24.04
# CORRECT - mounts only the file:
docker run --name bindmount -v /opt/hello.txt:/src/hello.txt ubuntu:24.04
The verification checks mount type is bind, source is /opt/hello.txt, and destination is /src/hello.txt.
Volume Not Found Error
Docker Auto-Creates Volumes
If you reference a volume that doesn't exist in docker run -v, Docker will automatically create it:
docker run --name volumemount -v data:/src ubuntu:24.04
However, the exercise may ask you to explicitly create the volume first:
docker volume create data
Verification checks the volume exists with the local driver:
docker volume ls | grep data
Verify a Volume Mount
docker inspect volumemount | jq '.[].Mounts[] | select(.Type=="volume" and .Destination=="/src")'
Common Questions
| Question | Answer |
|---|---|
| What's the difference between a volume and a bind mount? | Volumes are managed by Docker (/var/lib/docker/volumes/). Bind mounts use a specific host path you specify. |
Does docker volume ls show bind mounts? |
No. docker volume ls only shows Docker-managed volumes. Bind mounts don't appear. |
| When should I use bind mounts vs volumes? | Bind mounts for development (source code sharing). Volumes for production (managed persistence). |
| Why isn't my tmpfs data persisting? | tmpfs is in-memory by design. It's lost when the container stops. |
| My container can't find the mounted file. | Make sure the host path exists before running the container. For bind mounts, the host path must exist. |
Wrap-Up
Docker provides three data storage mechanisms, each with different persistence and use cases:
- Volumes are Docker-managed and persist after container removal. Best for production data.
- Bind mounts share a specific host path bidirectionally. Best for development.
- tmpfs is in-memory and non-persistent. Best for sensitive temporary data.
Remember: the DevSecOps Box is stateless. All Docker containers, volumes, and mounts are lost on page refresh.