Docker容器管理

网友投稿 1115 2022-09-04

Docker容器管理

Docker容器管理

一、Docker安装

[root@docker ~]# cat /etc/redhat-release CentOS Linux release 8.5.2111[root@docker ~]# wget ~]# mv docker-ce.repo /etc/yum.repos.d/[root@docker ~]# yum install -y docker-ce问题: 软件包 docker-ce-3:20.10.17-3.el8.x86_64 需要 containerd.io >= 1.4.1,但没有提供者可以被安装 - 安装的软件包的问题 buildah-1.22.3-2.module_el8.5.0+911+f19012f9.x86_64 - 软件包 buildah-1.22.3-2.module_el8.5.0+911+f19012f9.x86_64 需要 runc >= 1.0.0-26,但没有提供者可以被安装 - 无法为该任务安装最佳候选 - 软件包 runc-1.0.0-56.rc5.dev.git2abd837.module_el8.3.0+569+1bada2e4.x86_64 被模块过滤过滤掉 - 软件包 runc-1.0.0-66.rc10.module_el8.5.0+1004+c00a74f5.x86_64 被模块过滤过滤掉 - 软件包 runc-1.0.0-72.rc92.module_el8.5.0+1006+8d0e68a2.x86_64 被模块过滤过滤掉(尝试在命令行中添加 '--allowerasing' 来替换冲突的软件包 或 '--skip-broken' 来跳过无法安装的软件包 或 '--nobest' 来不只使用软件包的最佳候选)[root@docker yum.repos.d]# yum erase runc# yum install docker-ce -y#启动Docker服务[root@docker /]# systemctl enable docker[root@docker /]# systemctl start docker#查看Docker版本及启动状态[root@docker /]# docker -vDocker version 20.10.17, build 100c701[root@docker /]# docker versionClient: Docker Engine - Community Version: 20.10.17 API version: 1.41 Go version: go1.17.11 Git commit: 100c701 Built: Mon Jun 6 23:03:11 2022 OS/Arch: linux/amd64 Context: default Experimental: trueServer: Docker Engine - Community Engine: Version: 20.10.17 API version: 1.41 (minimum version 1.12) Go version: go1.17.11 Git commit: a89b842 Built: Mon Jun 6 23:01:29 2022 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.6.6 GitCommit: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1 runc: Version: 1.1.2 GitCommit: v1.1.2-0-ga916309 docker-init: Version: 0.19.0 GitCommit: de40ad0#查看Docker运行状态[root@docker /]# docker infoClient: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Docker Buildx (Docker Inc., v0.8.2-docker) scan: Docker Scan (Docker Inc., v0.17.0)Server: Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 20.10.17 Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true userxattr: false Logging Driver: json-file Cgroup Driver: cgroupfs Cgroup Version: 1 Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc Default Runtime: runc Init Binary: docker-init containerd version: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1 runc version: v1.1.2-0-ga916309 init version: de40ad0 Security Options: seccomp Profile: default Kernel Version: 4.18.0-348.el8.x86_64 Operating System: CentOS Linux 8 OSType: linux Architecture: x86_64 CPUs: 4 Total Memory: 782MiB Name: docker ID: RKJE:I7BN:MCMM:VETL:XDH4:Y2JJ:QVSN:JUYV:IOOT:YJY3:V5XC:ZFQE Docker Root Dir: /var/lib/docker Debug Mode: false Registry: Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false

二、Docker容器管理

#创建容器但不启动[root@docker ~]# docker create -it --name "nginx01" b692 /bin/bash f55c1a5e9969950bb720a2a4602ecd3092acbee0ab46ce63a177b68ec39d517d#查看运行状态的容器[root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4df23d8bbe6e daocloud.io/library/centos "/bin/bash" 2 hours ago Up 2 hours test#查看所有容器[root@docker ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf55c1a5e9969 b692 "/docker-entrypoint.…" 33 seconds ago Created nginx014df23d8bbe6e daocloud.io/library/centos "/bin/bash" 2 hours ago Up 2 hours test57246e6387d4 daocloud.io/library/centos:5 "/bin/bash" 3 hours ago Created wizardly_wiles#启动容器[root@docker ~]# docker start nginx01nginx01[root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf55c1a5e9969 b692 "/docker-entrypoint.…" About a minute ago Up 4 seconds 80/tcp nginx014df23d8bbe6e daocloud.io/library/centos "/bin/bash" 2 hours ago Up 2 hours test#创建并在后台运行一个容器 [root@docker ~]# docker run -itd --name="nginx02" --restart=always b692 /bin/bash #查看容器详细信息[root@docker ~]# docker inspect nginx02#bash进程覆盖掉了nginx进程[root@docker ~]# curl 172.17.0.4curl: (7) Failed to connect to 172.17.0.4 port 80: 拒绝连接#创建nginx03容器,并进入该容器[root@docker ~]# docker run -it --name="nginx03" --restart=always b692 /bin/bash root@cdacff99decd:/# exit #**断开与容器的连接且关闭容器**exit#创建nginx04容器,并进入该容器[root@docker ~]# docker run -it --name="nginx04" --restart=always b692 /bin/sh [root@docker ~]# docker run -it --name="nginx05" --restart=always b692 [root@docker ~]# docker run -itd --name="nginx06" --restart=always b692 4c9ea1c7397b7afd5e2ce09a4d71e6cc0fd819c487979e6ec9876fbe312ac0f7[root@docker ~]# docker inspect nginx06...........[root@docker ~]# curl 172.17.0.8Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.

