Docker (5) --- data volume

Insert picture description here

Previous: Docker (4) —Mirror

1. How to ensure data persistence?

  Here we should know that the docker technology is to use the downloaded image file to generate a running instance-container, which means that the image file is equivalent to a template, such as the image file of mysql. Download the same mysql image file. How to save the data generated during my operation? What if I want to use the data generated during the first run during the second run?

  In Docker, data volumes provide data persistence. The data volume appears as a directory or file in one or more containers, and is mounted to the container by docker, but it is not part of the joint file system, so when docker closes or deletes the container, it will not affect The data in the data volume. Data volumes can be used to store data, and can also be used to share data between containers.

2. Dockerfile reserved word instruction

instruction description
FROM The base image, based on which image the new image is based on
MAINTAINER Mirror maintainer's name and email address
RUN Commands to run when the container is built
EXPOSE Port exposed by the current container
WORKDIR Specify the directory where the terminal logs in to work by default after creating the container, a foothold
ENV Used to set environment variables during image building
ADD Copy the files in the host directory into the image and the ADD command will automatically process the URL and decompress the tar archive
COPY Similar to ADD, copy files and directories to the mirror. Copy the files / directories from the <source path> in the build context directory to the location of the <target path> in the mirror of the new layer {COPY src dest}
VOLUME Container data volume for data storage and persistence
CMD Specify a command to run when the container starts. There can be multiple CMD instructions in the Dockerfile, but only the last one takes effect, and the CMD will be replaced by the parameters after docker run.
ENTRYPOINT Specify a command to run when the container starts. The purpose of ENTRYPOINT is the same as CMD, which is to specify the container startup program and parameters.
ONBUILD When building an inherited Dockerfile, run the command and the parent image's onbuild is triggered after the child image is inherited.

3. Features of data volume?

  • Data volumes can be shared or reused between containers
  • Changes in the data volume will not be included in the update of the container
  • Changes in the data volume can take effect directly
  • The life cycle of the data volume continues until no container uses it

3. To achieve data sharing between the host machine and the container?

  Two concepts need to be clarified here:
1. Host machine: The
  host machine refers VMWare-----》CentOsto the Linux system that we enter through the way is our host machine.
2. The container
  is the running instance generated by the image file pulled on the host machine ~

Requirements:
  I now have a centos image, I hope that when the centos image is running, itscontainerdatadirectory and the host'smydatatwo directories can share data;

Command:
  docker run -it -v 宿主机中的共享目录(mydata)路径:容器中的共享目录(containerdata)的路径 Container name

Example: For
  example, mydata is in the root directory of the host machine (/mydata), and containerdata is in the root directory of the container (/containerdata)

The following figure is the root directory of the host machine, and there is no mydatafolder now
Insert picture description here
. The following figure is the root directory of the container centos, and there is no containerdatafolder now
Insert picture description here
. First, bind the directory:

docker run -it -v /mydata:/containerdata centos

2. View the binding relationship between the host and the container

docker inspect 容器ID

Insert picture description here
3. Check the host vessel and whether there were mydataand containerdatafolders

Host:
Insert picture description here
Container:
Insert picture description here
Now mydatacreate a document in the directory:

[root@bogon mydata]# touch file.txt
[root@bogon mydata]# ll
总用量 0
-rw-r--r--. 1 root root 0 3月  22 14:57 file.txt
[root@bogon mydata]# 

Now that the mydatadirectory and the containerdatadirectory in the centos container share data, there containerdatashould now be a file.txtdocument under the directory . View containerdatacatalog:

[root@7453e78d6615 /]# cd containerdata/
[root@7453e78d6615 containerdata]# ls -l
total 0
-rw-r--r--. 1 root root 0 Mar 22 06:57 file.txt
[root@7453e78d6615 containerdata]# 

Found that containerdatathere is a file.txtdocument under the directory , now edit the document through the container:
Insert picture description here
enter a paragraph in the document, according to the principle of data sharing, there should also be a sentence mydatain the file.txtdocument under the host's directory :
Insert picture description here
check the sink again containerdataUnder the host file.txt:
Insert picture description here
Here you have achieved data sharing between the host and the container ~

note:

  After the above binding operation, even if our container is closed and running, if we mydataperform the operation in the directory of the host machine, it will still be synchronized to centosthe containerdatadirectory in the image bound to it :

Examples:

  Close centos and exit: `

Insert picture description here
View the image file running at this time:
Insert picture description here
confirm that the centos image file above has been quit running ~
At this time mydata, create a file001.txtfile in the directory of the host machine and add content: this content is edited in the host
Insert picture description here
run the centos image file at this time, go to the containerdatadirectory to view:
view on CentOS image ID of the second close: docker ps -l
Insert picture description here
Restart the centos image file that was last closed:docker start 镜像ID

Insert picture description here

Enter the running centos container: docker attach image ID to
Insert picture description here
see containerdataif there are new file001.txtdocuments in the directory :

[root@e2fde7599c8b /]# cd containerdata/
[root@e2fde7599c8b containerdata]# ls -l
total 8
-rw-r--r--. 1 root root 38 Mar 22 07:22 file.txt
-rw-r--r--. 1 root root 35 Mar 22 07:13 file001.txt
[root@e2fde7599c8b containerdata]# cat file001.txt 

this content is edited in the host

[root@e2fde7599c8b containerdata]# 

It is found that the newly created file001.txtdocument and data on the host machine are synchronized to the centoscorresponding binding position of the container;

4. Add permissions when sharing data between host and container

Directory binding command for data sharing between host and container:

docker run -it -v /宿主机共享文件的绝对路径:/容器共享文件的绝对路径 :ro 镜像名

Note: ro is read only (read only permission)

After binding the shared data directory through the above command, we can write data (add, delete, modify) from the host machine, and the container can only read data

Published 117 original articles · Like 57 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/qq_43655835/article/details/104944691