启动守护式容器(在后台运行)docker run -d+容器名

但有的容器在后台运行,必须要有一个前台进程,容器运行的命令如果不是一直挂起的命令,例如 top,tail,容器就会自动退出,
类似于nginx,启动nginx只需要 service nginx start就可以了,但如果让nginx为后台进程模式运行,前台没有实际运行的应用,容器启动后会立即自杀,docker机制认为它没事可做了。所以最好的做法是让要运行的程序以前台进程的形式运行。

用守护式进程运行centos有没有必要?没必要,因为运行一个centos是像一个开关打开了centos,直接交互就可以了,如果用 docker run -d -it centos运行的话,这样不会进容器交互窗口,也不会让centos容器被docker杀死,但这样做没有意义,因为运行了centos势必要对系统文件进行编辑,没有必要在后台运行。
docker exec -it centos的容器号 bin/bash 就可以进入了

  1. [root@test 12345]# docker run -d centos
  2. d22ffbc3707c44de4f8e639b9477a5e899e4d885c2a186443aa6f7d4d5ea41ce
  3. [root@test 12345]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. [root@test 12345]#
  6. //以 docker run -it 7777:8080 tomcat 运行tomcat的话,会产生一个前台交互窗口
  7. //以 docker run -d 7777:8080 tomcat 运行tomcat的话,会以守护式容器的形式在后台运行,因为tomcat容器是一直会挂起运行的,不会被当做事情做完了被docker杀掉

查看容器日志 docker logs -f -t —tail +容器id

-t 日志的时间戳 -f 循环打印 -n 3 查看倒数3行

鲸鱼背上有多个容器,用于查看每个容器的日志。
Tip: while true 表示一直执行 while循环一节中已经学过

  1. [root@test 12345]# docker run -d centos /bin/sh -c "while true;do echo zzyy;sleep 2;done"
  2. 2df8352b191095c051094e736239dc3695a5b11dcee49ede80913c1fa6f183ed
  3. [root@test 12345]# docker ps //只有给后台运行的容器命令运行,后台的程序才会显示在线up
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 8c3e9246792b centos "/bin/sh -c 'while t…" 36 seconds ago Up 33 seconds compassionate_shannon
  6. [root@test 12345]# docker logs -f -t -n 3 8c3e9246792b //不断循环打印最后3行带有时间戳的日志
  7. 2021-01-14T00:53:00.202896885Z zzyy
  8. 2021-01-14T00:53:02.219291052Z zzyy
  9. 2021-01-14T00:53:04.222945964Z zzyy
  10. 2021-01-14T00:53:06.226363144Z zzyy

查看容器内的进程

docker top 容器id

  1. [root@test 12345]# docker top 8c3e9246792b
  2. UID PID PPID C STIME TTY TIME CMD
  3. root 53493 53303 0 16:52 ? 00:00:00 /bin/sh -c while true;do echo zzyy;sleep 2;done
  4. root 130653 53493 0 16:56 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 2

查看容器内部的细节—后续重点掌握

docker inspect 容器id
容器像一个花卷,是一层套一层的。json串形式嵌套的

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

docker attach 容器id 进入容器不执行新的进程

CTRL+P+Q退出容器后,又要进入正在运行的容器,并以命令行交互

  1. [root@test 12345]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. tomcat latest feba8d001e3f 3 weeks ago 649MB
  4. centos latest 300e315adb2f 5 weeks ago 209MB
  5. [root@test 12345]# docker run -it centos
  6. [root@9a5ab0c33777 /]# CTRL+P+Q退出容器
  7. [root@test 12345]# docker ps
  8. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  9. 9a5ab0c33777 centos "/bin/bash" 12 minutes ago Up 12 minutes stoic_kepler
  10. [root@test 12345]# docker attach 9a5ab0c33777 //进入容器
  11. [root@9a5ab0c33777 /]# ls -l /tmp
  12. total 8
  13. -rwx------. 1 root root 701 Dec 4 17:37 ks-script-esd4my7v
  14. -rwx------. 1 root root 671 Dec 4 17:37 ks-script-eusq_sc5

docker exec 执行容器中的进程

docker exec -it 容器id ls -l /tmp 执行容器中的 ls -l /tmp命令,但并没进入容器,相当于隔山打牛

  1. [root@test 12345]# docker exec -t 9a5ab0c33777 ls -l /tmp
  2. total 8
  3. -rwx------. 1 root root 701 Dec 4 17:37 ks-script-esd4my7v
  4. -rwx------. 1 root root 671 Dec 4 17:37 ks-script-eusq_sc5

docker exec -it 9a5ab0c33777 /bin/bash = docker attach 9a5ab0c33777

exec 功能更为强大一些

  1. [root@test 12345]# docker exec -t 9a5ab0c33777 /bin/bash
  2. [root@9a5ab0c33777 /]#

复制docker中的文件到主机
docker cp 容器id:文件路径 主机路径

  1. [root@test 12345]# docker cp 9a5ab0c33777 /tmp/ks-script-eusq_sc5 /root
  2. "docker cp" requires exactly 2 arguments.
  3. See 'docker cp --help'.
  4. //容器后面没加冒号报错
  5. Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
  6. docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
  7. Copy files/folders between a container and the local filesystem
  8. //复制docker种的文件到主机路径下
  9. [root@test 12345]# docker cp 9a5ab0c33777:/tmp/ks-script-eusq_sc5 /root
  10. [root@test 12345]# ls
  11. Desktop Documents Downloads Music Pictures Public Templates Videos
  12. [root@test 12345]# cd /root
  13. [root@test ~]# ls
  14. anaconda-ks.cfg initial-setup-ks.cfg ks-script-eusq_sc5 original-ks.cfg
  15. [root@test ~]#