Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

2. Docker uses templates to create images, container management, warehouse management, and data management

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

1. Docker uses templates to create images

First download a template

Http://download.openvz.org/template/precreated/

/ / the download speed is not fast. I downloaded a centos6 template centos-6-x86-minimal.tar.gz.

[root@fuxi01 ~] # wget http://download.openvz.org/template/precreated/centos-6-x86-minimal.tar.gz

The command to import the image is:

# cat centos-6-x86-minimal.tar.gz | docker import-centos6sha256:cdce38ce7fb223b243043be905be88f24635036cf850ceae013007f60a2dda51# docker images / / View the imported image # docker run-itd centos6 bash79ce4ee106cb84aadbc411489dafadd37d06a92e81b1682199b9e573c224ea6d [root@fuxi01 ~] # docker exec-it 79ce4ee1 bash [root@79ce4ee106cb /] # cat / etc/issue / / View its version CentOS release 6.8 (Final) Kernel\ r on an\ m [root@79ce4ee106cb /] # uname-aLinux 79ce4ee106cb 3.10.0-693.el7.x86_64 # 1 SMP Tue Aug 22 21:09 27 UTC 2017 x86 "64 GNU/Linux [root@79ce4ee106cb /] # exit [root@fuxi01 ~] # uname-aLinux fuxi01 3.10.0-693.el7.x86_64 # 1 SMP Tue Aug 22 21:09:27 UTC 2017 x86" 64 GNU/Linux

Thus, the kernel of the docker container and the kernel of the Linux machine are the same 3.10.0 kernel, so the docker container is based on the Linux kernel.

Export the existing image as a file:

# docker save-o centos7_with_nettool.tar centos_with_net

/ / save-o followed by the file name, followed by the image name. Save and load are a couple.

[root@fuxi01 ~] # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES79ce4ee106cb centos6 "bash" 4 days ago Up 4 days kind_pasteurb15b83c7c7a2 centos_with_net "/ bin/bash" 5 days ago Up 5 days infallible_lalande7ae3b5eb6e41 ubuntu "/ bin/bash" 12 days ago Up 12 days admiring_diffie43aae89a76ae centos "/ bin/bash" 13 days ago Up 13 days Serene_ days ago 512MBcentos_with_net latest 2803335f23a6 [root @ fuxi01 ~] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos6 latest cdce38ce7fb2 4 days ago 512MBcentos_with_net latest 2803335f23a6 5 days ago 261MBubuntu latest 549b9b86cb8d 3 weeks ago 64.2MBcentos Latest 0f3e07c0138f 3 months ago 220MByw_centos latest 0f3e07c0138f 3 months ago 220MB [root@fuxi01 ~] # docker rm-f b15b83c7c7a2b15b83c7c7a2 [root@fuxi01 ~] # docker rmi 2803335f23a6Untagged: centos_with_net:latestDeleted: sha256:2803335f23a68731a68ffbc860d6e72c15feb2e922cc8aefb1e013fd174b604eDeleted: sha256:c7d34cfde22ae64c7556234eed73668e4a7b4b2803e51bde4cbbdbd25bb5a2cb

You can also use this file to restore the local image:

# docker load-input centos7_with_nettool.tar or # docker load

< centos7_with_nettool.tar# docker push image_name docker push //可以把自己的镜像传到dockerhub官方网站上去,但前提是需要先注册一个用户,后续如果有需求再研究吧。 二、容器管理 # docker create -it centos6 bash //这样可以创建一个容器,但该容器并没有启动,docker create就像创建虚拟机一样。和docker run基本一样的用法。创建后,需要用docker ps -a才能看到,状态是Created。 # docker start container_id //启动容器后,可以使用 docker ps 查看到,有start 就有stop,和restart。 之前我们使用的docker run 相当于先create再start # docker run -it centos bash 这样进入了一个虚拟终端里面,我们可以运行一些命令,使用命令exit或者ctrl d 退出该bash,当退出后这个容器也会停止。执行docker ps -a可以看到它的状态是Exited的状态了。 # docker run -d 可以让容器在后台运行 比如:docker run -d centos bash -c "while :; do echo "123"; sleep 2; done" # docker run --name centos6_1 -itd centos6 bash // --name 给容器自定义名字,如果不定义,docker ps时最右侧的NAMES就是随机字符串。# docker exec -it centos6_1 bash //进入容器就可以直接写定义的名字了,不用去写它的ID了。# docker run --rm -it centos bash -c "sleep 30" //--rm 可以让容器退出后直接删除,在这里命令执行完容器就会退出。-c,在容器里执行命令。 docker logs 可以获取到容器的运行历史信息,用法如下: docker logs container_id [root@fuxi01 ~]# docker run -itd centos bash -c "echo 12345"7c2e09481628fa13df86bdc858d2641ea094a88ee4fbdc0feeda6f3efa059048[root@fuxi01 ~]# docker logs 7c2e0912345 docker attach 可以进入一个后台运行的容器,比如 # docker attach container_id //但是attach命令不好用,比如我们想要退出终端,就得exit了,这样容器也就退出stop了,还有一种方法:# docker exec -it container_id bash //可以临时打开一个虚拟终端,并且exit后,容器依然运行着。# docker rm container_id //container_id是docker ps -a的时候查看到的,这样就可以把container删除,如果是运行的容器,需要用rm -f。# docker export container_id >