For online documentation and support please refer to

#查看容器[root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4c9ea1c7397b b692 "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp nginx06ae1b5d52e919 b692 "/docker-entrypoint.…" 3 minutes ago Up 2 minutes 80/tcp nginx055fad20bffc57 b692 "/docker-entrypoint.…" 6 minutes ago Up 4 minutes 80/tcp nginx04cdacff99decd b692 "/docker-entrypoint.…" 12 minutes ago Up 7 minutes 80/tcp nginx0385018f0878a8 b692 "/docker-entrypoint.…" 16 minutes ago Up 16 minutes 80/tcp nginx02f55c1a5e9969 b692 "/docker-entrypoint.…" 28 minutes ago Up 25 minutes 80/tcp nginx014df23d8bbe6e daocloud.io/library/centos "/bin/bash" 3 hours ago Up 3 hours test#查看所有容器[root@docker ~]# docker ps -a#只查看容器id[root@docker ~]# docker ps -a -q4c9ea1c7397bae1b5d52e9195fad20bffc57cdacff99decd85018f0878a8f55c1a5e99694df23d8bbe6e57246e6387d4#查看镜像id[root@docker ~]# docker images -qb692a91e4e155d0da3dc9764300e315adb2f1ae98b2c895d#最近一次启动的容器[root@docker ~]# docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4cea1c7397b b692 "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp nginx06#查看容器信息[root@docker ~]# docker inspect b692#查看容器IP[root@docker ~]# docker inspect --format='{{.NetworkSettings.IPAddress}}' nginx06172.17.0.8#启动容器[root@docker ~]# docker start nginx01nginx01#正常关闭容器[root@docker ~]# docker stop nginx01#强制关闭容器[root@docker ~]# docker kill nginx01#关闭所有running状态的容器[root@docker ~]# docker kill $(docker ps -q)#删除容器[root@docker ~]# docker rm nginx01[root@docker ~]# docker rm f55c#根据状态删除所有容器[root@qfedu.com ~]# docker rm $(docker ps -qf status=exited)#重启容器[root@docker ~]# docker restart nginx01#暂停容器[root@docker ~]# docker pause nginx01 #恢复容器[root@docker ~]# docker unpause nginx01 #rename修改容器名称[root@docker ~]# docker rename nginx01 nginx[root@docker ~]# docker pause nginxnginx#inspect查看容器详细信息[root@docker ~]# docker inspect --format='{{.State.Status}}' nginxpaused[root@docker ~]# docker unpause nginxnginx[root@docker ~]# docker inspect --format='{{.State.Status}}' nginxrunning[root@docker ~]# docker inspect --format='{{.NetworkSettings.Ports}}' nginx01 map[80/tcp:[]][root@docker ~]# docker inspect --format='{{.NetworkSettings.MacAddress}}' nginx01 02:42:ac:11:00:08[root@docker ~]# docker rm f55cError response from daemon: You cannot remove a running container f55c1a5e9969950bb720a2a4602ecd3092acbee0ab46ce63a177b68ec39d517d. Stop the container before attempting removal or force remove#强制删除容器[root@docker ~]# docker rm f55c -f #创建容器[root@docker ~]# docker run -itd --name "nginx07" -p 8000:80 --restart=always b6925f6807061772d2d1a2aa3d5bbaedd2ebbea40a5267e1caf4d9fe7c75ca383fd2[root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES5f6807061772 b692 "/docker-entrypoint.…" 26 seconds ago Up 22 seconds 0.0.0.0:8000->80/tcp, :::8000->80/tcp nginx074c9ea1c7397b b692 "/docker-entrypoint.…" 13 hours ago Up 2 hours 80/tcp nginx06ae1b5d52e919 b692 "/docker-entrypoint.…" 13 hours ago Up 2 hours 80/tcp nginx055fad20bffc57 b692 "/docker-entrypoint.…" 13 hours ago Up 2 hours 80/tcp nginx04cdacff99decd b692 "/docker-entrypoint.…" 13 hours ago Up 2 hours 80/tcp nginx0385018f0878a8 b692 "/docker-entrypoint.…" 13 hours ago Up 2 hours 80/tcp nginx02f55c1a5e9969 b692 "/docker-entrypoint.…" 13 hours ago Up 14 minutes 80/tcp nginx014df23d8bbe6e daocloud.io/library/centos "/bin/bash" 15 hours ago Up 2 hours test#port输出容器端口与宿主机端口的映射情况[root@docker ~]# docker port nginx0780/tcp -> 0.0.0.0:800080/tcp -> :::8000[root@docker ~]# docker run -itd --name "nginx08" -p 8000:80 --restart=always b692 /bin/bash afbb3ccf057e42ebc81edac37f138bc3a4d0893fc9007e5349eb4710ec88af98docker: Error response from daemon: driver failed programming external connectivity on endpoint nginx08 (75274dd02fcb34bae6066d5b4a381f18c005b0dbc0c43169dc9e749b377c15c2): Bind for 0.0.0.0:8000 failed: port is already allocated.#连接容器#方法一 attach,容器创建时必须指定了交互shell[root@docker ~]# docker attach nginx01root@f55c1a5e9969:/# exitexit[root@docker ~]# docker attach nginx02root@85018f0878a8:/# exitexit[root@docker ~]# docker attach nginx03root@cdacff99decd:/# exitexit[root@docker ~]# docker attach nginx04#方法二 exec#交互型任务[root@docker ~]# docker exec -it nginx01 /bin/bashError response from daemon: Container f55c1a5e9969950bb720a2a4602ecd3092acbee0ab46ce63a177b68ec39d517d is not running[root@docker ~]# docker start nginx01nginx01[root@docker ~]# docker exec -it nginx01 /bin/bashroot@f55c1a5e9969:/# lsbin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp varboot docker-entrypoint.d etc lib media opt root sbin sys usrroot@f55c1a5e9969:/# #后台型任务[root@docker ~]# docker exec -it nginx01 touch /linux #查看执行情况[root@docker ~]# docker exec -it nginx01 /bin/bash root@f55c1a5e9969:/# lsbin dev docker-entrypoint.sh home lib64 media opt root sbin sys tmp varboot docker-entrypoint.d etc lib linux mnt proc run srv testfile usrroot@f55c1a5e9969:/# #查看容器运行的日志[root@docker ~]# docker logs nginx03#查看容器进程[root@docker ~]# docker top nginx06UID PID PPID C STIME TTY TIME CMDroot 5215 5196 0 21:54 pts/0 00:00:00 nginx: master process nginx -g daemon off;101 5260 5215 0 21:54 pts/0 00:00:00 nginx: worker process101 5261 5215 0 21:54 pts/0 00:00:00 nginx: worker process101 5262 5215 0 21:54 pts/0 00:00:00 nginx: worker process101 5263 5215 0 21:54 pts/0 00:00:00 nginx: worker process#实时输出Docker服务器端的事件,包括容器的创建,启动,关闭等。[root@docker ~]# docker start nginx01[root@docker ~]# docker events[root@docker ~]# docker wait nginx01137# diff 看容器内发生改变的文件root@f55c1a5e9969:/# touch skyboy[root@docker ~]# docker diff nginx01C /rootA /root/.bash_historyA /skyboyA /testfileA /linux#宿主机和容器之间相互Copy文件#容器[root@docker ~]# docker exec -it nginx01 /bin/bashroot@f55c1a5e9969:/# lsbin dev docker-entrypoint.sh home lib64 media opt root sbin srv testfile usrboot docker-entrypoint.d etc lib linux mnt proc run skyboy sys tmp varroot@f55c1a5e9969:/# echo 'I am study docker' > skyboyroot@f55c1a5e9969:/# cat skyboy I am study docker#宿主机[root@docker ~]# docker cp nginx01:/skyboy /root [root@docker ~]# cat /root/skyboy I am study docker#宿主机[root@docker ~]# mv /root/skyboy /root/docker[root@docker ~]# docker cp /root/docker nginx01:/#容器root@f55c1a5e9969:/# lsa.txt boot docker docker-entrypoint.sh home lib64 media opt root sbin srv testfile usrbin dev docker-entrypoint.d etc root@f55c1a5e9969:/# cat dockerI am study docker

