Container management, warehouse management data management, data volume backup and recovery

Container management

Create a container:

Create a new container, but it is not running

[root@localhost src]# docker create -it centos7 bash

776422814ba2cb2ca6dfc875989100dd388ba78c4d8cb6484aad0a4f67664710

View the container in the current startup state

[root@localhost src]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

90aae362613c centos_with_net "/bin/bash" 10 minutes ago Up 10 minutes gifted_hypatia

14ff3a80b8db centos "/bin/bash" 15 minutes ago Up 15 minutes romantic_chandrasekhar

View all containers, including those that are not started

docker ps -a

[root@localhost src]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

776422814ba2 centos7 "bash" 2 minutes ago Created //** Current Status** sharp_margulis

90aae362613c centos_with_net "/bin/bash" 10 minutes ago Up 10 minutes gifted_hypatia

14ff3a80b8db centos "/bin/bash" 16 minutes ago Up 16 minutes romantic_chandrasekhar

Start (restart, close) the container:

docker start/restart/stop CONTAINER ID

[root@localhost src]# docker start 77642281

77642281

[root@localhost src]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

776422814ba2 centos7 "bash" 5 minutes ago Up 13 seconds //sharp_margulis has been started

90aae362613c centos_with_net "/bin/bash" 14 minutes ago Up 14 minutes gifted_hypatia

14ff3a80b8db centos "/bin/bash" 19 minutes ago Up 19 minutes romantic_chandrasekhar

The docker run we used before is equivalent to create and then start

docker run -it centos bash

After entering a virtual terminal, we can run some commands, use the command exit or ctrl d to exit the bash, and the container will stop after exiting.

[root@localhost src]# docker run -it centos bash

[root@71c249e74843 /]#

[root@71c249e74843 /]# exit

Exit //Exit will not be running after exiting

[root@localhost src]#

You can let the container run in the background

docker run -d

比如:docker run -d centos bash -c "while :; do echo "123"; sleep 2; done"

[root@localhost src]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

4ced877f7363 centos "bash -c 'while :; d…" 3 minutes ago Up 3 minutes gallant_villani

Start with custom container name:

[root@localhost src]# docker run --name web -itd centos bash

cdf0afb174a59f1f8c32a521f4d2276279e5ce2e642a6352d746555af9d26b95

--name give the container a custom name

[root@localhost src]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

cdf0afb174a5 centos "bash" 24 seconds ago Up 24 seconds web

Enter the corresponding container interface through the container name

[root@localhost src]# docker exec -it web bash

[root@cdf0afb174a5 /]#

Delete after the container exits:

--rm Let the container exit and delete the container after executing the command; -c specifies the command to be executed

[root@localhost src]# docker run --rm -it centos bash -c "sleep 30"

View the historical information of the container operation:

docker logs CONTAINER ID

[root@localhost src]# docker run -itd centos bash -c "echo 123"

8918a6a6ad085e0dc16a108db87c717ff0009d93fd4572f3564e94caabaff357

[root@localhost src]# docker logs 8918a6

123

Enter the container running in the background :

docker attach CONTAINER ID

But the attach command is not easy to use. For example, if we want to exit the terminal, we have to exit so that the container will exit. There is another way

docker exec -it CONTAINER ID bash

You can temporarily open a virtual terminal, and after exit, the container is still running

Delete the container:

docker rm CONTAINER ID

The CONTAINER ID is checked during ps, so that the container can be deleted. If it is a running container, you can add -f

[root@localhost src]# docker rm cdf0afb

Error response from daemon: You cannot remove a running container cdf0afb174a59f1f8c32a521f4d2276279e5ce2e642a6352d746555af9d26b95. Stop the container before attempting removal or force remove

[root@localhost src]# docker rm -f cdf0afb

cdf0afb

Export container:

docker export container_id > file.tar

Export the container as a file, which can be migrated to other machines and needs to be imported

[root@localhost src]# docker export 776422 > aaa.tar

[root@localhost src]# ls

aaa.tar

Import the container:

cat aaa.tar | docker import - jin_test

This will generate a mirror of jin_test

[root@localhost src]# cat aaa.tar |docker import - jin_test

