Docker data volume container

  • premise

Docker container data generated, if not docker commit to generate a new image, as a mirror image of the data preserved by part, then when the container removed, naturally there will be no data.

In order to save the data in the docker, we use the volume. The purpose is to make the data in the container eat persistence.

  • Introduction of data volumes

Volume is the directory or file, present in one or more containers, a container to mount the docker, but not combined file system, it is possible to bypass Union File System for continuously storing some characteristic or shared data: volume It is designed to persistent data, completely independent of the life cycle of the container, so Docker does not delete its data volume mounted when the container is deleted

Features:

  1. Data volume may be reused or shared data between the vessel
  2. Volume change can take effect directly
  3. Data on the volume changes are not included in the update mirrored
  4. The life cycle of the data volume continues to use it until the container is not
  • Add data volumes within the container

Method One: Use the -v command

# 双向读写权限
docker run -it -v /宿主机目录:/容器内目录 镜像名称
# 宿主机可读写,容器只读
docker run -it -v /宿主机目录:/容器内目录:ro 镜像名称

Effect:

First created in the host host_sharedirectory

# 执行命令 docker run -it -v /宿主机目录:/容器内目录 镜像名称
docker run -it -v /host_share:/container_share centos

Into the interior of the container and found container_share folders created successfully

Press Ctrl + P + Q temporary withdrawal of the container (the container is not closed)

Check the data volume is mounted successfully
# 查看容器内部信息
docker inspect 容器ID

Test whether data sharing

After the test container is stopped, the host modified data is synchronized

  • Option two: Add DockerFile

For portable and sharing considerations, by -v Host Directory: container directories This method can not be directly implemented in the Dockerfile.

Since the host directory is dependent on the particular host, and can not guarantee the existence of such a specific directory on all host. The following describes how to add a data volume DockerFile

Under the new host mydocker root directory and enter, create a new mydockerfile File, Edit
FROM centos
VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
CMD echo "finished,--------success!"
CMD /bin/bash

VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]You can add more data volumes in the vessel by this method, the host shared address corresponding default settings

Build command using the current mirror generating mydockerfile
# docker build -f DockerFIle绝对路径 -t 命名空间/镜像名称 .
# 注意 . 不要忘了
docker build -f /mydocker/mydockerfile -t moti/centos .

Run into the container and found two volume catalog data has been successfully created

Ctrl + P + Q temporary withdrawal (the container does not stop), the use of docker inspect containers ID information about the data volume See

Red box is checked in the corresponding path of the host

Test whether to implement data sharing

  • Data volume container

命名的容器挂载数据卷,其它容器通过挂载这个(父容器)实现数据共享,挂载数据卷的容器,称之为数据卷容器

容器之间传递数据共享

先创建一个容器c1,在dataVolumeContainer1添加共享数据c1.txt

创建容器c2继承自c1,查看 dataVolumeContainer1 里面的文件,并在 dataVolumeContainer1 中添加c2.txt

创建容器c3继承自c1,查看 dataVolumeContainer1 里面的文件,并在 dataVolumeContainer1 中添加c3.txt

删除c1容器,查看c2和c3中的文件是否有变化(原来有c1.txt c2.txt c3.txt共三个文件)

可以发现,c1删除,对c2,c3是不影响的,那么c2与c3会不会共享文件?

可以看到,尽管c1删除了但是c2和c3仍然是数据共享的,那么删除c2,查看c3的数据有没有什么变化

结论:容器之间配置信息的传递,数据卷的生命周期会一直持续到没有容器使用它为止

Guess you like

Origin www.cnblogs.com/cnmoti/p/12088972.html