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

Basic Management of Docker Container

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the knowledge of "basic Management of Docker Container". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Docker introduction

It is a lightweight "virtual machine"

Open source tools for running applications in a Linux container

The difference between Docker and virtual machine

Virtual machine is on a physical machine, using virtualization technology, virtual out of multiple operating systems, each operating system is isolated. Docker is an open source application container engine, you still need to install the operating system on your computer, and then install the manager of the Docker container. Virtual machines are virtualized at the hardware level, while Docker is virtualized at the operating system level; virtual machines build operating systems by simulating hardware

Usage scenarios of Docker

Packaged applications simplify deployment

Can be migrated at will without underlying hardware

For example, servers are migrated from Tencent Cloud to Aliyun

Core concepts of Docker

Mirror image

Container

Warehouse

Two ways to install Docker in CentOS

Use curl to get docker's installation script for installation

Use the yum repository to install docker

Docker installation settings mirror source Install docker [root @ localhost ~] # yum install-y\ > yum-utils\ / / set source tool > device-mapper-persistent-data\ / / Mapping tool > lvm2 [root@localhost ~] # yum-config-manager-- add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo / / load Ali cloud image source [root@localhost ~] # yum install docker -ce-y / install docker container [root@localhost ~] # systemctl stop firewalld.service [root@localhost ~] # setenforce 0 [root@localhost ~] # systemctl start docker / / Open docker container [root@localhost ~] # systemctl enable docker / / set boot self-startup [root@localhost ~] # ps aux | grep docker / / check whether the docker process is enabled [root@localhost ~] # docker version / / Check the version [root@localhost ~] # docker search nginx / / search for public images [root@localhost ~] # docker pull nginx / / download images for image acceleration

Log in to Aliyun website-console-Product Service-Container Image Service-Image Accelerator-Select centos

[root@localhost ~] # tee / etc/docker/daemon.json "registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com"]>} > EOF {" registry-mirrors ": [" https://3a8s9zx5.mirror.aliyuncs.com"]}[root@localhost ~] # systemctl daemon-reload / / reload daemon [root@localhost ~] # systemctl restart docker/ / restart the docker service [root@localhost ~] # Docker pull nginx / / download nginx image [root@localhost ~] # docker images / / View the downloaded image information REPOSITORY TAG IMAGE ID CREATE SIZEnginx latest f7bb5701a33c 4 days ago 126MBdocker image basic operation

Container 1: 192.168.80.12

Container 2VR 192.168.80.13

[root@localhost ~] # docker inspect f7bb5701a33c / / View image information [root@localhost ~] # docker tag nginx:latest nginx:web / / add a new tag [root@localhost ~] # docker images / / View Image REPOSITORY TAG IMAGE ID CREATED SIZEnginx latest f7bb5701a33c 4 days ago 126MBnginx Web f7bb5701a33c 4 days ago 126MB// regenerates a mirror The original one will not disappear [root@localhost ~] # docker images | grep web / / View the image labeled web nginx web f7bb5701a33c 4 days ago 126MB [root@localhost ~] # docker rmi nginx:web / / Delete the image Or directly follow the ID number Untagged: nginx:web [root@localhost ~] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest f7bb5701a33c 4 days ago 126MB [root@localhost ~] # cd / opt/ [root@localhost opt] # docker save-o nginx nginx:latest / / check out the image [root@localhost opt] # lscontainerd Nginx rh [root@localhost opt] # scp / opt/nginx root@192.168.80.13:/opt/ remote copy to 13 server to start another virtual machine with docker (192.168.80.13) [root@localhost opt] # docker load

