Cloud Native 7 min read

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.

Open Source Linux
Open Source Linux
Open Source Linux
Master Docker Data Persistence: Volumes, Bind Mounts, and Tmpfs Explained

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_nginx
The 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 nginx
The 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.html

1.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.html
The 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_nginx

2. 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 nginx

This command is equivalent to the legacy syntax:

docker run -itd -p 81:80 -v /webapp:/usr/share/nginx/html nginx

2.2 Verify the Mount

2.3 Enter the Container

docker exec -it <container_id> /bin/bash
cd /usr/share/nginx/html
ls
When 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.04

3.2 Write Data Inside the Container

docker exec -it <container_id> /bin/bash
cd /test
echo 123123 > tmp.txt
cat tmp.txt

3.3 Restart the Container and Observe Data Loss

docker restart <container_id>
docker exec -it <container_id> /bin/bash
cd /test
cat tmp.txt
The 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Data PersistencevolumetmpfsBind Mount
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.