Operations 6 min read

Using Docker Volumes for Persistent Container Storage

This article explains how to use Docker volumes for persistent container storage, demonstrating data creation, volume creation, mounting, data persistence across container restarts, alternative -v usage, and sharing volumes between containers with --volumes-from.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Using Docker Volumes for Persistent Container Storage

Default Container Data Storage

We create some sample data inside a container, stop or delete the container, and observe whether the data is lost.

## 创建数据
[root@myserver ~]# docker ps
... (commands omitted for brevity) ...
# echo 12323 > tst.txt
# cat tst.txt
12323

After stopping and restarting the container, the data remains; after removing the container and recreating it, the data is lost.

Volume Usage Method

We manage Docker volumes using the docker volume command, creating a volume and mounting it into a container.

## 创建一个volume
[root@myserver ~]# docker volume create --name myvolume
myvolume
[root@myserver ~]# docker volume ls
DRIVER      VOLUME NAME
local       myvolume
[root@myserver ~]# docker volume inspect myvolume
[ { "CreatedAt": "2020-07-11T21:52:26-04:00", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/myvolume/_data", "Name": "myvolume", "Options": {}, "Scope": "local" } ]

## 在挂载点创建数据
[root@myserver ~]# cd /var/lib/docker/volumes/myvolume/_data
[root@myserver _data]# echo 11111 > a.txt
[root@myserver _data]# ls
a.txt

## 将卷挂载给容器
[root@myserver ~]# docker run -itd --name bb -v myvolume:/data:rw bulletinboard:1.0
... (container started) ...
[root@myserver ~]# docker exec -it bb bash
root@...:/data# ls
a.txt
root@...:/data# cat a.txt
11111

Alternative Volume Mounting Method

We can also specify a volume with the -v flag when starting a container; Docker will create the volume automatically if it does not exist.

# docker run -itd --name cc -v /tmp/test:/data:rw bulletinboard:1.0
... (container started) ...
# docker volume ls
DRIVER      VOLUME NAME
local       myvolume
# cd /tmp/test
# echo 111 > a.txt
# ls
a.txt
# docker exec -it cc bash
root@...:/data# ls
a.txt

Sharing Volumes Between Containers

Volumes can be shared using the --volumes-from option or by mounting the same volume in multiple containers.

# docker run -itd --name server01 -v vtest:/data webserver:v1
# docker run -itd --name server02 -v vtest:/data webserver:v1
# docker volume inspect vtest
... (inspect output) ...
# echo 123 > /var/lib/docker/volumes/vtest/_data/a.txt

# Alternative using --volumes-from
docker run -itd --name server02 --volumes-from server01 webserver:v1
DockerDevOpscontainervolumePersistent Storage
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

login 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.