Master Docker Data Persistence: Volumes, Bind Mounts, and Tmpfs Explained
This guide walks through Docker's three data‑persistence methods—Volumes, bind mounts, and tmpfs mounts—showing how to create, mount, verify, and clean up each option while highlighting their impact on container lifecycle and data durability.
Introduction
In Docker, a container’s read/write data resides in its storage layer and is lost when the container is removed. To achieve persistence, data must be mounted from the host. Docker offers three mounting methods: Volume, bind mounts, and tmpfs mounts.
1. Volume Basic Usage
1.1 Create a Volume
docker volume create for_nginx
docker volume ls
docker volume inspect for_nginxThe created volume for_nginx is stored under /var/lib/docker/volumes/for_nginx/_data on the host.
1.2 Mount the Volume to a Container
docker run -itd -p 88:80 --mount type=volume,source=for_nginx,target=/usr/share/nginx/html nginxThe source specifies the host volume, and target specifies the mount point inside the container.
1.3 Modify the Nginx Homepage
cd /var/lib/docker/volumes/for_nginx/_data/
echo "Hello world" > index.html1.4 Test Access
Access the Nginx page at http://localhost:88 to see the updated content.
1.5 Verify Persistence
docker rm -f <container_id>
cat /var/lib/docker/volumes/for_nginx/_data/index.htmlThe data remains after the container is removed, proving that volumes are independent of container lifecycle.
1.6 Clean Up the Volume
docker volume rm for_nginx2. Bind Mounts Basic Usage
2.1 Run a Container with a Bind Mount
docker run -itd -p 81:80 --mount type=bind,source=/webapp,target=/usr/share/nginx/html nginxThis command is equivalent to the legacy syntax:
docker run -itd -p 81:80 -v /webapp:/usr/share/nginx/html nginx2.2 Verify the Mount
2.3 Enter the Container
docker exec -it <container_id> /bin/bash
cd /usr/share/nginx/html
lsWhen a bind mount targets a non‑empty directory inside the container, the original files are hidden and the container sees only the host’s files, which is the key difference from volumes.
3. Tmpfs Mounts Usage
3.1 Run a Container with a Tmpfs Mount
docker run -itd --mount type=tmpfs,target=/test ubuntu:18.043.2 Write Data Inside the Container
docker exec -it <container_id> /bin/bash
cd /test
echo 123123 > tmp.txt
cat tmp.txt3.3 Restart the Container and Observe Data Loss
docker restart <container_id>
docker exec -it <container_id> /bin/bash
cd /test
cat tmp.txtThe file disappears because tmpfs stores data only in host memory and is removed when the container stops.
3.4 When to Use Tmpfs
Tmpfs is suitable for scenarios where data should not be persisted to disk for security or performance reasons.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
