• Docker--查看容器的启动参数(命令)--方法/实例


    原文网址:Docker--查看容器的启动参数(命令)--方法/实例_IT利刃出鞘的博客-CSDN博客

    简介

    说明

    本文介绍如何查看容器的启动命令。

    原命令

    本处我先创建一个redis容器,命令如下:

    1. docker run -p 6379:6379 --name container_redis \
    2. -v /work/docker/redis/config:/etc/redis/config \
    3. -v /work/docker/redis/data:/data \
    4. -d redis redis-server /etc/redis/config/redis.conf

    创建好之后如下:

    1. knife@ubuntu:~$ docker ps -a
    2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    3. 3cc96a16754f redis "docker-entrypoint.s…" 12 months ago Exited (0) 3 months ago container_redis

    方法总结

    1. docker ps -a --no-trunc
      1. 信息少
      2. 无法重现命令
    2. docker inspect
      1. json格式,信息多
      2. 无法重现命令
    3. runlike
      1. 第三方包,信息多
      2. 不能重现命令
    4. get_command_4_run_container(推荐
      1. 第三方包
      2. 几乎完美重现命令

    法1:docker ps -a --no-trunc(信息少)

    说明

    需要sudo去执行。

    执行命令并查看结果

    执行命令:

    sudo docker ps -a --no-trunc

    结果 

    1. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    2. 3cc96a16754f761fdff4eb04a8b87672db8dc33f7c09ddb978446bfcc2b970c1 redis "docker-entrypoint.sh redis-server /etc/redis/config/redis.conf" 12 months ago Exited (0) 3 months ago container_redis

    分析

    可以看到,其展示的命令为:docker-entrypoint.sh redis-server /etc/redis/config/redis.conf,与我们的命令差距很多。

    法2:docker inspect(json格式,信息多)

    说明

    格式:docker inspect [container_name]/[container_id]

    执行命令并查看结果

    执行命令

    docker inspect 3cc9

    结果

    1. [
    2. {
    3. "Id": "3cc96a16754f761fdff4eb04a8b87672db8dc33f7c09ddb978446bfcc2b970c1",
    4. "Created": "2021-08-15T10:55:17.432645938Z",
    5. "Path": "docker-entrypoint.sh",
    6. "Args": [
    7. "redis-server",
    8. "/etc/redis/config/redis.conf"
    9. ],
    10. "State": {
    11. "Status": "exited",
    12. "Running": false,
    13. "Paused": false,
    14. "Restarting": false,
    15. "OOMKilled": false,
    16. "Dead": false,
    17. "Pid": 0,
    18. "ExitCode": 0,
    19. "Error": "",
    20. "StartedAt": "2022-05-07T13:41:56.74430811Z",
    21. "FinishedAt": "2022-05-07T13:44:32.016651639Z"
    22. },
    23. "Image": "sha256:621ceef7494adfcbe0e523593639f6625795cc0dc91a750629367a8c7b3ccebb",
    24. "ResolvConfPath": "/var/lib/docker/containers/3cc96a16754f761fdff4eb04a8b87672db8dc33f7c09ddb978446bfcc2b970c1/resolv.conf",
    25. "HostnamePath": "/var/lib/docker/containers/3cc96a16754f761fdff4eb04a8b87672db8dc33f7c09ddb978446bfcc2b970c1/hostname",
    26. "HostsPath": "/var/lib/docker/containers/3cc96a16754f761fdff4eb04a8b87672db8dc33f7c09ddb978446bfcc2b970c1/hosts",
    27. "LogPath": "/var/lib/docker/containers/3cc96a16754f761fdff4eb04a8b87672db8dc33f7c09ddb978446bfcc2b970c1/3cc96a16754f761fdff4eb04a8b87672db8dc33f7c09ddb978446bfcc2b970c1-json.log",
    28. "Name": "/container_redis",
    29. "RestartCount": 0,
    30. "Driver": "overlay2",
    31. "Platform": "linux",
    32. "MountLabel": "",
    33. "ProcessLabel": "",
    34. "AppArmorProfile": "docker-default",
    35. "ExecIDs": null,
    36. "HostConfig": {
    37. "Binds": [
    38. "/work/docker/redis/config:/etc/redis/config",
    39. "/work/docker/redis/data:/data"
    40. ],
    41. "ContainerIDFile": "",
    42. "LogConfig": {
    43. "Type": "json-file",
    44. "Config": {}
    45. },
    46. "NetworkMode": "default",
    47. "PortBindings": {
    48. "6379/tcp": [
    49. {
    50. "HostIp": "",
    51. "HostPort": "6379"
    52. }
    53. ]
    54. },
    55. "RestartPolicy": {
    56. "Name": "no",
    57. "MaximumRetryCount": 0
    58. },
    59. "AutoRemove": false,
    60. "VolumeDriver": "",
    61. "VolumesFrom": null,
    62. "CapAdd": null,
    63. "CapDrop": null,
    64. "CgroupnsMode": "host",
    65. "Dns": [],
    66. "DnsOptions": [],
    67. "DnsSearch": [],
    68. "ExtraHosts": null,
    69. "GroupAdd": null,
    70. "IpcMode": "private",
    71. "Cgroup": "",
    72. "Links": null,
    73. "OomScoreAdj": 0,
    74. "PidMode": "",
    75. "Privileged": false,
    76. "PublishAllPorts": false,
    77. "ReadonlyRootfs": false,
    78. "SecurityOpt": null,
    79. "UTSMode": "",
    80. "UsernsMode": "",
    81. "ShmSize": 67108864,
    82. "Runtime": "runc",
    83. "ConsoleSize": [
    84. 0,
    85. 0
    86. ],
    87. "Isolation": "",
    88. "CpuShares": 0,
    89. "Memory": 0,
    90. "NanoCpus": 0,
    91. "CgroupParent": "",
    92. "BlkioWeight": 0,
    93. "BlkioWeightDevice": [],
    94. "BlkioDeviceReadBps": null,
    95. "BlkioDeviceWriteBps": null,
    96. "BlkioDeviceReadIOps": null,
    97. "BlkioDeviceWriteIOps": null,
    98. "CpuPeriod": 0,
    99. "CpuQuota": 0,
    100. "CpuRealtimePeriod": 0,
    101. "CpuRealtimeRuntime": 0,
    102. "CpusetCpus": "",
    103. "CpusetMems": "",
    104. "Devices": [],
    105. "DeviceCgroupRules": null,
    106. "DeviceRequests": null,
    107. "KernelMemory": 0,
    108. "KernelMemoryTCP": 0,
    109. "MemoryReservation": 0,
    110. "MemorySwap": 0,
    111. "MemorySwappiness": null,
    112. "OomKillDisable": false,
    113. "PidsLimit": null,
    114. "Ulimits": null,
    115. "CpuCount": 0,
    116. "CpuPercent": 0,
    117. "IOMaximumIOps": 0,
    118. "IOMaximumBandwidth": 0,
    119. "MaskedPaths": [
    120. "/proc/asound",
    121. "/proc/acpi",
    122. "/proc/kcore",
    123. "/proc/keys",
    124. "/proc/latency_stats",
    125. "/proc/timer_list",
    126. "/proc/timer_stats",
    127. "/proc/sched_debug",
    128. "/proc/scsi",
    129. "/sys/firmware"
    130. ],
    131. "ReadonlyPaths": [
    132. "/proc/bus",
    133. "/proc/fs",
    134. "/proc/irq",
    135. "/proc/sys",
    136. "/proc/sysrq-trigger"
    137. ]
    138. },
    139. "GraphDriver": {
    140. "Data": {
    141. "LowerDir": "/var/lib/docker/overlay2/6f7b59f8a6115763ce859b21e36ec8c9830168b68209553e9ba124c4fe041502-init/diff:/var/lib/docker/overlay2/4212737a00b6ba82cf09874c0eeabae4ca0aa0345b5fcd1690eaf4609284a801/diff:/var/lib/docker/overlay2/e6623c58e41ef054763a1f98b556ad13d61cdf09fc792a60170aaa199a3b812d/diff:/var/lib/docker/overlay2/43d31a4b08141db74748b65900c46de397264a5d2f0e0ac90618fee5892b0e53/diff:/var/lib/docker/overlay2/789cfe51a57d9f1466a1c607972fe05c1183021323dec70765ad8757b3a50e94/diff:/var/lib/docker/overlay2/5235f3100764a73f7ae63908bc4c38acea558c2857ef133bd07b146c8fe75e03/diff:/var/lib/docker/overlay2/79545a883b658eafbebbd1e7f753c9a3dd1ddc3afeb68c211eba6c8f98cb2cbd/diff",
    142. "MergedDir": "/var/lib/docker/overlay2/6f7b59f8a6115763ce859b21e36ec8c9830168b68209553e9ba124c4fe041502/merged",
    143. "UpperDir": "/var/lib/docker/overlay2/6f7b59f8a6115763ce859b21e36ec8c9830168b68209553e9ba124c4fe041502/diff",
    144. "WorkDir": "/var/lib/docker/overlay2/6f7b59f8a6115763ce859b21e36ec8c9830168b68209553e9ba124c4fe041502/work"
    145. },
    146. "Name": "overlay2"
    147. },
    148. "Mounts": [
    149. {
    150. "Type": "bind",
    151. "Source": "/work/docker/redis/data",
    152. "Destination": "/data",
    153. "Mode": "",
    154. "RW": true,
    155. "Propagation": "rprivate"
    156. },
    157. {
    158. "Type": "bind",
    159. "Source": "/work/docker/redis/config",
    160. "Destination": "/etc/redis/config",
    161. "Mode": "",
    162. "RW": true,
    163. "Propagation": "rprivate"
    164. }
    165. ],
    166. "Config": {
    167. "Hostname": "3cc96a16754f",
    168. "Domainname": "",
    169. "User": "",
    170. "AttachStdin": false,
    171. "AttachStdout": false,
    172. "AttachStderr": false,
    173. "ExposedPorts": {
    174. "6379/tcp": {}
    175. },
    176. "Tty": false,
    177. "OpenStdin": false,
    178. "StdinOnce": false,
    179. "Env": [
    180. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
    181. "GOSU_VERSION=1.12",
    182. "REDIS_VERSION=6.0.10",
    183. "REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-6.0.10.tar.gz",
    184. "REDIS_DOWNLOAD_SHA=79bbb894f9dceb33ca699ee3ca4a4e1228be7fb5547aeb2f99d921e86c1285bd"
    185. ],
    186. "Cmd": [
    187. "redis-server",
    188. "/etc/redis/config/redis.conf"
    189. ],
    190. "Image": "redis",
    191. "Volumes": {
    192. "/data": {}
    193. },
    194. "WorkingDir": "/data",
    195. "Entrypoint": [
    196. "docker-entrypoint.sh"
    197. ],
    198. "OnBuild": null,
    199. "Labels": {}
    200. },
    201. "NetworkSettings": {
    202. "Bridge": "",
    203. "SandboxID": "9c1a358b64057773c092066f512e0b81c09da5a4493a334815e1f5eef79080b0",
    204. "HairpinMode": false,
    205. "LinkLocalIPv6Address": "",
    206. "LinkLocalIPv6PrefixLen": 0,
    207. "Ports": {},
    208. "SandboxKey": "/var/run/docker/netns/9c1a358b6405",
    209. "SecondaryIPAddresses": null,
    210. "SecondaryIPv6Addresses": null,
    211. "EndpointID": "",
    212. "Gateway": "",
    213. "GlobalIPv6Address": "",
    214. "GlobalIPv6PrefixLen": 0,
    215. "IPAddress": "",
    216. "IPPrefixLen": 0,
    217. "IPv6Gateway": "",
    218. "MacAddress": "",
    219. "Networks": {
    220. "bridge": {
    221. "IPAMConfig": null,
    222. "Links": null,
    223. "Aliases": null,
    224. "NetworkID": "ec1e270faa291c8a4ba08c96d5784ded8d33f91771b6af6176620c90c2b1be23",
    225. "EndpointID": "",
    226. "Gateway": "",
    227. "IPAddress": "",
    228. "IPPrefixLen": 0,
    229. "IPv6Gateway": "",
    230. "GlobalIPv6Address": "",
    231. "GlobalIPv6PrefixLen": 0,
    232. "MacAddress": "",
    233. "DriverOpts": null
    234. }
    235. }
    236. }
    237. }
    238. ]

    分析

    可以看到,结果很全。但无法准确得到自己执行的命令是什么,只能对比结果中的字段。

    法3:runlike(信息多,不能重现命令)

    说明

            这是个第三方包,需要先安装:

    1. # python3里包含pip工具
    2. sudo apt-get install python3;
    3. # 使用pip安装runlike
    4. sudo pip install runlike

    runlike命令格式:runlike -p [容器名称]/[容器ID]

    执行命令并查看结果

    执行命令

    runlike -p 3cc9

    结果

    1. docker run \
    2. --name=container_redis \
    3. --hostname=3cc96a16754f \
    4. --env=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
    5. --env=GOSU_VERSION=1.12 \
    6. --env=REDIS_VERSION=6.0.10 \
    7. --env=REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-6.0.10.tar.gz \
    8. --env=REDIS_DOWNLOAD_SHA=79bbb894f9dceb33ca699ee3ca4a4e1228be7fb5547aeb2f99d921e86c1285bd \
    9. --volume=/work/docker/redis/config:/etc/redis/config \
    10. --volume=/work/docker/redis/data:/data \
    11. --volume=/data \
    12. --workdir=/data \
    13. -p 6379:6379 \
    14. --restart=no \
    15. --runtime=runc \
    16. --detach=true \
    17. redis \
    18. redis-server /etc/redis/config/redis.conf

    分析

    可以发现,参数也是比较全,大致可以对比出原来的命令。

    法4:get_command_4_run_container(完美)

    说明

            这是个第三方包,需要先安装:

    docker pull cucker/get_command_4_run_container

    命令格式:

    docker run --rm -v /var/run/docker.sock:/var/run/docker.sock cucker/get_command_4_run_container [容器名称]/[容器ID]

    可以将其封装为一个别名:

    1. echo "alias get_run_command='docker run --rm -v /var/run/docker.sock:/var/run/docker.sock cucker/get_command_4_run_container'" >> ~/.bashrc \
    2. && \
    3. . ~/.bashrc

    这样就可以这样用了:get_run_command [容器名称]/[容器ID]

    执行命令并查看结果

    执行命令

     docker run --rm -v /var/run/docker.sock:/var/run/docker.sock cucker/get_command_4_run_container 3cc9

    结果

    分析

    可以发现,几乎完美复现出了原来的命令!!

  • 相关阅读:
    【C++】---模拟实现string
    升哲科技城市级“算力+数字底座”服务亮相2023服贸会
    OpenCV+特征检测
    如何创建Vue项目并与后端django联调
    你不知道的Spring的依赖的查找和注入的来源
    VB.net进行CAD二次开发(四)
    信息技术--教学设计
    ActiveMQ 笔记(九)Windows部署:单机与集群部署流程
    HTML拆分与共享方式——多HTML组合技术
    DeepI2P: Image-to-Point Cloud Registration via Deep Classification
  • 原文地址:https://blog.csdn.net/feiying0canglang/article/details/126435646