三、Docker查看容器信息:格式化后

# 格式化:镜像,ID,端口号,状态[root@docker ~]# docker ps -a --format "table{{.Image}}\t{{.ID}}\t{{.Ports}}\t{{.Status}}"IMAGE CONTAINER ID PORTS STATUSdocker/getting-started 52bca3c677d4 0.0.0.0:8888->80/tcp, :::8888->80/tcp Up 39 minutesnginx:v11 f9710e015aa0 Exited (0) 41 minutes agocentos:v1 8acf7c7d3bbb Exited (1) 51 minutes agocentos:v1 9fe612ae824e Exited (127) 59 minutes agonginx:v1 bc2930aedb34 Exited (0) 28 hours agob692 82904f65698d 80/tcp Up 2 hoursb692 5f6807061772 0.0.0.0:8000->80/tcp, :::8000->80/tcp Up 2 hoursb692 4c9ea1c7397b 80/tcp Up 2 hoursb692 ae1b5d52e919 80/tcp Up 2 hoursb692 5fad20bffc57 80/tcp Up 2 hoursb692 cdacff99decd 80/tcp Up 2 hoursb692 85018f0878a8 80/tcp Up 2 hoursb692 f55c1a5e9969 Exited (137) 28 hours agodaocloud.io/library/centos 4df23d8bbe6e Up 2 hoursdaocloud.io/library/centos:5 57246e6387d4 Created#列出正在运行的容器[root@docker ~]# docker ps -a -f "status=running"CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES52bca3c677d4 docker/getting-started "/docker-entrypoint.…" 40 minutes ago Up 40 minutes 0.0.0.0:8888->80/tcp, :::8888->80/tcp zen_morse82904f65698d b692 "/docker-entrypoint.…" 30 hours ago Up 2 hours 80/tcp nginx085f6807061772 b692 "/docker-entrypoint.…" 30 hours ago Up 2 hours 0.0.0.0:8000->80/tcp, :::8000->80/tcp nginx074c9ea1c7397b b692 "/docker-entrypoint.…" 42 hours ago Up 2 hours 80/tcp nginx06ae1b5d52e919 b692 "/docker-entrypoint.…" 42 hours ago Up 2 hours 80/tcp nginx055fad20bffc57 b692 "/docker-entrypoint.…" 42 hours ago Up 2 hours 80/tcp nginx04cdacff99decd b692 "/docker-entrypoint.…" 42 hours ago Up 2 hours 80/tcp nginx0385018f0878a8 b692 "/docker-entrypoint.…" 42 hours ago Up 2 hours 80/tcp nginx024df23d8bbe6e daocloud.io/library/centos "/bin/bash" 45 hours ago Up 2 hours test#列出已经退出的容器[root@docker ~]# docker ps -a -f "status=exited"CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf9710e015aa0 nginx:v11 "/docker-entrypoint.…" 43 minutes ago Exited (0) 43 minutes ago lucid_ramanujan8acf7c7d3bbb centos:v1 "/bin/bash" 59 minutes ago Exited (1) 54 minutes ago charming_gagarin9fe612ae824e centos:v1 "/bin/bash" About an hour ago Exited (127) About an hour ago keen_rosalindbc2930aedb34 nginx:v1 "/bin/bash" 28 hours ago Exited (0) 28 hours ago eloquent_mccarthyf55c1a5e9969 b692 "/docker-entrypoint.…" 43 hours ago Exited (137) 28 hours ago nginx01#获取单个容器IDdocker inspect --format '{{ .NetworkSettings.IPAddress }}' #[root@docker ~]# docker inspect --format '{{ .NetworkSettings.IPAddress }}' nginx02172.17.0.8#获取所有容器的ip[root@docker ~]# docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq)/zen_morse - 172.17.0.10/lucid_ramanujan - /charming_gagarin - /keen_rosalind - /eloquent_mccarthy - /nginx08 - 172.17.0.9/nginx07 - 172.17.0.6/nginx06 - 172.17.0.3/nginx05 - 172.17.0.2/nginx04 - 172.17.0.7/nginx03 - 172.17.0.5/nginx02 - 172.17.0.8/nginx01 - /test - 172.17.0.4

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:MySQL实战:explain详解(下)
下一篇:干货|MySQL两千万数据大表优化过程,三种解决方案!(mysql百万数据设计和优化)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~