< nginx //载入镜像//或者使用docker load --input 存出文件名[root@localhost opt]# docker images //查看镜像信息REPOSITORY TAG IMAGE ID CREATED SIZEnginx latest f7bb5701a33c 4 days ago 126MB回到原虚拟机(192.168.80.12)[root@localhost opt]# docker tag nginx:latest nginx:web //修改标签[root@localhost opt]# docker login //登录docker(你需要注册)Username: //用户名Password: //密码[root@localhost opt]# docker push nginx:web //上传公有仓库docker容器基本操作[root@localhost opt]# docker create -it nginx:latest /bin/bash //基于镜像创建一个容器 -i让容器的标准输入保持打开,-t让docker分配一个伪终端36fdfb0925ba040c094d585d70a3481bd450c7d39e6636ceeb10b5c1b9743593[root@localhost opt]# docker ps -a //-a列出最近一次启动的容器CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES36fdfb0925ba nginx:latest "/bin/bash" 3 seconds ago Created mystifying_dijkstra[root@localhost opt]# docker start 36fdfb0925ba //开启容器36fdfb0925ba[root@localhost opt]# docker ps -a //查看此时容器状态为开启CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES36fdfb0925ba nginx:latest "/bin/bash" 16 minutes ago Up 6 seconds 80/tcp mystifying_dijkstra操作步骤[root@localhost opt]# docker search centos7 //查看找centos7镜像[root@localhost opt]# docker pull paigeeworld/centos7 //下载镜像[root@localhost opt]# docker images //查看镜像REPOSITORY TAG IMAGE ID CREATED SIZEnginx latest f7bb5701a33c 4 days ago 126MBnginx web f7bb5701a33c 4 days ago 126MBpaigeeworld/centos7 latest 4cbe7aa905e7 5 years ago 382MB[root@localhost opt]# docker create -it paigeeworld/centos7 /bin/bash //创建容器c48649c8cee9124cb456be4f93882e6dff16f88ba45051731138142d99293dfe[root@localhost opt]# docker ps -a //查看容器CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc48649c8cee9 paigeeworld/centos7 "/bin/bash" 4 seconds ago Created relaxed_curran36fdfb0925ba nginx:latest "/bin/bash" 24 minutes ago Exited (0) 3 minutes ago mystifying_dijkstra[root@localhost opt]# docker start c48649c8cee9 //开启容器c48649c8cee9[root@localhost opt]# docker ps -a //查看容器CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc48649c8cee9 paigeeworld/centos7 "/bin/bash" 34 seconds ago Up 7 seconds relaxed_curran36fdfb0925ba nginx:latest "/bin/bash" 24 minutes ago Exited (0) 3 minutes ago容器的基本操作[root@localhost opt]# docker run paigeeworld/centos7 /usr/bin/bash -c ls / //docker run直接下载镜像,创建容器,并开启,进入容器执行命令,退出binbootdevetchome[root@localhost opt]# docker ps -a //查看容器CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc48649c8cee9 paigeeworld/centos7 "/bin/bash" 9 minutes ago Up 9 minutes relaxed_curran[root@localhost opt]# docker exec -it c48649c8cee9 /bin/bash //进入容器(必须为开启状态)bash-4.2# ls /bin dev home lib64 media opt root sbin sys usrboot etc lib lost+found mnt proc run srv tmp varbash-4.2# exit //退出容器exit[root@localhost opt]# docker ps -a //此时容器状态还是开启的状态CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc48649c8cee9 paigeeworld/centos7 "/bin/bash" 10 minutes ago Up 10 minutes relaxed_curran[root@localhost opt]# docker stop c48649c8cee9 //关闭容器c48649c8cee9[root@localhost opt]# docker ps -a //此时容器为退出状态CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc48649c8cee9 paigeeworld/centos7 "/bin/bash" 11 minutes ago Exited [root@localhost opt]# docker run -d paigeeworld/centos7 /bin/bash -c "while true;do echo hello;done" //持续在后台执行,-d在后台进行运行398f3d27f36b7f59a2167a71e71f61064e4e9a0808dfa13404caec0280a0b9c2[root@localhost opt]# docker ps -a //查看容器一直是开启状态CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES398f3d27f36b paigeeworld/centos7 "/bin/bash -c 'while…" 9 seconds ago Up 7 seconds [root@localhost opt]# docker export 36fdfb0925ba >

Nginx_c / / Container Export [root@localhost opt] # lscontainerd nginx nginx_c rh [root@localhost opt] # scp / opt/nginx_c root@192.168.13.129:/opt/ remote copy to another virtual machine to another virtual machine [root@localhost opt] # lscontainerd nginx nginx_c rh [root@localhost opt] # cat nginx_c | docker import-nginx:web / / Container import generates Mirror without creating a container sha256:1488d058197863aedd46d289eeb11dc39f19a2b855c3ecf383331a4d0bac568c [root@localhost opt] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx web 1488d0581978 5 seconds ago 125MB [root@localhost opt] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost opt] # docker ps-a | awk'{print "docker rm" $1}'| bash / / batch delete container docker resource control limits cpu usage rate