sha256: 17665006fbe6ba985a669350c261e7c952ae981503aca1de443b1df0711ed8b4

[root@localhost src]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

jin_test latest 17665006fbe6 About a minute ago 435MB

Warehouse management

For companies, it is not appropriate to push their own images to the docker public warehouse, but we can build our own local docker private warehouse.

Download the registry mirror:

docker pull registry

The registry image is an official image provided by docker, which can be used to create a local private warehouse

[root@localhost src]# docker pull registry

Using default tag: latest

latest: Pulling from library/registry

0a6724ff3fcd: Pull complete

d550a247d74f: Pull complete

1a938458ca36: Pull complete

acd758c36fc9: Pull complete

9af6d68b484a: Pull complete

Digest: sha256:d5459fcb27aecc752520df4b492b08358a1912fcdfa454f7d2101d4b09991daa

Status: Downloaded newer image for registry:latest

docker.io/library/registry:latest

Start the registry image as a container:

docker run -d -p 5000:5000 registry

-p specifies the mapping port to the host, the left is the host listening port, and the right is the container listening port

[root@localhost src]# docker run -d -p 5000:5000 registry

6bce9295a140fc1218794d3a970f1548601ec9fc07309919416ede38cfe0198d

[root@localhost src]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

6bce9295a140 registry "/entrypoint.sh /etc…" 37 seconds ago Up 36 seconds 0.0.0.0:5000->5000/tcp quizzical_lederberg

View private warehouse:

[root@localhost src]# curl 127.0.0.1:5000/v2/_catalog

{"repositories":[]}

Upload the image to the private warehouse:

Label

docker tag centos7 192.168.111.137:5000/centos7

Mark the tag, it must have the ip:port of the private warehouse

[root@localhost src]# docker tag centos7 192.168.111.136:5000/centos7

[root@localhost src]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

jin_test latest 17665006fbe6 20 hours ago 435MB

192.168.111.136:5000/centos7 latest 3f34ff44ea96 20 hours ago 435MB

centos7 latest 3f34ff44ea96 20 hours ago 435MB

centos_with_net latest ad5399f1e54b 20 hours ago 242MB

registry latest 678dfa38fcfa 5 days ago 26.2MB

centos latest 300e315adb2f 2 weeks ago 209MB

jin_centos latest 300e315adb2f 2 weeks ago 209MB

ubuntu latest f643c72bc252 3 weeks ago 72.9MB

Modify the configuration file

vim /etc/docker/daemon.json

Change to the following content, you cannot add it, you need to delete the previous accelerator url

{

"insecure-registries": ["192.168.111.137:5000"]

}

Need to restart after modifying the configuration file

systemctl restart docker

The container has been closed and the registry container needs to be restarted

[root@localhost src]# docker start 6bce92

6bce92

[root@localhost src]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

6bce9295a140 registry "/entrypoint.sh /etc…" 7 minutes ago Up 2 seconds 0.0.0.0:5000->5000/tcp quizzical_lederberg

Upload mirror:

[root@localhost src]# docker push 192.168.111.137:5000/centos7

Using default tag: latest

The push refers to repository [192.168.111.137:5000/centos7]

788edba9eaa8: Pushed

latest: digest: sha256:d1fca36f123eb34819f65b4b52196cd4aa1304c4012dcd679a2bf1054a9d6e55 size: 529

View private warehouse:

[root@localhost src]# curl 127.0.0.1:5000/v2/_catalog

{"repositories":["centos7"]}

Upload a mirror again:

[root@localhost src]# docker tag ubuntu 192.168.111.137:5000/ubuntu

[root@localhost src]# docker push 192.168.111.137:5000/ubuntu

Using default tag: latest

The push refers to repository [192.168.111.137:5000/ubuntu]

f6253634dc78: Pushed

9069f84dbbe9: Pushed

bacd3af13903: Pushed

latest: digest: sha256:4e4bc990609ed865e07afc8427c30ffdddca5153fd4e82c20d8f0783a291e241 size: 943

[root@localhost src]# curl 127.0.0.1:5000/v2/_catalog

{"repositories":["centos7","ubuntu"]}

Data management

