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-----》CentOs
to 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, itscontainerdata
directory and the host'smydata
two 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 mydata
folder now
. The following figure is the root directory of the container centos, and there is no containerdata
folder now
. 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
3. Check the host vessel and whether there were mydata
and containerdata
folders
Host:
Container:
Now mydata
create 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 mydata
directory and the containerdata
directory in the centos container share data, there containerdata
should now be a file.txt
document under the directory . View containerdata
catalog:
[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 containerdata
there is a file.txt
document under the directory , now edit the document through the container:
enter a paragraph in the document, according to the principle of data sharing, there should also be a sentence mydata
in the file.txt
document under the host's directory :
check the sink again containerdata
Under the host file.txt
:
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 mydata
perform the operation in the directory of the host machine, it will still be synchronized to centos
the containerdata
directory in the image bound to it :
Examples:
Close centos and exit: `
View the image file running at this time:
confirm that the centos image file above has been quit running ~
At this time mydata
, create a file001.txt
file in the directory of the host machine and add content: this content is edited in the host
run the centos image file at this time, go to the containerdata
directory to view:
view on CentOS image ID of the second close: docker ps -l
Restart the centos image file that was last closed:docker start 镜像ID
Enter the running centos container: docker attach image ID to
see containerdata
if there are new file001.txt
documents 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.txt
document and data on the host machine are synchronized to the centos
corresponding 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