Limit the usage of cpu with the-- cpu-quota option

This is achieved by modifying the configuration file cpu.cfs_quota_us

Multitasking to share cpu proportionally

Docker run-cpu-shares 1024 Container A

Docker run-cpu-shares 1024 container B

Docker run-cpu-shares 2048 Container C

Use the-- cpuset-cpus option to restrict the right to use the cpu kernel [root@localhost opt] # docker run-- cpu-quota 20000 nginx:latest / / set 20% limit [root@localhost opt] # cd / sys/fs/cgroup/cpu/docker/ [root@localhost opt] # cat cpu.cfs_quota_us-1 [root@localhost opt] # docker run-itd-- name C1-- cpu-shares 512 paigeeworld/centos7 / / create container C1 set weight Make the proportion of c1 and c2 cpu resources 33.3% and 66.7% ec4ab03a7969eebe4746cfe67184bc2c6f9c97e81b22bc2ffab452820a78a0a7 [root @ localhost opt] # docker run-itd-- name c2-- cpu-shares 1024 paigeeworld/centos7c688b014329c6a33b0d66947f4489a1a1cb6febc321090ecb4a82b68ae6df250 [root@localhost opt] # docker ps-a / View Container CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc688b014329c Paigeeworld/centos7 "/ bin/bash" About a minute ago Up About a minute c2ec4ab03a7969 paigeeworld/centos7 "/ bin/bash" About a minute ago Up About a minute C1 [root@localhost opt] # docker run-- name c3-- cpuset-cpus 0Power1 paigeeworld/centos7 / / limit the use of containers specified Cpu [root@localhost opt] # docker ps-a / / View container information CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES751409a81682 paigeeworld/centos7 "/ bin/bash" 8 seconds ago Exited (0) 7 seconds ago c3 [root@localhost opt] # docker run-- name c5-m 512m paigeeworld/centos7 / / memory usage limit [root@localhost opt] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES8c3101668345 paigeeworld/centos7 "/ bin/bash" 5 seconds ago Exited (0 ) 4 seconds ago c5 limits on blkio-- device-read-bps: limit the amount of data that can be written to a device-- device-write-bps: limit the amount of data written to a device-- device-read-iops: limit the number of times to read a device-- device-write-iops: limit the number of times to write to a device [root@localhost opt] # docker run-d-- device-write-bps / dev/sda: Data Management Operation of 30mb paigeeworld/centos7docker

It is easy to view the data generated in the container

Data sharing among multiple containers

Two ways of management

Data volume

Data volume container

Data volume

A data volume is a special directory for containers.

Data volume container

A data volume container is an ordinary container.

Data volume sharing (sharing between host and container) [root@localhost ~] # docker pull centos / / download image [root@localhost] # docker run-v / var/www:/data1-- name web1-it centos / bin/bash / / share of associated host and container [root@2483bee94f1a /] # cd data1/ [root@2483bee94f1a data1] # echo "123" > test01.txt [root@2483bee94f1a data1] # exit exit [root@localhost ~] # cat / var/www/test01.txt 123data volume container sharing (container and container) [root@localhost ~] # docker run-- name web100-v / data1-v / data2-it centos / bin/bash / / create a web100 container with two volumes as data1 2 [root@ba6a328c068e /] # cd data1/ [root@ba6a328c068e data1] # echo "111" > 111.txt / / write contents in the directory [root@ba6a328c068e data1] # cd.. / data2/ [root@ba6a328c068e data2] # echo" 222" > 222.txt [root@ba6a328c068e data2] # exitexit [root@localhost ~] # docker run-it-volumes-from web100-it centos / bin/bash / / Mount the new container to the data volume container web100 [root@3f64be49dadd /] # cat data1/111.txt 111[ root@3f64be49dadd /] # cat data2/222.txt 222Basic Management of Docker Container "ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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