The container is started by a mirror. If new data is generated in the container, the data will also be deleted when the container is closed or deleted. There is a certain risk to the data.

In order to prevent the newly generated data in the container from being deleted, we can mount the local directory to the container.

Mount the local directory to the container:

-v specifies the mount directory,: the front /data/ is the local directory of the host, and the back /data/ is the directory in the container, which will be automatically created in the container

[root@localhost src]# touch /data/aaa

[root@localhost src]# docker run -tid -v /data/:/data centos bash

d057e4f65c55a6709a0ce033ad05d5197d448144f2bf80cc4a3d9d32255d33bf

[root@localhost src]# docker exec -it d057e4f bash

[root@d057e4f65c55 /]# ls /data/

aaa

[root@d057e4f65c55 /]# mkdir /data/abc

[root@d057e4f65c55 /]# exit

exit

[root@localhost src]# ls /data/

aaa abc

Mount the data volume:

In fact, when we mount the directory, we can specify the container name. If we don't specify it, we will define it randomly. For example, if we did not specify it above, it will generate a name heuristic_kapitsa, this name can be used to see the rightmost column using the command docker ps

[root@localhost src]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

d057e4f65c55 centos "bash" 3 minutes ago Up 3 minutes heuristic_kapitsa

638fb8d2921d centos "bash" 5 minutes ago Up 5 minutes upbeat_golick

6bce9295a140 registry "/entrypoint.sh /etc…" 27 minutes ago Up 13 minutes 0.0.0.0:5000->5000/tcp quizzical_lederberg

Open a new container:

Mount the data volume, use the above container as the data volume container to open a new container

[root@localhost src]# docker run -itd --volumes-from heuristic_kapitsa centos bash

a86e8b942f0f44a265588d5a6d7ecd6ffd3b63e2ad5962376ede2940b6014b06

[root@localhost src]# docker exec -it a86e8b bash

[root@a86e8b942f0f /]# ls -l /data/

total 0

-rw-r--r--. 1 root root 0 Dec 23 11:36 aaa

drwxr-xr-x. 2 root root 6 Dec 23 11:37 abc

You can see above that the directory of the new container is the same as the directory of the data volume container

Define the data volume container:

Sometimes, we need multiple containers to share data with each other, similar to NFS in Linux, so we can build a special data volume container, and then other containers directly mount the data volume.

First create a data volume container:

docker run -itd -v / data / --name testvol centos bash

Note that /data/ is the /data directory of the container, not the /data directory of the host; testvol is the name of the custom data volume container

Then let other containers mount the data volume:

[root@localhost src]# docker run -itd --volumes-from testvol centos7 bash

c64b95f7dc9ff6e5dea6309112eed91931f929d8d652bc0154818e83a7e8f79f

Here is the container directory of the centos image where the centos image is mounted

In addition, if the data volume that each machine wants to mount is different, you can make a soft link, make a soft link to the directory you want to mount, and link to the same specified directory.

Data volume backup and recovery

Data volume backup:

Note: First, we need to use the testvol data volume to open a new container, and we also need to mount the local /vol_data_backup/ directory to the container's /backup, so that we can create new files in the /backup directory in the container See it directly in the /data/backup/ directory. Then pack the files under the /data/ directory into a data.tar file and put them under the /backup directory

Create a local mount directory: mkdir /data/backup

docker run --volumes-from testvol -v /data/backup/:/backup centos tar cvf /backup/data.tar /data

testvol is the name of the data volume container; -v specifies the mount directory; centos is the new container image; tar is packaged, and /data is the directory to be backed up

Data volume recovery:

The process is opposite to the above, first create a data volume container, then create a new container and mount the data volume container, and then put the tar package in the mount directory to unpack.

Create a new data volume container:

[root@localhost backup]# docker run -itd -v /data/ --name testvol2 centos bash

f67099cf8ec77f22a6172a2179bb8a2cf56ddbed80046d64fbc848c970ac51ea

Mount the data volume to create a new container, and unpack:

[root@localhost backup]# docker run --volumes-from testvol2 -v /data/backup:/backup centos tar xvf /backup/data.tar

Guess you like

Origin blog.51cto.com/11451960/2640817