Docker Storage Management
-
volumes, the volume stored in a portion of the host file system management Docker (/ var / lib / docker / volumes /) in. Entirely managed by Docker
-
bind mounts, bind mounts, file or directory on the host can be mounted into a container
-
tmpfs, only stored in the memory of the host system, and does not write the host's file system
volumes
#匿名卷
$ docker volume create
$ docker volume ls
$ docker volume create volume1
Start with a container volume
$ docker container run \
-it \
--name shiyanlou001 \
-v volume1:/volume1 \
--rm ubuntu /bin/bash
$ docker container run \
-it --name shiyanlou002 \
--mount type=volume,src=volume1,target=/volume1 \
--rm ubuntu /bin/bash
bind-mounts
$ docker container run \
-it \
-v /home/shiyanlou:/home/shiyanlou \
--name shiyanlou003 \
--rm ubuntu /bin/bash
$ docker container run \
-it \
--mount type=bind,src=/home/shiyanlou,target=/home/shiyanlou \
--name shiyanlou004 \
--rm ubuntu /bin/bash
tmpfs
Only stored in memory in the host.
$ docker run \
-it \
--mount type=tmpfs,target=/test \
--name shiyanlou008 \
--rm ubuntu bash
Data volume container
Create a data volume of the container
# 创建一个名为 vdata 的数据卷
$ docker volume create vdata
# 创建一个挂载了 vdata 的容器,这个容器就是数据卷容器
$ docker container run \
-it \
-v vdata:/vdata
--name ShiyanlouVolume ubuntu /bin/bash
# 在 /vdata 目录下创建一个文本文件
$ echo "I am ShiyanlouVolume" > /vdata/f.txt
# 创建容器 test1
$ docker container run \
-it \
--volumes-from ShiyanlouVolume \
--name test1 ubuntu /bin/bash
# 查看 vdata 目录是否存在
$ ls -dl /vdata/
# 创建一个文件,并写入内容
$ echo "I am test1" > /vdata/test1.txt
# 创建容器 test2
$ docker container run \
-it \
--volumes-from ShiyanlouVolume \
--name test2 ubuntu /bin/bash
# 查看 vdata 目录是否存在
$ ls -dl /vdata/
# 创建一个文件,并写入内容
$ echo "I am test2" > /vdata/test2.txt
# 查看数据卷中的内容
$ ls -al /vdata/
data backup
$ docker container run \
--volumes-from ShiyanlouVolume \
-v /home/shiyanlou/backup:/backup \
ubuntu tar cvf /backup/backup.tar /vdata/
Data Recovery
$ docker container run \
--volumes-from ShiyanlouVolume \
-v /home/shiyanlou/backup:/backup \
ubuntu tar xvf /backup/backup.tar -C /