Docker container data management experiment (data volume)

create mirror

docker run -tid --name container name -p   host port: container port    mirroring command (consistent with -v, or multiple -p)

(The host port comes first, access a certain port [service] of the host, and map to the port in the container through iptables rules)

-p: port mapping (map the service port in the container to the host, because the ip address of the accessed host is used for external access; the container is a process created based on the host kernel)

A mutual communication and cooperation between services in the container is realized through the virtual bridge (like switch) of docker0

Parametric analysis

 -t When creating a container, -t creates a pseudo-terminal ~ (very important)

-i is standard input (very important)

-v is the mapping between /var/lib/registry in the container and the host's myregistry

(The mapping can map multiple; multiple -v, and the files mapped from the host can also be seen in the container)

-d: run in the background

When run runs the container, the following /bin/bash command interpreter needs to interpret the command as the standard input of the container, and then execute it!

When deleting a container, docker rm short id/container name (multiple can be deleted at the same time; separated by spaces)

Entering the container is attach; when exit exits, the container also ends correspondingly (you need to start the container before entering the container)

Docker start short ID/name (you can view the short id through docker ps -a)

Do not enter the container but execute the command: exec (Lenovo find's execution command -exec)

Container Data Management (Experimental)

 Map the directory or file on the docker host to the container, which is equivalent to the mount operation in the linux system

1) When creating a container, specify that the container uses the directory as the data volume

docker  run  -tid  --name  c1  -v  /root/CS:/root/test  centos  /bin/bash

Check the existence of the test file under the c1 container/root 

docker exec c1 ls /root

The host touches an f1 file (touch CS/f1)

Check that the f1 file exists 

docker exec c1 ls /root/test

It is equivalent to mounting a directory of the host machine in the container, and the operation of the directory corresponding to the host machine (it can be put into programs or files, and can also be used in the container)

2) When creating a container, specify that the container uses files as data volumes

docker run -tid --name 容器名 -v 宿主机文件:容器内部文件 镜像名 命令

First, the host creates a file

echo  aaaaaaaaa > /root/file1

Then create a container and mount the file

docker run -tid  --name c2  -v   /root/file1:/root/file2  centos /bin/bash

Remarks: The files on the host machine need to exist in advance

Check the existence of the file2 file in the container

docker exec  c2  ls  /root

View the content appended to the container c2 file

docker   exec  c2   cat  /root/file2

The host then appends bbbbbbbbbbb content

echo  bbbbbbbbbbb >> file1

View additional content at this time

docker exec  c2  cat  /root/file2

data volume


A data volume is a special directory available to containers that bypasses the file system and provides many useful features

Data volumes can be shared and reused between containers.
Modifications to data volumes take effect immediately.
Updates to data volumes will not affect mirroring.
Volumes persist until no more containers are used.


The use of data volumes is similar to the mount operation on directories or files under Linux

Data volume container (experimental)

Build a container, and then continue to create subsequent containers based on the created container. When creating c2 and c3, let him not find the host for mapping, but let him find the container c1 directly.

Dedicate a container as a data volume for other containers

Create a data volume container used by other containers

docker run -tid --name container name -v host directory: container internal directory image name command

Create a container and specify the data volume container to use

docker run -tid --name container name --volumes-from data volume container name image name name

The example is as follows

docker run -tid --name c1 -v /root/CS:root/test centos /bin/bash

docker run -tid --name c2 --volumes-from c1 centos /bin/bash

docker run -tid --name c3 --volumes-from c1 centos /bin/bash

Create data volume container c1

docker run -tid --name c1 -v /root/CS:/root/test centos /bin/bash

(If it does not exist in the /root/test host, it will create it by itself.)

View created containers (docker ps -a)

ls Check that there are CS tests in the host machine in the current directory (ls)

Check that there is an additional test in the c1 container/root directory (docker exec c1 ls /root)

Next, when we create c2 and c3, we will not let him find the host for mapping, let him directly find the container c1

docker run -tid --name c2 --volumes-from c1 centos /bin/bash

Check if there is a test under root under docker exec c2 (docker exec c2 ls /root)

Create a c3 container again

docker run -tid --name c3 --volumes-from c1 centos /bin/bash

View c3 (docker exec c3 ls /root)

Touch a file on the host machine (touch CS/1.txt)

View container c1 c2 c3

docker exec c1 ls /root/test

docker exec c2 ls /root/test

docker exec c3 ls /root/test

All of the above have 1.txt files

Guess you like

Origin blog.csdn.net/m0_72264240/article/details/130426802