这里的容器可以理解为,以镜像为模板,生成的各种容器。

5.1 启动容器

  1. docker run [可选参数] image
  2. # 参数
  3. -- name="容器名称"
  4. -d # 以后台方式运行
  5. -it # 以交互式方式运行
  6. -p # 端口
  7. # 使用格式: 主机端口:容器端口
  8. -P # 随机指定端口(大写)

image.png

5.2 退出容器

  1. exit # 停止并退出容器
  2. Ctrl+p+q # 退出但不停止容器
  3. ╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░────────────────────────────────────────────────────░▒▓ took 4s at 16:04:59 ▓▒░
  4. ╰─ docker ps
  5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  6. c338b3c19b4d centos "/bin/bash" 9 seconds ago Up 7 seconds youthful_villani

5.3 查看运行的容器

  1. ╰─ docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. ╭─░▒▓ ~
  1. ╰─ docker ps --help
  2. Usage: docker ps [OPTIONS]
  3. List containers
  4. Options:
  5. -a, --all Show all containers (default shows just running)
  6. -f, --filter filter Filter output based on conditions provided
  7. --format string Pretty-print containers using a Go template
  8. -n, --last int Show n last created containers (includes all states) (default -1)
  9. -l, --latest Show the latest created container (includes all states)
  10. --no-trunc Don't truncate output
  11. -q, --quiet Only display container IDs
  12. -s, --size Display total file sizes
  13. ╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ ✔  at 16:01:26 ▓▒░
  14. ╰─

如果携带参数 a 则列出历史运行容器

  1. ╰─ docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 2ee64876800b centos "/bin/bash" 9 minutes ago Exited (0) About a minute ago hopeful_roentgen
  4. ╰─ docker ps -aq
  5. 2ee64876800b

5.4 删除容器

  1. docker rm centos
  2. ╰─ docker rm -f c338b3c19b4d
  3. c338b3c19b4d
  4. # 删除所有容器
  5. docker rm -f $(docker ps -aq)
  6. ╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ at 16:06:56 ▓▒░
  7. ╰─ docker rm -f $(docker ps -aq)
  8. 2ee64876800b
  9. docker ps -a -q|xargs docker rm

5.5 启动和停止容器

  1. docker start 容器id # 启动容器
  2. docker restart 容器id # 重启容器
  3. docker stop 容器id # 停止
  4. docker kill 容器id # 强制停止

5.6 后台启动容器

  1. ╰─ docker run -d centos
  2. 4b303d957914ddc63436af5148641b88fcf64b5d62bf5ff05d9a5e0f28ed66ee
  3. ╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ at 16:11:09 ▓▒░
  4. ╰─ docker ps
  5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  6. ╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ at 16:11:15 ▓▒░
  7. ╰─ docker ps -a
  8. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  9. 4b303d957914 centos "/bin/bash" 9 seconds ago Exited (0) 8 seconds ago ecstatic_hermann
  10. ╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ at 16:11:18 ▓▒░
  11. ╰─

启动后发现立马停掉,因为docker使用后台运行,必须要有一个前台进程,否则docker会停止。

5.7 查看日志

docker logs

  1. ╰─ docker logs --help
  2. Usage: docker logs [OPTIONS] CONTAINER
  3. Fetch the logs of a container
  4. Options:
  5. --details Show extra details provided to logs
  6. -f, --follow Follow log output
  7. --since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
  8. -n, --tail string Number of lines to show from the end of the logs (default "all")
  9. -t, --timestamps Show timestamps
  10. --until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
  11. ╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ at 16:13:11 ▓▒░
  12. ╰─
  1. docker -tf --tail 10 容器id # 持续输出查看前10条日志内容

5.8 容器进程信息

  1. docker top
  2. ╰─ docker top 6e956ce062dc
  3. UID PID PPID C STIME TTY TIME CMD
  4. root 3084 3058 0 08:22 ? 00:00:00 /bin/bash
  5. ╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ at 16:24:24 ▓▒░
  6. ╰─
  • UID 用户ID
  • PID 进程ID
  • PPID 父进程ID

    5.9 查看容器详细信息

    ```shell docker inspect

╰─ docker inspect —help

Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID…]

Return low-level information on Docker objects

Options: -f, —format string Format the output using the given Go template -s, —size Display total file sizes if the type is container —type string Return JSON for specified type ╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ ✔  at 16:25:36 ▓▒░ ╰─

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

5.10 进入当前正在运行的容器

  1. # 进入正在后台运行的容器
  2. docker exec -it 容器id /bin/bash
  3. ╰─ docker exec -it 6e956ce062dc /bin/bash
  4. [root@6e956ce062dc /]#
  5. docker attach 容器id
  6. ╰─ docker attach 6e956ce062dc
  7. [root@6e956ce062dc /]#

exec 和 attach区别

  • exec 进入容器后开启一个新的会话
  • attach 继续当前会话

5.11 容器拷贝文件到主机

docker cp 容器id:容器路径 主机路径

  1. docker cp
  2. ╰─ docker cp --help
  3. Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
  4. docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
  5. Copy files/folders between a container and the local filesystem
  6. Use '-' as the source to read a tar archive from stdin
  7. and extract it to a directory destination in a container.
  8. Use '-' as the destination to stream a tar archive of a
  9. container source to stdout.
  10. Options:
  11. -a, --archive Archive mode (copy all uid/gid information)
  12. -L, --follow-link Always follow symbol link in SRC_PATH
  13. ╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ at 16:34:30 ▓▒░
  14. ╰─
  1. ╭─░▒▓ /home ▓▒░─────────────────────────────────────────────────────────────────────────────░▒▓ 1 at 16:38:16 ▓▒░
  2. ╰─ docker cp 7834712eefa5:/home/log.log ~/Documents/
  3. ╭─░▒▓ /home ▓▒░───────────────────────────────────────────────────────────────────────────────░▒▓ at 16:39:28 ▓▒░
  4. ╰─ cd ~/Documents
  5. ╭─░▒▓ ~/Documents ▓▒░───────────────────────────────────────────────────────────────────────────░▒▓ at 16:39:37 ▓▒░
  6. ╰─ ls
  7. 21-05-12-14-52-35_1.gif google-extensions themes
  8. 21 install.sh vnote导出
  9. 21级.zip iterm2Theme 草稿纸-81.jpg
  10. Adobe leanote.dockerfile 未命名.gif
  11. Apifox log.log 数学重点
  12. Axure lzfblog-cn-nginx-0728230452.zip 考前突击(模拟卷).xmind
  13. MWebLibrary mbadolato-iTerm2-Color-Schemes-7f73027 笛卡尔积.key
  14. MuMu共享文件夹 mysql.opml 线性代数.pdf
  15. Profie mysql.svg 软件工程.xmind
  16. SiYuan mysql基础.xmind 期末知识点.xmind
  17. ToDesk quickstart-app 毕业登记表p1.pdf
  18. docs score.sql 专升本数学考点.xmind
  19. fonts scripts
  20. ╭─░▒▓ ~/Documents ▓▒░───────────────────────────────────────────────────────────────────────────░▒▓ at 16:39:38 ▓▒░
  21. ╰─