当前位置:   article > 正文

Docker常用命令总结_docker启动命令

docker启动命令
  • 基础命令

1.启动docker

systemctl start docker

2.关闭docker

systemctl stop docker

3.设置docker为自启动

systemctl enable --now docker

4.重启docker

systemctl restart docker

3.查看docker版本信息

docker version

4.查看docker详细信息

docker info

  1. Client:
  2. Debug Mode: false #client端是否开启 debug
  3. Server:
  4. Containers: 2 #当前主机运行的容器总数
  5. Running: 0 #有几个容器是正在运行的
  6. Paused: 0 #有几个容器是暂停的
  7. Stopped: 2 #有几个容器是停止的
  8. Images: 4 #当前服务器的镜像数
  9. Server Version: 19.03.5 #服务端版本
  10. Storage Driver: overlay2 #正在使用的存储引擎
  11. Backing Filesystem: extfs #后端文件系统,即服务器的磁盘文件系统
  12. Supports d_type: true #是否支持 d_type
  13. Native Overlay Diff: true #是否支持差异数据存储
  14. Logging Driver: json-file
  15. #日志类型,每个容器的标准输出以日志存放在
  16. /var/lib/docker/containers/<CONTAINERID>/<CONTAINER ID>-json.log
  17. Cgroup Driver: cgroupfs #Cgroups 类型
  18. Plugins: #插件
  19. Volume: local #卷
  20. Network: bridge host ipvlan macvlan null overlay #overlay 跨主机通信
  21. Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog #日志类型
  22. Swarm: inactive #是否支持 swarm
  23. Runtimes: runc #已安装的容器运行时
  24. Default Runtime: runc #默认使用的容器运行时
  25. Init Binary: docker-init #初始化容器的守护进程,即 pid 为 1 的进程
  26. containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339 #版本
  27. runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657 #runc版本
  28. init version: fec3683 #init 版本
  29. Security Options: #安全选项
  30. apparmor #安全模块,https://docs.docker.com/engine/security/apparmor/
  31. seccomp #安全计算模块,即制容器操作,https://docs.docker.com/engine/security/seccomp/
  32. Profile: default #默认的配置文件
  33. Kernel Version: 4.15.0-29-generic #宿主机内核版本
  34. Operating System: Ubuntu 18.04.1 LTS #宿主机操作系统
  35. OSType: linux #宿主机操作系统类型
  36. Architecture: x86_64 #宿主机架构
  37. CPUs: 1 #宿主机CPU数量
  38. Total Memory: 962MiB #宿主机总内存
  39. Name: ubuntu1804.wang.org #宿主机hostname
  40. ID: IZHJ:WPIN:BRMC:XQUI:VVVR:UVGK:NZBM:YQXT:JDWB:33RS:45V7:SQWJ #宿主机ID
  41. Docker Root Dir: /var/lib/docker
  42. #宿主机关于docker数据的保存目录,建议使用独立SSD的磁
  43. 盘,保证性能和空间
  44. Debug Mode: false #server端是否开启debug
  45. Registry: https://index.docker.io/v1/ #仓库路径
  46. Labels:
  47. Experimental: false #是否测试版
  48. Insecure Registries:
  49. 127.0.0.0/8 : #非安全的镜像仓库
  50. Registry Mirrors:
  51. https://si7y70hh.mirror.aliyuncs.com/ #镜像仓库
  52. Live Restore Enabled: false #是否开启活动重启 (重启docker-daemon 不关闭容器 )
  53. WARNING: No swap limit support #系统警告信息 (没有开启 swap 资源限制 )

#解决上述SWAP报警提示:

1.修改配置文件

vim /etc/default/grub

----省略---

GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 swapaccount=1" #修改此行

---省略---

2.生效修改后的grub

#Ubuntu执行

update-grub

#Centos执行

grub2-mkconfig -o /boot/grub2/grub.cfg

3.重启系统

reboot

5.查看docker命令的帮助

docker --help

6.查看docker子命令的帮助

#比如忘记docker pull的选项和可带的参数

docker pull --help

  • 镜像命令

1.拉取镜像

#加:tag(版本号)则是拉取指定版本

#不加tag(版本号)即拉取docker仓库中 该镜像的最新版本latest

格式:docker pull [镜像名]

格式:docker pull [镜像名:tag]

  1. #拉取最新版本
  2. [root@centos7-temp ~]# docker pull nginx
  3. Using default tag: latest #默认下载最新版本
  4. latest: Pulling from library/nginx
  5. a2abf6c4d29d: Pull complete #分层下载
  6. a9edb18cadd1: Pull complete
  7. 589b7251471a: Pull complete
  8. 186b1aaa4aa6: Pull complete
  9. b4df32aa5a72: Pull complete
  10. a0bcbecc962e: Pull complete
  11. Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31 #摘要
  12. Status: Downloaded newer image for nginx:latest
  13. docker.io/library/nginx:latest #下载的完整地址
  14. #拉取指定版本
  15. [root@centos7-temp ~]# docker pull nginx:1.18.0
  16. 1.18.0: Pulling from library/nginx
  17. f7ec5a41d630: Pull complete
  18. 0b20d28b5eb3: Pull complete
  19. 1576642c9776: Pull complete
  20. c12a848bad84: Pull complete
  21. 03f221d9cf00: Pull complete
  22. Digest: sha256:e90ac5331fe095cea01b121a3627174b2e33e06e83720e9a934c7b8ccc9c55a0
  23. Status: Downloaded newer image for nginx:1.18.0
  24. docker.io/library/nginx:1.18.0
  25. #查看本地镜像列表
  26. [root@centos7-temp ~]# docker images
  27. REPOSITORY TAG IMAGE ID CREATED SIZE
  28. nginx latest 605c77e624dd 12 months ago 141MB
  29. nginx 1.18.0 c2c45d506085 21 months ago 133MB

