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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Docker core concept 1. What is Docker?

● is a lightweight "virtual machine"

● 's open source tools for running applications in a Linux container

2. The difference between Docker and virtual machine Docker container virtual machine startup characteristics almost no loss of computing power loss of about 50% performance is close to the native weaker than the system support (stand-alone) thousands of dozens of isolated resource limits completely isolated 3, Docker use scenarios

● packaged applications simplify deployment

● can be migrated at will without underlying hardware

● example: server migration from Tencent Cloud to Ali Cloud

4. The core concept of Docker

Mirror image

Container

Warehouse (public warehouse, private warehouse, storing images)

5. Two ways to install Docker in CentOS

● uses CURL to obtain Docker's installation script for installation

● uses the YUM repository to install Docker

Docker Mirror Operations docker Image basic Operations Container 1 docker inspect f7bb5701a33c 192.168.13.128 Container 2VR 192.168.13.129 [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 check out the image [root@localhost opt] # lscontainerd nginx rh [root] directly with 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 # # @ localhost opt] # scp / opt/nginx root@192.168.13.129:/opt/ # # remote copy to a 129server to start another virtual machine with docker (192.168.13.129) [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.13.128)[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 an image but not Create 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 limit cpu usage rate limit cpu usage by modifying the configuration file cpu.cfs_quota_us to achieve proportional sharing of cpudocker run-cpu-shares 1024 container Adocker run-cpu-shares 1024 container Bdocker run-cpu-shares 2048 capacity Device C uses 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## restricts the container to use the 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 limit 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-- number of data management for device-write-bps / dev/sda:30mb paigeeworld/centos7docker According to the management operation, it is convenient to view the data generated in the container to realize data sharing among multiple containers. Two management methods: data volume data volume container data volume data volume is a special directory for container use data volume container data volume container is a common container data volume sharing (host and container sharing): [root@localhost ~] # docker pull centos # # download image [root@localhost ~] # docker run-v / var/www:/data1-- name web1-it centos / bin/bash## associated host and container sharing [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## creates 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 222

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