Docker安装: https://docs.docker.com/engine/install/centos/

创建交互式容器

  1. # 运行docker容器, 退出则容器停止运行
  2. docker run --name ubuntu-test -it ubuntu /bin/bash
  3. --name 指定名称
  4. -it 为新建的容器提供一个交互式shell
  5. # 启动容器
  6. docker start ubuntu-test
  7. # 附着到运行中的容器上, 退出则容器停止运行
  8. docker attach ubuntu-test

创建守护式容器

  1. # 后台运行容器
  2. docker run --name ubuntu-test2 -d -p 81:80 ubuntu /bin/bash -c "while true;do echo hello world; sleep 1; done"
  3. -d 后台运行容器
  4. -p 指定端口映射(宿主机:容器)
  5. # 获取容器日志
  6. docker logs -ft ubuntu-test2
  7. -f 跟踪最新日志, 也可以写成"docker logs --tail 100f ubuntu-test2"
  8. -t 为日志加上时间戳
  9. # 查看容器内的进程
  10. docker top ubuntu-test2
  11. # 查看一个或多个容器的统计信息(CPU, 内存, 网络IO, 存储IO)
  12. docker stats ubuntu-test2 ubuntu-test3
  13. # 在容器内部运行进程
  14. # 以后台任务模式在容器内创建文件
  15. docker exec -d ubuntu-test2 touch /etc/a.txt
  16. #以交互式任务模式在容器中启动一个shell
  17. docker exec -it ubuntu-test2 /bin/bash
  18. # 停止容器, 后接名称或ID
  19. docker stop ubuntu-test2
  20. # 查看最近x个容器, 不论运行还是停止
  21. docker ps -n x
  22. # 删除容器
  23. docker rm ubuntu-test2
  24. # 删除所有容器, -aq 返回所有容器的ID列表
  25. docker rm `docker ps -aq`

docker日志驱动

  1. docker run --log-driver="syslog" --name ubuntu-test3 -d ubuntu /bin/bash -c "while true;do echo hello world; sleep 1; done"
  2. --log-driver 可以指定日志驱动选项
  3. 1.默认"json-file", docker logs提供基础
  4. 2."syslog"选项, 禁用docker logs, 可以在启动Docker守护进程时指定该选项, 将所有容器日志输出都重定向到Syslog, 或者通过docker run 对个别容器进行日志重定向输出
  5. 3."none"选项, 禁用所有日志

深入容器

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