2.查看镜像分层历史

格式:docker image history [镜像名]

  1. [root@centos7-temp ~]# docker image history nginx
  2. IMAGE CREATED CREATED BY SIZE COMMENT
  3. 605c77e624dd 12 months ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
  4. <missing> 12 months ago /bin/sh -c #(nop) STOPSIGNAL SIGQUIT 0B
  5. <missing> 12 months ago /bin/sh -c #(nop) EXPOSE 80 0B
  6. <missing> 12 months ago /bin/sh -c #(nop) ENTRYPOINT ["/docker-entr… 0B
  7. <missing> 12 months ago /bin/sh -c #(nop) COPY file:09a214a3e07c919a… 4.61kB
  8. <missing> 12 months ago /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7… 1.04kB
  9. <missing> 12 months ago /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0… 1.96kB
  10. <missing> 12 months ago /bin/sh -c #(nop) COPY file:65504f71f5855ca0… 1.2kB
  11. <missing> 12 months ago /bin/sh -c set -x && addgroup --system -… 61.1MB
  12. <missing> 12 months ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~bullseye 0B
  13. <missing> 12 months ago /bin/sh -c #(nop) ENV NJS_VERSION=0.7.1 0B
  14. <missing> 12 months ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.21.5 0B
  15. <missing> 12 months ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B
  16. <missing> 12 months ago /bin/sh -c #(nop) CMD ["bash"] 0B
  17. <missing> 12 months ago /bin/sh -c #(nop) ADD file:09675d11695f65c55… 80.4MB

3.查看镜像的元数据(后面加容器,也可查看容器的元数据)

#以json格式显示

格式:docker inspect [镜像/容器]

  1. #查看nginx的镜像元数据
  2. [root@centos7-temp ~]# docker inspect nginx

4.搜索镜像

格式:docker search [镜像名]

  1. #搜索STARS>9000的mysql镜像
  2. [root@centos7-temp ~]# docker search --filter=STARS=9000 mysql
  3. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  4. mysql MySQL is a widely used, open-source relation… 13639 [OK]

5.查看本地镜像列表

