Master Docker Data Volumes: Persist and Share Container Data Efficiently
This guide explains how Docker handles data persistence using volumes, covering host‑directory mounts, dedicated volume containers, read‑only options, and practical commands for creating, sharing, backing up, restoring, and migrating data across containers.
Common Docker Data Management Questions
How to persist data in a Docker container, e.g., uploaded files or logs?
How to update files inside a container without rebuilding the image each time?
Now let's discuss Docker data management: data volumes.
Docker provides two ways to manage data
Mount a host directory or file.
Create a dedicated data volume container and share it with related containers.
1. Basic concept of data volumes
Data volumes are special directories that can be used by one or more containers, bypassing the Union File System (UFS) and offering useful features.
Data volumes can be shared and reused across containers
Changes to a volume take effect immediately
Updating a volume does not affect the image
A volume persists until no container uses itTip: Using a data volume is similar to mounting a directory or file in Linux.
You can add a data volume to a container with the -v option of docker run. Multiple -v flags can be used in a single command to mount several volumes.
2. Mounting a host folder as a volume
Using the -v flag you can also mount a host folder into the container.
# docker run -d -v /data/www:/var/www/html -p 80:80 httpd-phpThis mounts the host directory /data/www to /var/www/html inside the container, useful for testing by editing source code on the host.
View the mount result:
Note: Mounting host files is not portable and cannot be defined in a Dockerfile.
By default Docker mounts volumes with read‑write permission, but you can mount them read‑only:
# docker run -d -v /data/www:/var/www/html:ro -p 80:80 httpd-phpThe :ro option makes the mount read‑only.
3. Creating and using a dedicated volume container
If you need persistent data shared among containers, create a named volume container and mount it from other containers.
# docker run -d -p 4444:22 -v /data --name data centos:sshdMount the /dbdata volume in another container using --volumes-from:
# docker run -d -p 5555:22 --volumes-from data --name test centos:sshdFiles created in /data of the data container are instantly visible in the test container.
4. Backing up, restoring, and migrating data volumes
Data volumes can be backed up, restored, or migrated using another container.
# docker run --volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdataThis creates a new container, mounts the dbdata volume, and archives its contents to /backup/backup.tar.
# docker run -v /dbdata --name dbdata2 ubuntu /bin/bashThen restore the backup into a new volume:
# docker run --volumes-from dbdata2 -v $(pwd):/backup busybox tar xvf /backup/backup.tarYou can automate backup, migration, and restoration with your preferred tools.
Data volumes effectively solve container data persistence; logs and application data can be placed in volumes. Future articles will cover container log management.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
