You must know Docker data volume (Volume)

Benpian has joined " the .NET Core ON K8S study and practice series index ", you can click to see more container technology related series of articles.

First, the data will be mounted to the container Docker

  In Docker in order to achieve persistent data (so-called persistent data Docker i.e. data is not ended with the end of the Container ), the need for data from the host to mount into the container. Docker currently provides three different ways to mount the data from the host to the vessel:

  (1) volumes: Docker part of the host file system management, by default in / var / lib / docker / volumes directory; ( most common way )

  

  From the above chart may know, all current Container data are stored in the directory below, without specifying when creating the volume, so the default Docker help us create many anonymous (names on top of this pile of long ID) volumes.

  (2) bind mounts: means may be stored anywhere on the host system; ( more commonly used way )

  However, bind mount at different host systems are not portable, such as Windows and Linux directory structure is not the same, bind mount points to host directory can not be the same. This is also the reason why the bind mount in Dockerfile not appear, because it would not be Dockerfile transplant.

  (3) tmpfs: Mount stored in memory in the host system, the host does not write to the file system; ( generally not used the way )

  Schematic three ways as follows:

  

Second, the basic use of Volume

2.1 Volume Management

Docker-EDC the Create Volume # nginx-Vol // create a custom container volume 
# Docker Volume LS  // View all container volume 
# Docker Volume EDC-the Inspect nginx-Vol // View details information specified container volume

  For example, here we create a custom container volume, entitled "edc-nginx-vol":

  

2.2 Creating a specified volume of container

  With custom container volume, we can use this data to create a volume of container, here we nginx as an example:

# docker run -d -it --name=edc-nginx -p 8800:80 -v edc-nginx-vol:/usr/share/nginx/html nginx

  Wherein, on behalf -v mount data volume, as used herein, the custom data volume edc-nginx-vol, and the data volume mount / usr / share / nginx / html (yum This directory is installed nginx default directory page).

  If not specified by -v, then the default Docker will help us create anonymous data mapping and mount volumes.

  After creating the container, we can look into the container inside:

  

   You can see there are two default page, then we start a new SSH connection to the host go to the data volume just created inside look:

  

   You can see, we have access to two default page inside the container, can be seen, volume help function similar to a soft link us to do. Changes inside the container, we can perceive in the host, the host and the changes in the inside, inside the container can be perceived.

  At this point, if we manually stop and remove the current nginx container, container volume we will find inside the file is still, and has not been removed.

  

   由此可以验证,在数据卷里边的东西是可以持久化的。如果下次还需要创建一个nginx容器,那么还是复用当前数据卷里面的文件。

  此外,我们还可以启动多个nginx容器实例,并且共享同一个数据卷,复用性和扩展性较强。

2.3 清理卷

  如果不再使用自定义数据卷了,那么可以手动清理掉:

# docker stop edc-nginx // 暂停容器实例
# docker rm edc-nginx // 移除容器实例
# docker volume rm edc-nginx-vol // 删除自定义数据卷

三、Bind Mounts的基本使用

3.1 使用卷创建一个容器

docker run -d -it --name=edc-nginx -v /app/wwwroot:/usr/share/nginx/html nginx

  这里指定了将宿主机上的 /app/wwwroot 目录(如果没有会自动创建)挂载到 /usr/share/nginx/html (这个目录是yum安装nginx的默认网页目录)。

  这时我们再次进入容器内部看看:

  

   可以看到,与volumes不同,bind mounts的方式会隐藏掉被挂载目录里面的内容(如果非空的话),这里是/usr/share/nginx/html 目录下的内容被隐藏掉了,因此我们看不到。

  但是,我们可以将宿主机上的文件随时挂载到容器中:

  Step1.新建一个index.html

  

  Step2.在容器中查看

  

3.2 验证绑定

docker inspect edc-nginx

   通过上述命令可以看到一大波配置,我们要关注的是:

  

3.3 清理

docker stop edc-nginx
docker rm edc-nginx

  同volumes一样,当我们清理掉容器之后,挂载目录里面的文件仍然还在,不会随着容器的结束而消失,从而实现数据持久化。

3.4 应用案例

  在服务治理组件中,服务发现组件是一个最常用的组件之一,Consul是一个流行的服务发现开源项目,Consul推荐我们使用配置文件的方式注册服务信息。因此,我们常常会将填写好服务注册配置文件放在宿主机的一个文件目录下将其挂载到Consul的容器指定目录下,如下所示:

docker run -d -p 8500:8500 --restart=always \
-v /XiLife/consul/data/server1:/consul/data -v /XiLife/consul/conf/server1:/consul/config \
-e CONSUL_BIND_INTERFACE='eth0' --privileged=true \
--name=consul_server_1 consul:1.4.4 agent -server -bootstrap-expect=3 -ui -node=consul_server_1 -client='0.0.0.0' \
-data-dir /consul/data -config-dir /consul/config -datacenter=xdp_dc;

  可以看到,我们通过Bind Mounts的方式将宿主机上的/XiLife/consul/data/server1目录挂载到了容器的/consul/data目录下,还将/XiLife/consul/conf/server1目录挂载到了容器的/consul/config目录下,而容器下的两个目录/consul/data和/consul/config则是我们指定的存放agent数据和配置文件的地方。因此,宿主机上的配置文件的变化会及时反映到容器中,比如我们在宿主机上的目录下更新了配置文件,那么只需要reload一下Consul的容器实例即可:

docker exec consul-server consul reload

  *.这里的consul-server是容器的名字,consul reload是重新加载的命令(非restart)。

四、小结

  本文探索了Docker的数据卷及挂载数据到容器的两种主要方式Volumes和Bind Mounts,并介绍基本的使用方式和步骤,通过数据卷我们可以实现Docker的数据持久化,在实际应用中比较广泛。

参考资料

(1)李振良,《 Docker Volume详解
(2)CloudMan,《 每天5分钟玩转Docker容器技术
(3)阿龙,《 Docker存储卷详解

Guess you like

Origin www.cnblogs.com/edisonchou/p/docker_volumes_introduction.html