docker images

  1. #查看本地镜像列表
  2. [root@centos7-temp ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. nginx latest 605c77e624dd 12 months ago 141MB
  5. nginx 1.18.0 c2c45d506085 21 months ago 133MB
  6. ##显示完整的ImageID
  7. [root@centos7-temp ~]# docker images --no-trunc
  8. REPOSITORY TAG IMAGE ID CREATED SIZE
  9. nginx latest sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85 12 months ago 141MB
  10. nginx 1.18.0 sha256:c2c45d506085d300b72a6d4b10e3dce104228080a2cf095fc38333afe237e2be 21 months ago 133MB

#显示结果信息说明:

  1. REPOSITORY      #镜像所属的仓库名称
  2. TAG            #镜像版本号(标识符),默认为latest
  3. IMAGE ID        #镜像唯一ID标识,如果ID相同,说明是同一个镜像有多个名称
  4. CREATED         #镜像在仓库中被创建时间
  5. VIRTUAL SIZE    #镜像的大小

5.导出和导入镜像

(1)导出镜像

#利用docker save命令可以将从本地镜像导出为一个打包 tar文件,然后复制到其它主机进行导入使用

格式:docker save 镜像名 -o tar文件路径

选项:-o:将输出字符串写入文件

用法:

  1. #查看本地镜像列表
  2. [root@docker1 ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. nginx latest 605c77e624dd 12 months ago 141MB
  5. nginx 1.18.0 c2c45d506085 21 months ago 133MB
  6. [root@docker1 ~]# docker save nginx:1.18.0 -o /data/nginx.tar
  7. #打包镜像为tar文件
  8. [root@docker1 ~]# docker save nginx:1.18.0 -o /data/nginx.tar
  9. #查看tar文件
  10. [root@docker1 ~]# ls /data/nginx.tar
  11. /data/nginx.tar
  12. #上传至另一台主机上
  13. [root@docker1 ~]# scp /data/nginx.tar 10.0.0.100:/data
  14. The authenticity of host '10.0.0.100 (10.0.0.100)' can't be established.
  15. ECDSA key fingerprint is SHA256:ZyQrEY2+tXpQpgIXx8tA8ZSzHLXmhChf2aQrbEtX+Ls.
  16. ECDSA key fingerprint is MD5:1b:f1:e3:71:ff:8c:66:c8:39:63:e0:47:82:df:15:cc.
  17. Are you sure you want to continue connecting (yes/no)? yes
  18. Warning: Permanently added '10.0.0.100' (ECDSA) to the list of known hosts.
  19. root@10.0.0.100's password:
  20. nginx.tar 100% 131MB 78.4MB/s 00:01

#打包所有镜像的方法:

  1. #打包所有镜像至不同文件
  2. [root@centos7-temp ~]# docker images | awk 'NR!=1{print $1,$2}' | while read repo tag\
  3. > ;do docker save $repo:$tag -o /opt/$repo-$tag.tar ;done
  4. #打包所有镜像至同一个文件
  5. 方法1: 使用image ID导出镜像,在导入后的镜像没有REPOSITORY和TAG,显示为<none>
  6. [root@centos7-temp ~]#docker save `docker images -qa` -o all.tar
  7. [root@centos7-temp ~]# docker images
  8. REPOSITORY TAG IMAGE ID CREATED SIZE
  9. <none> <none> 605c77e624dd 12 months ago 141MB
  10. <none> <none> 3218b38490ce 12 months ago 516MB
  11. 方法2:将所有镜像导入到一个文件中,此方法导入后可以看REPOSITORY和TAG
  12. [root@centos7-temp ~]#docker save `docker images | awk 'NR!=1{print $1":"$2}'` -o all.tar
  13. [root@centos7-temp ~]# docker images
  14. REPOSITORY TAG IMAGE ID CREATED SIZE
  15. nginx latest 605c77e624dd 12 months ago 141MB
  16. mysql latest 3218b38490ce 12 months ago 516MB
  17. 方法3:将所有镜像导入到一个文件中,此方法导入后可以看REPOSITORY和TAG
  18. [root@centos7-temp ~]#docker image save `docker image ls --format "{{.Repository}}:{{.Tag}}"` -o all.tar
  19. [root@centos7-temp ~]# docker images
  20. REPOSITORY TAG IMAGE ID CREATED SIZE
  21. nginx latest 605c77e624dd 12 months ago 141MB
  22. mysql latest 3218b38490ce 12 months ago 516MB

(2)导入镜像

#主机ip:10.0.0.100

#利用docker load命令可以将镜像导出的打包或压缩文件再导入

格式:docker load -i tar文件路径

选项:-i:输入字符串从tar存档文件读取

用法:

  1. #查看本地镜像列表
  2. [root@centos7-temp ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. #导入镜像
  5. [root@centos7-temp ~]# docker load -i /data/nginx.tar
  6. 7e718b9c0c8c: Loading layer [==================================================>] 72.52MB/72.52MB
  7. f3fdf88f1cb7: Loading layer [==================================================>] 64.6MB/64.6MB
  8. 6fcbf7acaafd: Loading layer [==================================================>] 3.072kB/3.072kB
  9. 4fe7d87c8e14: Loading layer [==================================================>] 4.096kB/4.096kB
  10. 4fa6704c8474: Loading layer [==================================================>] 3.584kB/3.584kB
  11. Loaded image: nginx:1.18.0
  12. #再次查看本地镜像列表
  13. [root@centos7-temp ~]# docker images
  14. REPOSITORY TAG IMAGE ID CREATED SIZE
  15. nginx 1.18.0 c2c45d506085 21 months ago 133MB

(3)一次导出多个镜像,然后导入

  1. #查看本地镜像列表
  2. [root@centos7-temp ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. nginx latest 605c77e624dd 12 months ago 141MB
  5. nginx 1.18.0 c2c45d506085 21 months ago 133MB
  6. #打包两个不同版本的nginx镜像
  7. [root@centos7-temp ~]# docker save nginx > /all.tar
  8. #查看tar文件
  9. [root@centos7-temp ~]# ll -h /all.tar
  10. -rw-r--r-- 1 root root 270M 11 16:54 /all.tar
  11. #删除原来的两个镜像
  12. [root@centos7-temp ~]# docker rmi -f `docker images -q`
  13. Untagged: nginx:latest
  14. Untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
  15. Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
  16. Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
  17. Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
  18. Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
  19. Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
  20. Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
  21. Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
  22. Untagged: nginx:1.18.0
  23. Untagged: nginx@sha256:e90ac5331fe095cea01b121a3627174b2e33e06e83720e9a934c7b8ccc9c55a0
  24. Deleted: sha256:c2c45d506085d300b72a6d4b10e3dce104228080a2cf095fc38333afe237e2be
  25. Deleted: sha256:43d6c481a041dbcc1d8ea9c565b1b692bcb28da3414683c316703c669c012ebc
  26. Deleted: sha256:defebc732c194dd0b5b39e20c4d014896ce120207f5dfdb41ed6696b0e8224d6
  27. Deleted: sha256:4ea0f2550407442f808812429981c0b62d8dd6a531db8a412640293a1faf8f3c
  28. Deleted: sha256:778ca58cf39b8fa0776ade88562750a035a24ec5afb7dc4ab2aa892b2c09769d
  29. Deleted: sha256:7e718b9c0c8c2e6420fe9c4d1d551088e314fe923dce4b2caf75891d82fb227d
  30. #再次查看本地镜像列表
  31. [root@centos7-temp ~]# docker images
  32. REPOSITORY TAG IMAGE ID CREATED SIZE
  33. #导入镜像
  34. [root@centos7-temp ~]# docker load -i /all.tar
  35. 7e718b9c0c8c: Loading layer [==================================================>] 72.52MB/72.52MB
  36. f3fdf88f1cb7: Loading layer [==================================================>] 64.6MB/64.6MB
  37. 6fcbf7acaafd: Loading layer [==================================================>] 3.072kB/3.072kB
  38. 4fe7d87c8e14: Loading layer [==================================================>] 4.096kB/4.096kB
  39. 4fa6704c8474: Loading layer [==================================================>] 3.584kB/3.584kB
  40. Loaded image: nginx:1.18.0
  41. 2edcec3590a4: Loading layer [==================================================>] 83.86MB/83.86MB
  42. e379e8aedd4d: Loading layer [==================================================>] 62MB/62MB
  43. b8d6e692a25e: Loading layer [==================================================>] 3.072kB/3.072kB
  44. f1db227348d0: Loading layer [==================================================>] 4.096kB/4.096kB
  45. 32ce5f6a5106: Loading layer [==================================================>] 3.584kB/3.584kB
  46. d874fd2bc83b: Loading layer [==================================================>] 7.168kB/7.168kB
  47. Loaded image: nginx:latest
  48. #再次查看本地镜像列表
  49. [root@centos7-temp ~]# docker images
  50. REPOSITORY TAG IMAGE ID CREATED SIZE
  51. nginx latest 605c77e624dd 12 months ago 141MB
  52. nginx 1.18.0 c2c45d506085 21 months ago 133MB

6.删除镜像

格式:docker rmi  [选项] [镜像名]

选项:-f:强制删除

用法:

  1. #删除指定的镜像
  2. [root@centos7-temp ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. nginx latest 605c77e624dd 12 months ago 141MB
  5. mysql latest 3218b38490ce 12 months ago 516MB
  6. [root@centos7-temp ~]# docker rmi -f nginx
  7. Untagged: nginx:latest
  8. Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
  9. Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
  10. Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
  11. Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
  12. Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
  13. Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
  14. Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
  15. [root@centos7-temp ~]# docker images
  16. REPOSITORY TAG IMAGE ID CREATED SIZE
  17. mysql latest 3218b38490ce 12 months ago 516MB
  18. #删除多个镜像
  19. [root@centos7-temp ~]# docker images
  20. REPOSITORY TAG IMAGE ID CREATED SIZE
  21. nginx latest 605c77e624dd 12 months ago 141MB
  22. mysql latest 3218b38490ce 12 months ago 516MB
  23. [root@centos7-temp ~]# docker rmi nginx mysql
  24. Untagged: nginx:latest
  25. Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
  26. Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
  27. Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
  28. Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
  29. Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
  30. Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
  31. Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
  32. Untagged: mysql:latest
  33. Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
  34. Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
  35. Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
  36. Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9
  37. Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3
  38. Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff
  39. Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba
  40. Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7
  41. Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da
  42. Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838
  43. Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0
  44. Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa
  45. Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2
  46. [root@centos7-temp ~]# docker images
  47. REPOSITORY TAG IMAGE ID CREATED SIZE
  48. #删除所有的镜像
  49. [root@centos7-temp ~]# docker images
  50. REPOSITORY TAG IMAGE ID CREATED SIZE
  51. <none> <none> 605c77e624dd 12 months ago 141MB
  52. mysql latest 3218b38490ce 12 months ago 516MB
  53. [root@centos7-temp ~]# docker rmi -f `docker images -q`
  54. Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
  55. Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
  56. Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
  57. Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
  58. Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
  59. Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
  60. Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
  61. Untagged: mysql:latest
  62. Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
  63. Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
  64. Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
  65. Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9
  66. Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3
  67. Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff
  68. Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba
  69. Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7
  70. Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da
  71. Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838
  72. Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0
  73. Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa
  74. Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2
  75. [root@centos7-temp ~]# docker images
  76. REPOSITORY TAG IMAGE ID CREATED SIZE

7.镜像打标签

格式:docker tag 旧TAG 新TAG

用法:

  •  容器命令

1.启动容器

注意: 容器启动后,如果容器内没有前台运行的进程,将自动退出停止

格式:docker run [选项] [镜像名] [shell命令] [参数]

选项:

--name:指定容器名称

-it:生成一个交互式的终端

-h:指定容器的主机名

--rm:退出时自动删除容器

-d:后台运行容器

--restart:定义容器的重启策略

-P:随机端口映射

-p:指定端口映射

#启动容器的流程:

用法: 

  1. #运行第一个容器
  2. [root@centos7-temp ~]# docker run hello-world
  3. Unable to find image 'hello-world:latest' locally
  4. latest: Pulling from library/hello-world
  5. 2db29710123e: Pull complete
  6. Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
  7. Status: Downloaded newer image for hello-world:latest
  8. Hello from Docker!
  9. This message shows that your installation appears to be working correctly.
  10. To generate this message, Docker took the following steps:
  11. 1. The Docker client contacted the Docker daemon.
  12. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
  13. (amd64)
  14. 3. The Docker daemon created a new container from that image which runs the
  15. executable that produces the output you are currently reading.
  16. 4. The Docker daemon streamed that output to the Docker client, which sent it
  17. to your terminal.
  18. To try something more ambitious, you can run an Ubuntu container with:
  19. $ docker run -it ubuntu bash
  20. Share images, automate workflows, and more with a free Docker ID:
  21. https://hub.docker.com/
  22. For more examples and ideas, visit:
  23. https://docs.docker.com/get-started/
  24. #查看本地镜像列表,生成了hello-world镜像
  25. [root@centos7-temp ~]# docker images
  26. REPOSITORY TAG IMAGE ID CREATED SIZE
  27. nginx 1.18.0 605c77e624dd 12 months ago 141MB
  28. nginx latest 605c77e624dd 12 months ago 141MB
  29. mysql latest 3218b38490ce 12 months ago 516MB
  30. hello-world latest feb5d9fea6a5 15 months ago 13.3kB
  31. #查看容器运行进程
  32. [root@centos7-temp ~]# docker ps -a
  33. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  34. 08be02244e32 hello-world "/hello" 56 seconds ago Exited (0) 55 seconds ago dazzling_banzai
  1. #一次性运行容器中的命令
  2. [root@centos7-temp ~]#docker ps
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. [root@centos7-temp ~]# docker run nginx echo "Hello "
  5. Hello
  6. [root@centos7-temp ~]# docker ps -a
  7. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  8. 96ccb61ea8cf nginx "/docker-entrypoint.…" 5 seconds ago Exited (0) 4 seconds ago hopeful_carver
  1. #指定容器名称
  2. [root@centos7-temp ~]# docker run --name a1 nginx
  3. /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
  4. /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
  5. /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
  6. 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
  7. 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
  8. /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
  9. /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
  10. /docker-entrypoint.sh: Configuration complete; ready for start up
  11. 2023/01/04 09:35:50 [notice] 1#1: using the "epoll" event method
  12. 2023/01/04 09:35:50 [notice] 1#1: nginx/1.21.5
  13. 2023/01/04 09:35:50 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
  14. 2023/01/04 09:35:50 [notice] 1#1: OS: Linux 3.10.0-1160.81.1.el7.x86_64
  15. 2023/01/04 09:35:50 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
  16. 2023/01/04 09:35:50 [notice] 1#1: start worker processes
  17. 2023/01/04 09:35:50 [notice] 1#1: start worker process 31
  18. 2023/01/04 09:35:50 [notice] 1#1: start worker process 32
  19. ^C2023/01/04 09:37:22 [notice] 1#1: signal 2 (SIGINT) received, exiting
  20. 2023/01/04 09:37:22 [notice] 31#31: exiting
  21. 2023/01/04 09:37:22 [notice] 31#31: exit
  22. 2023/01/04 09:37:22 [notice] 32#32: exiting
  23. 2023/01/04 09:37:22 [notice] 32#32: exit
  24. 2023/01/04 09:37:22 [notice] 1#1: signal 17 (SIGCHLD) received from 31
  25. 2023/01/04 09:37:22 [notice] 1#1: worker process 31 exited with code 0
  26. 2023/01/04 09:37:22 [notice] 1#1: signal 29 (SIGIO) received
  27. 2023/01/04 09:37:22 [notice] 1#1: signal 17 (SIGCHLD) received from 32
  28. 2023/01/04 09:37:22 [notice] 1#1: worker process 32 exited with code 0
  29. 2023/01/04 09:37:22 [notice] 1#1: exit
  30. [root@centos7-temp ~]# docker ps -a
  31. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  32. 20c5cc588587 nginx "/docker-entrypoint.…" About a minute ago Exited (0) 9 seconds ago a1
  1. #运行交互式容器
  2. [root@centos7-temp ~]# docker run -it nginx sh
  3. # ls
  4. bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
  5. boot docker-entrypoint.d etc lib media opt root sbin sys usr
  6. #
  1. #退出交互式容器,容器不在运行
  2. # exit
  3. [root@centos7-temp ~]#
  4. #查看运行的容器
  5. [root@centos7-temp ~]# docker ps
  6. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  1. #退出交互式容器,容器继续运行
  2. 同时按三个键,ctrl+p+q
  3. #查看运行的容器
  4. [root@centos7-temp ~]# docker ps
  5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  6. d17aecdf3a46 nginx "/docker-entrypoint.…" 19 seconds ago Up 18 seconds 80/tcp ecstatic_archimedes
  1. #设置容器内的主机名
  2. [root@centos7-temp ~]# docker run -it -h centos7 alpine
  3. / # hostname
  4. centos7
  5. / # cat /etc/hosts
  6. 127.0.0.1 localhost
  7. ::1 localhost ip6-localhost ip6-loopback
  8. fe00::0 ip6-localnet
  9. ff00::0 ip6-mcastprefix
  10. ff02::1 ip6-allnodes
  11. ff02::2 ip6-allrouters
  12. 172.17.0.2 centos7
  13. / #
  1. #一次性运行容器,退出后立即删除,用于测试
  2. [root@centos7-temp ~]# docker run --rm alpine cat /etc/issue
  3. Welcome to Alpine Linux 3.17
  4. Kernel \r on an \m (\l)
  5. #查看运行的容器
  6. [root@centos7-temp ~]# docker ps
  7. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  1. #前台运行容器,会占用终端
  2. [root@centos7-temp ~]# docker run nginx
  3. /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
  4. /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
  5. /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
  6. 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
  7. 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
  8. /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
  9. /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
  10. /docker-entrypoint.sh: Configuration complete; ready for start up
  11. 2023/01/04 15:02:23 [notice] 1#1: using the "epoll" event method
  12. 2023/01/04 15:02:23 [notice] 1#1: nginx/1.21.5
  13. 2023/01/04 15:02:23 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
  14. 2023/01/04 15:02:23 [notice] 1#1: OS: Linux 3.10.0-1160.81.1.el7.x86_64
  15. 2023/01/04 15:02:23 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
  16. 2023/01/04 15:02:23 [notice] 1#1: start worker processes
  17. 2023/01/04 15:02:23 [notice] 1#1: start worker process 31
  18. 2023/01/04 15:02:23 [notice] 1#1: start worker process 32
  19. #后台运行容器
  20. [root@centos7-temp ~]# docker run -d nginx
  21. 5ede6fcb99bd6064be801beac288d2ce6de8d62e5f38dad4e2f756ee0de8dcd0
  22. [root@centos7-temp ~]# docker ps
  23. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  24. 5ede6fcb99bd nginx "/docker-entrypoint.…" 10 seconds ago Up 10 seconds 80/tcp modest_perlman
  1. #设置容器开机自启动(默认不会自启动)
  2. [root@centos7-temp ~]# docker run -d --name nginx --restart=always nginx
  3. 6aea8688df49ffd012e57a3c46bfa1e3f71dbeb3c2eca2de21a9f267e4d4c42a
  4. [root@centos7-temp ~]# reboot
  5. [root@centos7-temp ~]# docker ps
  6. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  7. 6aea8688df49 nginx "/docker-entrypoint.…" 39 seconds ago Up 18 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
  1. #随机端口映射
  2. [root@centos7-temp ~]# docker run -P nginx
  3. [root@centos7-temp ~]# ss -ntl
  4. State Recv-Q Send-Q Local Address:Port Peer Address:Port
  5. LISTEN 0 128 *:32769 *:*
  6. LISTEN 0 128 *:22 *:*
  7. LISTEN 0 100 127.0.0.1:25 *:*
  8. LISTEN 0 128 [::]:32769 [::]:*
  9. LISTEN 0 128 [::]:22 [::]:*
  10. LISTEN 0 100 [::1]:25 [::]:*
  11. #此命令可以查看容器端口映射关系
  12. [root@centos7-temp ~]# docker port 1bf6734c7b04
  13. 80/tcp -> 0.0.0.0:32769
  14. 80/tcp -> :::32769
  15. #测试访问
  16. [root@centos7-temp ~]# curl 127.0.0.1:32769 -I
  17. HTTP/1.1 200 OK
  18. Server: nginx/1.21.5
  19. Date: Sun, 08 Jan 2023 15:30:08 GMT
  20. Content-Type: text/html
  21. Content-Length: 615
  22. Last-Modified: Tue, 28 Dec 2021 15:28:38 GMT
  23. Connection: keep-alive
  24. ETag: "61cb2d26-267"
  25. Accept-Ranges: bytes
  26. #退出前台运行,容器也会停止
  27. [root@centos7-temp ~]# docker run -P nginx
  28. 2023/01/08 15:33:42 [notice] 1#1: exit
  29. [root@centos7-temp ~]# docker ps
  30. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

#容器启动后,默认处于预定义的NAT网络中,所以外部网络的主机无法直接访问容器中网络服务docker run -P 可以将事先容器预定义的所有端口映射宿主机的网卡的随机端口,默认从32768开始
使用随机端口时,当停止容器后再启动可能会导致端口发生变化 

  1. #指定端口映射
  2. [root@centos7-temp ~]# docker run -d -p 80:80 nginx
  3. cd4108e6577e20e270510f3d087c0e6fa82ef918d2d342cc5bd5870f334c1519
  4. [root@centos7-temp ~]# ss -ntl
  5. State Recv-Q Send-Q Local Address:Port Peer Address:Port
  6. LISTEN 0 128 *:80 *:*
  7. LISTEN 0 128 *:22 *:*
  8. LISTEN 0 100 127.0.0.1:25 *:*
  9. LISTEN 0 128 [::]:80 [::]:*
  10. LISTEN 0 128 [::]:22 [::]:*
  11. LISTEN 0 100 [::1]:25 [::]:*
  12. [root@centos7-temp ~]# docker ps
  13. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  14. cd4108e6577e nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp pedantic_clarke

#docker run -p 可以将容器的预定义的指定端口映射到宿主机的相应端口
注意: 多个容器映射到宿主机的端口不能冲突,但容器内使用的端口可以相同 

2.列出容器

格式:docker ps [选项]

选项:

-a:列出所有,包括运行和停止的

-q:只显示容器ID

-s:查看容器大小

-l:查看最新创建的容器

-f:查看停止运行的容器

用法:

  1. #查看运行的容器
  2. [root@centos7-temp ~]# docker ps
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 6950646e97f8 nginx "/docker-entrypoint.…" 27 minutes ago Up 2 seconds 80/tcp nginx
  5. #查看所有容器
  6. [root@centos7-temp ~]# docker ps -a
  7. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  8. e506a29205b6 mysql "docker-entrypoint.s…" 3 seconds ago Exited (1) 2 seconds ago kind_ardinghelli
  9. 6950646e97f8 nginx "/docker-entrypoint.…" 28 minutes ago Up 42 seconds 80/tcp nginx
  10. #只显示容器ID
  11. [root@centos7-temp ~]# docker ps -q
  12. 6950646e97f8
  13. #查看容器大小
  14. [root@centos7-temp ~]# docker ps -s
  15. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
  16. 6950646e97f8 nginx "/docker-entrypoint.…" 29 minutes ago Up 2 minutes 80/tcp nginx 1.09kB (virtual 141MB)
  17. #查看最新创建的容器
  18. [root@centos7-temp ~]# docker ps -l
  19. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  20. e506a29205b6 mysql "docker-entrypoint.s…" 2 minutes ago Exited (1) 2 minutes ago kind_ardinghelli
  21. #查看停止运行的容器
  22. [root@centos7-temp ~]# docker ps -f 'status=exited'
  23. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  24. e506a29205b6 mysql "docker-entrypoint.s…" 3 minutes ago Exited (1) 3 minutes ago kind_ardinghelli

3.查看容器内的进程

格式:docker top 容器名/容器ID

用法:

  1. #容器名查看
  2. [root@centos7-temp ~]# docker top nginx
  3. UID PID PPID C STIME TTY TIME CMD
  4. root 2053 2033 0 23:42 ? 00:00:00 nginx: master process nginx -g daemon off;
  5. 101 2098 2053 0 23:42 ? 00:00:00 nginx: worker process
  6. 101 2099 2053 0 23:42 ? 00:00:00 nginx: worker process
  7. #容器ID查看
  8. [root@centos7-temp ~]# docker top 6950646e97f8
  9. UID PID PPID C STIME TTY TIME CMD
  10. root 2053 2033 0 23:42 ? 00:00:00 nginx: master process nginx -g daemon off;
  11. 101 2098 2053 0 23:42 ? 00:00:00 nginx: worker process
  12. 101 2099 2053 0 23:42 ? 00:00:00 nginx: worker process

 4.查看容器内的资源使用情况

格式:docker stats 容器名/容器ID

  1. #查看指定容器
  2. [root@centos7-temp ~]# docker stats nginx
  3. CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
  4. 6950646e97f8 nginx 0.00% 1.957MiB / 1.934GiB 0.10% 740B / 0B 0B / 0B 3
  5. [root@centos7-temp ~]# docker stats 6950646e97f8
  6. CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
  7. 6950646e97f8 nginx 0.00% 1.957MiB / 1.934GiB 0.10% 740B / 0B 0B / 0B 3
  8. #查看所有容器
  9. [root@centos7-temp ~]# docker stats
  10. CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
  11. 4b13c503d086 elasticsearch 0.30% 1.135GiB / 1.934GiB 58.72% 1.22kB / 984B 251MB / 1.62MB 44
  12. 6950646e97f8 nginx 0.00% 1.957MiB / 1.934GiB 0.10% 740B / 0B 0B / 0B 3

5.查看容器的详细信息

格式:docker inspect 容器名/容器ID

[root@centos7-temp ~]# docker inspect nginx

6.删除容器

格式:docker rm [选项] 容器名/容器ID

-f:强制删除正在运行的容器

-a:删除所有,包括运行和停止的

用法:

  1. #正在运行的容器需要加上-f选项强制删除
  2. [root@centos7-temp ~]# docker rm 6950646e97f8
  3. Error response from daemon: You cannot remove a running container 6950646e97f88ce338d4294c1bd6d088b5e5245709f49604a137ba5951879607. Stop the container before attempting removal or force remove
  4. [root@centos7-temp ~]# docker rm -f 6950646e97f8
  5. 6950646e97f8
  1. #删除所有容器
  2. [root@centos7-temp ~]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 4b13c503d086 elasticsearch:7.6.2 "/usr/local/bin/dock…" 20 minutes ago Up 20 minutes 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp elasticsearch
  5. e506a29205b6 mysql "docker-entrypoint.s…" 36 minutes ago Exited (1) 36 minutes ago kind_ardinghelli
  6. [root@centos7-temp ~]# docker rm -f `docker ps -a`
  7. [root@centos7-temp ~]# docker ps -a
  8. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  1. #删除所有正在运行的容器
  2. [root@centos7-temp ~]#docker rm -f `docker ps -q -f status=running`
  3. #删除所有停止运行的容器
  4. [root@centos7-temp ~]#docker rm `docker ps -qf status=exited`
  5. [root@centos7-temp ~]docker container prune -f

7.启动和停止容器

格式:

docker start 容器名/容器ID

docker stop 容器名/容器ID

docker restart 容器名/容器ID

docker pause 容器名/容器ID

docker unpause 容器名/容器ID

用法:

  1. #启动容器
  2. [root@centos7-temp ~]# docker ps
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. [root@centos7-temp ~]# docker start 730a80fdf57f
  5. 730a80fdf57f
  6. [root@centos7-temp ~]# docker ps
  7. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  8. 730a80fdf57f nginx "/docker-entrypoint.…" 3 days ago Up 5 seconds 80/tcp nginx1
  9. #停止容器
  10. [root@centos7-temp ~]# docker ps
  11. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  12. 730a80fdf57f nginx "/docker-entrypoint.…" 3 days ago Up About a minute 80/tcp nginx1
  13. [root@centos7-temp ~]# docker stop 730a80fdf57f
  14. 730a80fdf57f
  15. [root@centos7-temp ~]# docker ps
  16. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  17. #重启容器
  18. [root@centos7-temp ~]# docker ps -a
  19. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  20. 54218a657176 nginx "/docker-entrypoint.…" About a minute ago Exited (0) About a minute ago nginx1
  21. 7542f9a9e4ca busybox "sh" 3 days ago Exited (0) 3 days ago hardcore_taussig
  22. 7912c335a5e1 mysql "docker-entrypoint.s…" 3 days ago Exited (1) 3 days ago musing_bose
  23. [root@centos7-temp ~]# docker restart nginx1
  24. nginx1
  25. [root@centos7-temp ~]# docker ps
  26. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  27. 54218a657176 nginx "/docker-entrypoint.…" 2 minutes ago Up 5 seconds 80/tcp nginx1
  28. #暂停容器中的所有进程
  29. [root@centos7-temp ~]# docker pause nginx1
  30. nginx1
  31. #恢复容器中的所有进程
  32. [root@centos7-temp ~]# docker unpause nginx1
  33. nginx1

8.给正在运行的容器发信号

格式:docker kill 容器名/容器ID

用法:

  1. #默认发送关闭信号
  2. [root@centos7-temp ~]# docker ps
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 54218a657176 nginx "/docker-entrypoint.…" 15 minutes ago Up 12 minutes 80/tcp nginx1
  5. [root@centos7-temp ~]# docker kill nginx1
  6. nginx1
  7. [root@centos7-temp ~]# docker ps
  8. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  1. #强制关闭所有运行中的容器
  2. [root@centos7-temp ~]# docker ps
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 246349e5a3a8 busybox "sh" 7 seconds ago Up 5 seconds busybox1
  5. 54218a657176 nginx "/docker-entrypoint.…" 23 minutes ago Up 3 minutes 80/tcp nginx1
  6. [root@centos7-temp ~]# docker kill `docker ps -a -q`
  7. 246349e5a3a8
  8. 54218a657176
  9. Error response from daemon: Cannot kill container: eb90aedd7724: Container eb90aedd772498b8ca5090708f5e80a2fc6d51fc6ccbc46b6eb0438a520d4788 is not running
  10. Error response from daemon: Cannot kill container: b02457163726: Container b02457163726d73a1951ca6fe8a806ab7d309327321157f90924895419096702 is not running
  11. Error response from daemon: Cannot kill container: 7d9d15cdebcc: Container 7d9d15cdebccfbf257d132c62ad3011fae5fb13c12507fa0832cb9a36b10b242 is not running
  12. Error response from daemon: Cannot kill container: 73e5f7ffbd24: Container 73e5f7ffbd246b19cf714c26a8977665e329658540dd0d6bbfe350d69ee06598 is not running
  13. Error response from daemon: Cannot kill container: 7912c335a5e1: Container 7912c335a5e16a3d236d916eed47ea8b57738e228080d21e4a69b3c7ddf8b5cc is not running
  14. [root@centos7-temp ~]# docker ps
  15. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

9.进入正在运行的容器

(1)

#操作会在同一个容器的多个会话界面同步显示,所有使用此方式进入容器的操作都是同步显示的,且使用exit退出后容器自动关闭,不推荐使用,需要进入到有shell环境的容器

格式:docker attach 容器名/容器ID

用法:

  1. #进入正在运行的centos容器
  2. [root@centos7-temp ~]# docker ps
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 14e6aa0417f5 centos "/bin/bash" 5 seconds ago Up 5 seconds c1
  5. [root@centos7-temp ~]# docker attach c1
  6. #同时在第二个终端attach到容器,执行命令,两个终端同步显示结果
  7. [root@14e6aa0417f5 /]# cat /etc/redhat-release
  8. CentOS Linux release 8.4.2105
  9. #exit同时退出
  10. [root@14e6aa0417f5 /]# exit
  11. exit
  12. [root@centos7-temp ~]#
  1. #退出后,容器自动停止
  2. [root@centos7-temp ~]# docker ps
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 14e6aa0417f5 centos "/bin/bash" 19 minutes ago Up 2 minutes c1
  5. [root@centos7-temp ~]# docker attach c1
  6. [root@14e6aa0417f5 /]# exit
  7. exit
  8. [root@centos7-temp ~]# docker ps
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

 (2)

#在运行中的容器启动新进程,可以执行单次命令,以及进入容器测试环境使用此方式,使用exit退出,但容器还在运行,此为推荐方式

格式:docker exec -it 容器名/容器ID sh/bash

选项:-it:在容器中生成一个交互式的终端

用法:

  1. #执行一次性命令
  2. [root@centos7-temp ~]# docker exec c1 cat /etc/redhat-release
  3. CentOS Linux release 8.4.2105
  4. #进入容器内执行命令,exit退出,但容器不停止
  5. [root@centos7-temp ~]# docker ps
  6. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  7. 14e6aa0417f5 centos "/bin/bash" 19 minutes ago Up About a minute c1
  8. [root@centos7-temp ~]# docker exec -it c1 bash
  9. [root@14e6aa0417f5 /]# cat /etc/redhat-release
  10. CentOS Linux release 8.4.2105
  11. [root@14e6aa0417f5 /]# exit
  12. exit
  13. [root@centos7-temp ~]# docker ps
  14. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  15. 14e6aa0417f5 centos "/bin/bash" 19 minutes ago Up 2 minutes c1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/96156
推荐阅读
相关标签
  

闽ICP备14008679号