Docker usage guide (4) - the use of data volumes

1. The use of data volumes

Sometimes you need to use a database, but you want its data to be stored locally. Docker provides a data volume for you to easily manipulate data. A data volume is a special directory available to one or more containers that bypasses UFS and provides many useful features:

  • Data volumes can be shared and reused between containers
  • Modifications to the data volume will take effect immediately
  • Updates to data volumes do not affect mirroring
  • The data volume will always exist by default, even if the container is deleted

Note : The use of data volumes is similar to mounting a directory or file in Linux. The files in the directory designated as the mount point in the mirror will be hidden, and the mounted data volume can be displayed.

This experiment environment: Tencent Cloud Server  CentOS 6.7 x86_64

Add a data volume:
# docker run -d -it --name busybox -v /data/ busybox
This creates a /data directory inside the container and mounts a data volume to the container's /data directory.

into the container:
# docker exec -it busybox sh

View the directory map:

# docker inspect -f {{.Volumes}} busybox  
map[/data:/var/lib/docker/volumes/b98191464fb0b1a888507b1e5b324802012297342adfe5d6125bcbfd08b621a9/_data]

You can see that the /data directory in the container is mapped to the /var/lib/docker/volumes/b98191464fb0b1a888507b1e5b324802012297342adfe5d6125bcbfd08b621a9/_data directory, so the data is synchronized between the /data directory in the container and this directory.

[root@sta2 data]# cd  /var/lib/docker/volumes/b98191464fb0b1a888507b1e5b324802012297342adfe5d6125bcbfd08b621a9/_data
[root@sta2 _data]# touch b

View in the /data directory of the container:

/data # ls a b
b file still exists.

The data volume is designed to persist data, its life cycle is independent of the container, Docker does not automatically delete the data volume after the container is deleted, and there is no mechanism such as garbage collection to process data that is not referenced by any container roll. If you need to remove the data volume while deleting the container. docker rm -v This command can be used when removing a container  .

[root@sta2 docker]# docker stop eec30d8d6fce [root@sta2 docker]# docker rm -v eec30d8d6fce
The -v flag can also specify to mount a directory on the local host into the container, and the -v flag can also mount a single file from the host into the container

[root@sta2 docker]# docker run -it --name mybusybox -v /data:/data busybox sh
This method is equivalent to specifying the directory to be mapped in the local machine, and loading the local data volume /data directory to the /data directory in the container.

/ # cd  /data/
/data # ls
a
/data # touch c

然后在本机的 /data 目录查看 c 文件是否存在:

[root@sta2 data]# ls  
a  c

Docker 挂载数据卷的默认权限是读写,用户也可以通过 :ro 指定为只读。

# docker run -it --name mybusybox -v /data:/data:ro busybox sh

二.数据卷容器

如果你有一些持续更新的数据需要在容器之间共享,最好创建数据卷容器。

[root@sta2 data]# docker run -d -v /data/ --name dbdata busybox #首先,创建一个名为 dbdata 的数据卷容器
然后,在其他容器中使用 –volumes-from 来挂载 dbdata 容器中的数据卷。

# docker run -d --volumes-from dbdata --name db1 nginx
# docker run -d --volumes-from dbdata --name db2 nginx

也可以使用 –volumes-from 来挂载来自多个容器的多个数据卷:

# docker run -d --name db3 --volumes-from db1 --volumes-from db nginx
提示:使用 –volumes-from 参数所挂载数据卷的容器自己并不需要保持在运行状态。

备份数据卷

首先使用–volumes-from 标记来创建一个加载 dbdata 容器卷的容器,并从主机挂载当前目录到容器的 /backup 目录。命令如下:
# docker run --volumes-from dbdata -v /data:/backup busybox tar cvf /backup/backup.tar.gz /data

恢复

如果要恢复数据到一个容器,首先创建一个带有空数据卷的容器 dbdata2。

# docker run -v /dbdata --name dbdata2 ubuntu /bin/bash
然后创建另一个容器,挂载 dbdata2 容器卷中的数据卷,并使用 untar 解压备份文件到挂载的容器卷中。

# docker run --volumes-from dbdata2 -v /data:/backup busybox tar xvf /backup/backup.tar.gz
为了查看/验证恢复的数据,可以再启动一个容器挂载同样的容器卷来查看

# docker run --volumes-from dbdata2 busybox /bin/ls /dbdata

删除数据

如果删除了挂载的容器(db1 和 db2),数据卷并不会被自动删除。如果要删除一个数据卷,必须在删除最后一个还挂载着它的容器时使用 docker rm -v 命令来指定同时删除关联的容器。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325779381&siteId=291194637