帮助命令

  1. docker version #显示docker的版本信息
  2. docker info #显示docker的系统信息,包括镜像和容器的数量等
  3. docker 命令 --help #显示该命令的帮助命令

帮助文档的地址:https://docs.docker.com/engine/reference/commandline/dockerd/

镜像命令

docker images 查看所有本地的主机上的镜像

  1. [root@localhost ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. mysql 5.7 9f1d21c1025a 4 days ago 448MB
  4. hello-world latest d1165f221234 4 months ago 13.3kB
  5. cptactionhank/atlassian-jira-software 8.1.0 f9f09151d8df 2 years ago 531MB
  6. #解释
  7. REPOSITORY 镜像的仓库
  8. TAG 镜像的版本
  9. IMAGE ID 镜像的唯一标识
  10. CREATED 镜像创建时间
  11. SIZE 镜像的大小
  12. #可选项
  13. [root@localhost ~]# docker images --help
  14. Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
  15. List images
  16. Options:
  17. -a, --all #列出所有的镜像
  18. --digests Show digests
  19. -f, --filter filter Filter output based on conditions provided
  20. --format string Pretty-print images using a Go template
  21. --no-trunc Don't truncate output
  22. -q, --quiet #只显示镜像的ID

docker search 搜索镜像

  1. [root@localhost ~]# docker search mysql
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. mysql MySQL is a widely used, open-source relation 11164 [OK]
  4. mariadb MariaDB Server is a high performing open sou 4235 [OK]
  5. #可选项 通过搜查来过滤
  6. --filter=STARS=5000 #搜索出来的镜像就是STARTS大于5000的
  7. --[root@localhost ~]# docker search --help
  8. Usage: docker search [OPTIONS] TERM
  9. Search the Docker Hub for images
  10. Options:
  11. -f, --filter filter Filter output based on conditions provided
  12. --format string Pretty-print search using a Go template
  13. --limit int Max number of search results (default 25)
  14. --no-trunc Don't truncate output
  15. [root@localhost ~]# docker search mysql --filter=STARS=5000
  16. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  17. mysql MySQL is a widely used, open-source relation… 11164 [OK]

docker pull 下载命令

  1. #下载镜像 docker pull 镜像名字[:tag]
  2. [root@localhost ~]# docker pull mysql
  3. Using default tag: latest #如果不写tag,默认就是latest最新版本
  4. latest: Pulling from library/mysql
  5. 33847f680f63: Pull complete #分层下载,docker image 的核心 联合文件系统
  6. 5cb67864e624: Pull complete
  7. 1a2b594783f5: Pull complete
  8. b30e406dd925: Pull complete
  9. 48901e306e4c: Pull complete
  10. 603d2b7147fd: Pull complete
  11. 802aa684c1c4: Pull complete
  12. 715d3c143a06: Pull complete
  13. 6978e1b7a511: Pull complete
  14. f0d78b0ac1be: Pull complete
  15. 35a94d251ed1: Pull complete
  16. 36f75719b1a9: Pull complete
  17. Digest: sha256:8b928a5117cf5c2238c7a09cd28c2e801ac98f91c3f8203a8938ae51f14700fd #签名
  18. Status: Downloaded newer image for mysql:latest
  19. docker.io/library/mysql:latest #真实地址
  20. #两个命令等价
  21. docker pull mysql
  22. docker pull docker.io/library/mysql:latest
  23. #指定版本下载
  24. [root@localhost ~]# docker pull mysql:5.7
  25. 5.7: Pulling from library/mysql
  26. 33847f680f63: Already exists
  27. 5cb67864e624: Already exists
  28. 1a2b594783f5: Already exists
  29. b30e406dd925: Already exists
  30. 48901e306e4c: Already exists
  31. 603d2b7147fd: Already exists
  32. 802aa684c1c4: Already exists
  33. 5b5a19178915: Pull complete
  34. f9ce7411c6e4: Pull complete
  35. f51f6977d9b2: Pull complete
  36. aeb6b16ce012: Pull complete
  37. Digest: sha256:be70d18aedc37927293e7947c8de41ae6490ecd4c79df1db40d1b5b5af7d9596
  38. Status: Downloaded newer image for mysql:5.7
  39. docker.io/library/mysql:5.7
  40. #前面的内容可以看出是已经存在的文件,就不需要再次下载,只需更新新的内容,极大的节省内存,这就是联合文件系统

docker rmi 删除镜像

  1. [root@localhost ~]# docker rmi -f d1165f221234
  2. Untagged: hello-world:latest
  3. Untagged: hello-world@sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
  4. Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
  5. docker rmi -f 镜像ID #删除指定的镜像
  6. docker rmi -f 容器ID 容器ID 容器ID #删除多个镜像
  7. docker rmi -f $(docker images -aq) #删除全部的镜像

容器命令

我们有了镜像才可以创建容器,下载一个centos

  1. [root@localhost ~]# docker pull centos
  2. Using default tag: latest
  3. latest: Pulling from library/centos
  4. 7a0437f04f83: Pull complete
  5. Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
  6. Status: Downloaded newer image for centos:latest
  7. docker.io/library/centos:latest

新建容器并启动

  1. docker run [可选参数] image
  2. #参数说明
  3. --name="Name" 容器名字 tomcat01 tomcat02 ,用来区分容器
  4. -d 后台方式运行
  5. -it 使用交互方式运行,进入容器查看内容
  6. -p 指定容器的端口 -p 8080:8080
  7. -p ip:主机端口:容器端口
  8. -p 主机端口:容器端口(常用)
  9. -p 容器端口
  10. -P 随机指定端口
  11. #测试一下 启动并进入容器
  12. [root@localhost ~]# docker run -it centos /bin/bash
  13. [root@0f6efcd405d9 /]# ls #查看容器内的centos
  14. bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
  15. #从容器中退出到主机
  16. [root@0f6efcd405d9 /]# exit
  17. exit
  18. [root@localhost ~]# ls
  19. anaconda-ks.cfg jira-master.zip xampp-linux-x64-7.4.14-0-installer.run.1
  20. atlassian-jira-software-7.2.2-x64.bin xampp-linux-x64-7.4.14-0-installer.run

列出所有的运行的容器

  1. #docker ps 命令
  2. #列出当前正在运行的容器
  3. -a #列出当前正在运行的容器 + 带出历史运行的容器
  4. -n=? #显示最近创建的容器
  5. -q #只显示容器的编号
  6. [root@localhost ~]# docker ps
  7. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  8. [root@localhost ~]# docker ps -a
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  10. 0f6efcd405d9 centos "/bin/bash" 4 minutes ago Exited (0) About a minute ago strange_grothendieck
  11. 1e87b2e797e1 cptactionhank/atlassian-jira-software:8.1.0 "/docker-entrypoint.…" 46 hours ago Exited (255) 59 minutes ago 0.0.0.0:8090->8080/tcp, :::8090->8080/tcp jira-software
  12. 1853c568ab06 9f1d21c1025a "docker-entrypoint.s…" 46 hours ago Exited (255) 59 minutes ago 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
  13. c5812bde1a92 d1165f221234 "/hello" 3 days ago Exited (0) 3 days ago vibrant_cartwright
  14. ba84d1187514 d1165f221234 "/hello" 5 days ago Exited (0) 5 days ago condescending_mahavira

退出容器

  1. exit #直接容器停止并退出
  2. Ctrl + P + Q #容器不停止退出
  3. [root@localhost ~]# docker ps #当前没有容器运行
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. [root@localhost ~]# docker run -it centos /bin/bash #容器不停止退出
  6. [root@cd37c8887b62 /]# [root@localhost ~]# docker ps #可以看到当前正在运行的容器
  7. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  8. cd37c8887b62 centos "/bin/bash" 21 seconds ago Up 20 seconds clever_fermat

删除容器

  1. docker rm 容器ID #删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm-f
  2. docker rm -f $(docker ps -aq) #删除所有的容器
  3. docker ps -a -q|xargs docker rm #删除所有的容器
  4. [root@localhost ~]# docker rm cd37c8887b62
  5. Error response from daemon: You cannot remove a running container cd37c8887b6241953c518df86764812a24751d92853e759837e98b5153f2840e. Stop the container before attempting removal or force remove
  6. [root@localhost ~]# docker rm -f cd37c8887b62
  7. cd37c8887b62

启动和停止容器的操作

  1. docker start 容器ID #启动容器
  2. docker restart 容器ID #重启容器
  3. docker stop 容器ID #停止当前正在运行的容器
  4. docker kill 容器ID #强制停止当前容器

常用其他命令

后台启动容器

  1. #docker run -d 镜像名
  2. [root@localhost ~]# docker ps
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. [root@localhost ~]# docker run -d centos
  5. 579465de4963353554e1bd2af04782c263223f674552c329b555336ad22083cc
  6. [root@localhost ~]# docker ps
  7. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  8. 从上面可以发现问题:在使用命令后台启动centos之后,docker ps 发现没有运行的容器
  9. centos停止运行了?
  10. 为什么?
  11. 常见的坑:docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止

查看日志

  1. docker logs -f -t --taii 容器,发现容器没有日志
  2. #自己编写一段shell脚本
  3. [root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo hello,world;sleep 1;done"
  4. 94a80050dc4b064efc64a1f01b1b646e6e778b9afd7de8439f29e8102e3bec9f
  5. [root@localhost ~]# docker ps
  6. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  7. 94a80050dc4b centos "/bin/sh -c 'while t…" 29 seconds ago Up 28 seconds nervous_kare
  8. [root@localhost ~]# docker logs -tf --tail 10 94a80050dc4b
  9. 2021-07-24T09:45:35.957645789Z hello,world
  10. 2021-07-24T09:45:36.962548024Z hello,world
  11. 2021-07-24T09:45:37.967424222Z hello,world
  12. 2021-07-24T09:45:38.970952228Z hello,world
  13. #显示日志
  14. -tf #显示日志 t 显示时间戳
  15. --tail number #要显示的日志条数

查看容器中进程信息

  1. [root@localhost ~]# docker top 94a80050dc4b
  2. UID PID PPID C STIME TTY TIME CMD
  3. root 3914 3895 0 17:44 ? 00:00:00 /bin/sh -c while true;do echo hello,world;sleep 1;done
  4. root 4430 3914 0 17:50 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
  5. [root@localhost ~]#

查看容器中的元数据

  1. docker inspect 容器id
  2. [root@localhost ~]# docker inspect 94a80050dc4b
  3. [
  4. {
  5. "Id": "94a80050dc4b064efc64a1f01b1b646e6e778b9afd7de8439f29e8102e3bec9f", #可以看到这里 我们容器ID的就是这里容器id取了前几位
  6. "Created": "2021-07-24T09:44:30.154427774Z",
  7. "Path": "/bin/sh", #容器的启动路径
  8. "Args": [
  9. "-c",
  10. "while true;do echo hello,world;sleep 1;done" #刚刚那个手写的脚本 一直循环输入hello world 每一秒
  11. ],
  12. "State": {
  13. "Status": "running",
  14. "Running": true,
  15. "Paused": false,
  16. "Restarting": false,
  17. "OOMKilled": false,
  18. "Dead": false,
  19. "Pid": 3914,
  20. "ExitCode": 0,
  21. "Error": "",
  22. "StartedAt": "2021-07-24T09:44:30.626810771Z",
  23. "FinishedAt": "0001-01-01T00:00:00Z"
  24. },
  25. "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
  26. "ResolvConfPath": "/var/lib/docker/containers/94a80050dc4b064efc64a1f01b1b646e6e778b9afd7de8439f29e8102e3bec9f/resolv.conf",
  27. "HostnamePath": "/var/lib/docker/containers/94a80050dc4b064efc64a1f01b1b646e6e778b9afd7de8439f29e8102e3bec9f/hostname",
  28. "HostsPath": "/var/lib/docker/containers/94a80050dc4b064efc64a1f01b1b646e6e778b9afd7de8439f29e8102e3bec9f/hosts",
  29. "LogPath": "/var/lib/docker/containers/94a80050dc4b064efc64a1f01b1b646e6e778b9afd7de8439f29e8102e3bec9f/94a80050dc4b064efc64a1f01b1b646e6e778b9afd7de8439f29e8102e3bec9f-json.log",
  30. "Name": "/nervous_kare",
  31. "RestartCount": 0,
  32. "Driver": "overlay2",
  33. "Platform": "linux",
  34. "MountLabel": "",
  35. "ProcessLabel": "",
  36. "AppArmorProfile": "",
  37. "ExecIDs": null,
  38. "HostConfig": {
  39. "Binds": null,
  40. "ContainerIDFile": "",
  41. "LogConfig": {
  42. "Type": "json-file",
  43. "Config": {
  44. "max-size": "100m"
  45. }
  46. },
  47. "NetworkMode": "default",
  48. "PortBindings": {},
  49. "RestartPolicy": {
  50. "Name": "no",
  51. "MaximumRetryCount": 0
  52. },
  53. "AutoRemove": false,
  54. "VolumeDriver": "",
  55. "VolumesFrom": null,
  56. "CapAdd": null,
  57. "CapDrop": null,
  58. "CgroupnsMode": "host",
  59. "Dns": [],
  60. "DnsOptions": [],
  61. "DnsSearch": [],
  62. "ExtraHosts": null,
  63. "GroupAdd": null,
  64. "IpcMode": "private",
  65. "Cgroup": "",
  66. "Links": null,
  67. "OomScoreAdj": 0,
  68. "PidMode": "",
  69. "Privileged": false,
  70. "PublishAllPorts": false,
  71. "ReadonlyRootfs": false,
  72. "SecurityOpt": null,
  73. "UTSMode": "",
  74. "UsernsMode": "",
  75. "ShmSize": 67108864,
  76. "Runtime": "runc",
  77. "ConsoleSize": [
  78. 0,
  79. 0
  80. ],
  81. "Isolation": "",
  82. "CpuShares": 0,
  83. "Memory": 0,
  84. "NanoCpus": 0,
  85. "CgroupParent": "",
  86. "BlkioWeight": 0,
  87. "BlkioWeightDevice": [],
  88. "BlkioDeviceReadBps": null,
  89. "BlkioDeviceWriteBps": null,
  90. "BlkioDeviceReadIOps": null,
  91. "BlkioDeviceWriteIOps": null,
  92. "CpuPeriod": 0,
  93. "CpuQuota": 0,
  94. "CpuRealtimePeriod": 0,
  95. "CpuRealtimeRuntime": 0,
  96. "CpusetCpus": "",
  97. "CpusetMems": "",
  98. "Devices": [],
  99. "DeviceCgroupRules": null,
  100. "DeviceRequests": null,
  101. "KernelMemory": 0,
  102. "KernelMemoryTCP": 0,
  103. "MemoryReservation": 0,
  104. "MemorySwap": 0,
  105. "MemorySwappiness": null,
  106. "OomKillDisable": false,
  107. "PidsLimit": null,
  108. "Ulimits": null,
  109. "CpuCount": 0,
  110. "CpuPercent": 0,
  111. "IOMaximumIOps": 0,
  112. "IOMaximumBandwidth": 0,
  113. "MaskedPaths": [
  114. "/proc/asound",
  115. "/proc/acpi",
  116. "/proc/kcore",
  117. "/proc/keys",
  118. "/proc/latency_stats",
  119. "/proc/timer_list",
  120. "/proc/timer_stats",
  121. "/proc/sched_debug",
  122. "/proc/scsi",
  123. "/sys/firmware"
  124. ],
  125. "ReadonlyPaths": [
  126. "/proc/bus",
  127. "/proc/fs",
  128. "/proc/irq",
  129. "/proc/sys",
  130. "/proc/sysrq-trigger"
  131. ]
  132. },
  133. "GraphDriver": {
  134. "Data": {
  135. "LowerDir": "/var/lib/docker/overlay2/dd4bfbc3d94f5ba7595ffec677271b7e7ea12ac16e8deadc9557737ced4155b4-init/diff:/var/lib/docker/overlay2/57444df837df39ca7b692f700d46e99c9ac472d0a79463398c5c1baffb11e9d0/diff",
  136. "MergedDir": "/var/lib/docker/overlay2/dd4bfbc3d94f5ba7595ffec677271b7e7ea12ac16e8deadc9557737ced4155b4/merged",
  137. "UpperDir": "/var/lib/docker/overlay2/dd4bfbc3d94f5ba7595ffec677271b7e7ea12ac16e8deadc9557737ced4155b4/diff",
  138. "WorkDir": "/var/lib/docker/overlay2/dd4bfbc3d94f5ba7595ffec677271b7e7ea12ac16e8deadc9557737ced4155b4/work"
  139. },
  140. "Name": "overlay2"
  141. },
  142. "Mounts": [],
  143. "Config": {
  144. "Hostname": "94a80050dc4b",
  145. "Domainname": "",
  146. "User": "",
  147. "AttachStdin": false,
  148. "AttachStdout": false,
  149. "AttachStderr": false,
  150. "Tty": false,
  151. "OpenStdin": false,
  152. "StdinOnce": false,
  153. "Env": [
  154. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  155. ],
  156. "Cmd": [
  157. "/bin/sh",
  158. "-c",
  159. "while true;do echo hello,world;sleep 1;done"
  160. ],
  161. "Image": "centos",
  162. "Volumes": null,
  163. "WorkingDir": "",
  164. "Entrypoint": null,
  165. "OnBuild": null,
  166. "Labels": {
  167. "org.label-schema.build-date": "20201204",
  168. "org.label-schema.license": "GPLv2",
  169. "org.label-schema.name": "CentOS Base Image",
  170. "org.label-schema.schema-version": "1.0",
  171. "org.label-schema.vendor": "CentOS"
  172. }
  173. },
  174. "NetworkSettings": {
  175. "Bridge": "",
  176. "SandboxID": "9e0b6936f4fa99b01c7803e6fa505807eec0ade1bdf48ef929e68dd1e365eb09",
  177. "HairpinMode": false,
  178. "LinkLocalIPv6Address": "",
  179. "LinkLocalIPv6PrefixLen": 0,
  180. "Ports": {},
  181. "SandboxKey": "/var/run/docker/netns/9e0b6936f4fa",
  182. "SecondaryIPAddresses": null,
  183. "SecondaryIPv6Addresses": null,
  184. "EndpointID": "4e44f3eaad9d60a39b1885d26a6b7d7bb2ffd906bbde6aff3c083e7f80065482",
  185. "Gateway": "172.17.0.1",
  186. "GlobalIPv6Address": "",
  187. "GlobalIPv6PrefixLen": 0,
  188. "IPAddress": "172.17.0.3",
  189. "IPPrefixLen": 16,
  190. "IPv6Gateway": "",
  191. "MacAddress": "02:42:ac:11:00:03",
  192. "Networks": {
  193. "bridge": {
  194. "IPAMConfig": null,
  195. "Links": null,
  196. "Aliases": null,
  197. "NetworkID": "517171546df0fe9e01080f2fdfd4183ebcaba019655f8cec73ef220eb2b39eaa",
  198. "EndpointID": "4e44f3eaad9d60a39b1885d26a6b7d7bb2ffd906bbde6aff3c083e7f80065482",
  199. "Gateway": "172.17.0.1",
  200. "IPAddress": "172.17.0.3",
  201. "IPPrefixLen": 16,
  202. "IPv6Gateway": "",
  203. "GlobalIPv6Address": "",
  204. "GlobalIPv6PrefixLen": 0,
  205. "MacAddress": "02:42:ac:11:00:03",
  206. "DriverOpts": null
  207. }
  208. }
  209. }
  210. }
  211. ]

进入当前正在运行的容器

  1. #我们通常容器都是使用后台方式运行的,需要进入容器修改一些配置时
  2. #命令
  3. docker exec -it 容器id BashShell
  4. #测试
  5. [root@localhost ~]# docker ps
  6. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  7. 51fa8fe0d85b centos "/bin/bash" 20 seconds ago Up 19 seconds quirky_elion
  8. [root@localhost ~]# docker exec -it 51fa8fe0d85b /bin/bash
  9. [root@51fa8fe0d85b /]# ls
  10. bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
  11. [root@51fa8fe0d85b /]# ps -ef
  12. UID PID PPID C STIME TTY TIME CMD
  13. root 1 0 0 09:14 pts/0 00:00:00 /bin/bash
  14. root 15 0 0 09:15 pts/1 00:00:00 /bin/bash
  15. root 32 15 0 09:16 pts/1 00:00:00 ps -ef
  16. #方式二
  17. docker attach 容器id
  18. [root@localhost ~]# docker attach 51fa8fe0d85b
  19. #docker exec #进入容器后开启一个新的终端
  20. #docker attach #进入容器正在执行的终端,不会启动新的进程!

从容器内拷贝文件到主机上

  1. docker cp 容器id
  2. [root@localhost home]# docker attach 2075c5dd8b33 #进入docker容器内部
  3. [root@2075c5dd8b33 /]# cd /home
  4. [root@2075c5dd8b33 home]# ls
  5. #在容器内新建一个文件
  6. [root@2075c5dd8b33 home]# touch test.java
  7. [root@2075c5dd8b33 home]# exit
  8. exit
  9. #将文件拷贝出来到主机上
  10. [root@localhost home]# docker cp 2075c5dd8b33:/home/test.java /home
  11. [root@localhost home]# ls
  12. hello.java jira test.java
  13. #拷贝是一个手动过程,未来使用-v 卷的技术,可以实现,自动同步