File.tar / / export container, can be migrated to other machines, need to import, import is docker load-- input. # cat file.tar | docker import-aming_test / / this will generate an image of aming_test

III. Warehouse management

# docker pull registry

/ / download registry image. Registy is an official image provided by docker, which we can use to create a local docker private repository. When pulling registry, it will be slow without an accelerator. Accelerator: / etc/docker/daemon.json

All containers will stop after restarting docker. If you want to start all containers automatically in batch

Execute: systemctl restart docker & & docker start $(docker ps-a-Q)

Start all container commands in docker

# docker start $(docker ps-a | awk'{print $1}'| tail-n + 2)

Command to close all containers in docker

# docker stop $(docker ps-a | awk'{print $1}'| tail-n + 2)

# docker run-d-p 5000 registry

/ / launch the container with registry image, and-p maps the container port to the host: the listening port of the host on the left and the listening port of the container on the right. You can also write other ports and customize them.

/ / to access port 80 in the container, you need to do a port mapping. For example, the ip in the centos container is 172.17.0.2, and the IP is internal, but the host's 192.168.255.128 is external, and port 80 from the host to the container is ping accessible.

# curl 127.0.0.1:5000/v2/_catalog / / you can visit it {"repositories": []}

There is no content, we need to send the image to the warehouse, to the warehouse to see.

Next, upload one of the images to the private warehouse.

# docker tag centos 192.168.255.128:5000/centos / / Mark tag. First, the IP of the host must be ip:port# docker push 192.168.255.128:5000/centos / / with a private repository to push the marked image to the private repository.

It will not be successful at this time, Get https://192.168.255.128:5000/v2/: http: server gave HTTP response to HTTPS client reported an error, and https is used here.

Solution:

Change the profile:

# vi / etc/docker/daemon.json / / is changed to this item, and the previous accelerator needs to be deleted, otherwise push will not succeed. Don't use HTTPS, use HTTP. {"insecure-registries": ["192.168.255.128docker start id 5000"]} # systemctl restart docker # docker ps-a / / check that the container has been closed, and you also need to start # docker start id / / the id here is the registry container id

Push again

# docker push 192.168.255.128:5000/centos# curl 127.0.0.1:5000/v2/_catalog / / you can view the pushed image {"repositories": ["centos"]} / / the centos here, which is the string on the last side of the push. # docker tag ubuntu 192.168.255.128:5000/ubuntu / / tag and push, and you can find it in the private warehouse. # docker push 192.168.255.128:5000/ubuntu# curl 127.0.0.1:5000/v2/_catalog {"repositories": ["centos", "ubuntu"]} the new machine pulls the image from the private warehouse: first install docker, then define the warehouse address vi / etc/docker/daemon.json, start docker, and then pull is fine: # docker pull 192.168.255.128:5000/ubuntu// plus IP:port is downloaded from the private warehouse If you don't add it, it will be downloaded officially.

IV. Data management

The container is started by an image, and the new data generated in the container will be deleted when the container is stopped or deleted, which means that the data is risky. So come up with a way to mount the host directory to the container, so that even if the container is stopped or deleted, the data is still in the host directory.

1. Mount the local directory to the container

# docker run-tid-v / data/:/data centos bash266bc8cd6fe8fe57c919acddbd958a756bbbdeeb1d6cecf9a49938ee0cd746ab [root@fuxi01 ~] # ls / datachange.log ftp gitroot mariadb mysql tomcat-instance user_passwd wwwroot [root@fuxi01 ~] # docker exec-it 266bc8c bash [root@266bc8cd6fe8 /] # ls / datachange.log ftp gitroot mariadb mysql tomcat-instance user_passwd wwwroot [root@266bc8cd6fe8 /] # / / in this way, the data directory in the container and the data directory of the host are synchronized, and the changes are synchronized.

/ /-v is used to specify the mount directory. / data/ is the local directory of the host, and / data/ is the directory in the container, which is automatically created in the container.

two。 Mount the data volume (--volumes-from)

In fact, when we mount a directory, we can use-- name to specify the container name. If it is not specified, it will be randomly defined. For example, if it is not specified above, it generates a name called heuristic_almeida, which can be seen in the rightmost column using the command docker ps.

# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES266bc8cd6fe8 centos "bash" 23 hours ago Up 23 hours heuristic_almeida# docker run-itd-volumes-from heuristic_almeida centos6 bash53ad0ecc0ea797915dd62ce03e886c05f86171136404b35a1f8049542cf57358

In this way, we use the centos6 image to create a new container and use the heuristic_almeida container as a data volume. If you enter the centos6 container, you can ls / data to see that the content is the same as that of centos. What the data directory of the centos container is like, it is what it is here, and it is associated.

3. Define a 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 mount the data volume directly.

First set up a data volume container

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

/ / Note that the / data/ here is the container's / data directory, not the local / data/ directory. You don't need to map the host's directory. Share this directory as a public directory.

Then let other containers mount the data volume

# docker run-itd-- volumes-from testvol centos6 bash

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report