简介
说明

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

原命令

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

docker run -p 6379:6379 –name container_redis \
-v /work/docker/redis/config:/etc/redis/config \
-v /work/docker/redis/data:/data \
-d redis redis-server /etc/redis/config/redis.conf
创建好之后如下:

knife@ubuntu:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3cc96a16754f redis “docker-entrypoint.s…” 12 months ago Exited (0) 3 months ago container_redis
方法总结

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

需要sudo去执行。

执行命令并查看结果

执行命令:

sudo docker ps -a –no-trunc
结果

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
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
结果

[
{
“Id”: “3cc96a16754f761fdff4eb04a8b87672db8dc33f7c09ddb978446bfcc2b970c1”,
“Created”: “2021-08-15T10:55:17.432645938Z”,
“Path”: “docker-entrypoint.sh”,
“Args”: [
“redis-server”,
“/etc/redis/config/redis.conf”
],
“State”: {
“Status”: “exited”,
“Running”: false,
“Paused”: false,
“Restarting”: false,
“OOMKilled”: false,
“Dead”: false,
“Pid”: 0,
“ExitCode”: 0,
“Error”: “”,
“StartedAt”: “2022-05-07T13:41:56.74430811Z”,
“FinishedAt”: “2022-05-07T13:44:32.016651639Z”
},
“Image”: “sha256:621ceef7494adfcbe0e523593639f6625795cc0dc91a750629367a8c7b3ccebb”,
“ResolvConfPath”: “/var/lib/docker/containers/3cc96a16754f761fdff4eb04a8b87672db8dc33f7c09ddb978446bfcc2b970c1/resolv.conf”,
“HostnamePath”: “/var/lib/docker/containers/3cc96a16754f761fdff4eb04a8b87672db8dc33f7c09ddb978446bfcc2b970c1/hostname”,
“HostsPath”: “/var/lib/docker/containers/3cc96a16754f761fdff4eb04a8b87672db8dc33f7c09ddb978446bfcc2b970c1/hosts”,
“LogPath”: “/var/lib/docker/containers/3cc96a16754f761fdff4eb04a8b87672db8dc33f7c09ddb978446bfcc2b970c1/3cc96a16754f761fdff4eb04a8b87672db8dc33f7c09ddb978446bfcc2b970c1-json.log”,
“Name”: “/container_redis”,
“RestartCount”: 0,
“Driver”: “overlay2”,
“Platform”: “linux”,
“MountLabel”: “”,
“ProcessLabel”: “”,
“AppArmorProfile”: “docker-default”,
“ExecIDs”: null,
“HostConfig”: {
“Binds”: [
“/work/docker/redis/config:/etc/redis/config”,
“/work/docker/redis/data:/data”
],
“ContainerIDFile”: “”,
“LogConfig”: {
“Type”: “json-file”,
“Config”: {}
},
“NetworkMode”: “default”,
“PortBindings”: {
“6379/tcp”: [
{
“HostIp”: “”,
“HostPort”: “6379”
}
]
},
“RestartPolicy”: {
“Name”: “no”,
“MaximumRetryCount”: 0
},
“AutoRemove”: false,
“VolumeDriver”: “”,
“VolumesFrom”: null,
“CapAdd”: null,
“CapDrop”: null,
“CgroupnsMode”: “host”,
“Dns”: [],
“DnsOptions”: [],
“DnsSearch”: [],
“ExtraHosts”: null,
“GroupAdd”: null,
“IpcMode”: “private”,
“Cgroup”: “”,
“Links”: null,
“OomScoreAdj”: 0,
“PidMode”: “”,
“Privileged”: false,
“PublishAllPorts”: false,
“ReadonlyRootfs”: false,
“SecurityOpt”: null,
“UTSMode”: “”,
“UsernsMode”: “”,
“ShmSize”: 67108864,
“Runtime”: “runc”,
“ConsoleSize”: [
0,
0
],
“Isolation”: “”,
“CpuShares”: 0,
“Memory”: 0,
“NanoCpus”: 0,
“CgroupParent”: “”,
“BlkioWeight”: 0,
“BlkioWeightDevice”: [],
“BlkioDeviceReadBps”: null,
“BlkioDeviceWriteBps”: null,
“BlkioDeviceReadIOps”: null,
“BlkioDeviceWriteIOps”: null,
“CpuPeriod”: 0,
“CpuQuota”: 0,
“CpuRealtimePeriod”: 0,
“CpuRealtimeRuntime”: 0,
“CpusetCpus”: “”,
“CpusetMems”: “”,
“Devices”: [],
“DeviceCgroupRules”: null,
“DeviceRequests”: null,
“KernelMemory”: 0,
“KernelMemoryTCP”: 0,
“MemoryReservation”: 0,
“MemorySwap”: 0,
“MemorySwappiness”: null,
“OomKillDisable”: false,
“PidsLimit”: null,
“Ulimits”: null,
“CpuCount”: 0,
“CpuPercent”: 0,
“IOMaximumIOps”: 0,
“IOMaximumBandwidth”: 0,
“MaskedPaths”: [
“/proc/asound”,
“/proc/acpi”,
“/proc/kcore”,
“/proc/keys”,
“/proc/latency_stats”,
“/proc/timer_list”,
“/proc/timer_stats”,
“/proc/sched_debug”,
“/proc/scsi”,
“/sys/firmware”
],
“ReadonlyPaths”: [
“/proc/bus”,
“/proc/fs”,
“/proc/irq”,
“/proc/sys”,
“/proc/sysrq-trigger”
]
},
“GraphDriver”: {
“Data”: {
“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”,
“MergedDir”: “/var/lib/docker/overlay2/6f7b59f8a6115763ce859b21e36ec8c9830168b68209553e9ba124c4fe041502/merged”,
“UpperDir”: “/var/lib/docker/overlay2/6f7b59f8a6115763ce859b21e36ec8c9830168b68209553e9ba124c4fe041502/diff”,
“WorkDir”: “/var/lib/docker/overlay2/6f7b59f8a6115763ce859b21e36ec8c9830168b68209553e9ba124c4fe041502/work”
},
“Name”: “overlay2”
},
“Mounts”: [
{
“Type”: “bind”,
“Source”: “/work/docker/redis/data”,
“Destination”: “/data”,
“Mode”: “”,
“RW”: true,
“Propagation”: “rprivate”
},
{
“Type”: “bind”,
“Source”: “/work/docker/redis/config”,
“Destination”: “/etc/redis/config”,
“Mode”: “”,
“RW”: true,
“Propagation”: “rprivate”
}
],
“Config”: {
“Hostname”: “3cc96a16754f”,
“Domainname”: “”,
“User”: “”,
“AttachStdin”: false,
“AttachStdout”: false,
“AttachStderr”: false,
“ExposedPorts”: {
“6379/tcp”: {}
},
“Tty”: false,
“OpenStdin”: false,
“StdinOnce”: false,
“Env”: [
“PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”,
“GOSU_VERSION=1.12”,
“REDIS_VERSION=6.0.10”,
“REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-6.0.10.tar.gz”,
“REDIS_DOWNLOAD_SHA=79bbb894f9dceb33ca699ee3ca4a4e1228be7fb5547aeb2f99d921e86c1285bd”
],
“Cmd”: [
“redis-server”,
“/etc/redis/config/redis.conf”
],
“Image”: “redis”,
“Volumes”: {
“/data”: {}
},
“WorkingDir”: “/data”,
“Entrypoint”: [
“docker-entrypoint.sh”
],
“OnBuild”: null,
“Labels”: {}
},
“NetworkSettings”: {
“Bridge”: “”,
“SandboxID”: “9c1a358b64057773c092066f512e0b81c09da5a4493a334815e1f5eef79080b0”,
“HairpinMode”: false,
“LinkLocalIPv6Address”: “”,
“LinkLocalIPv6PrefixLen”: 0,
“Ports”: {},
“SandboxKey”: “/var/run/docker/netns/9c1a358b6405”,
“SecondaryIPAddresses”: null,
“SecondaryIPv6Addresses”: null,
“EndpointID”: “”,
“Gateway”: “”,
“GlobalIPv6Address”: “”,
“GlobalIPv6PrefixLen”: 0,
“IPAddress”: “”,
“IPPrefixLen”: 0,
“IPv6Gateway”: “”,
“MacAddress”: “”,
“Networks”: {
“bridge”: {
“IPAMConfig”: null,
“Links”: null,
“Aliases”: null,
“NetworkID”: “ec1e270faa291c8a4ba08c96d5784ded8d33f91771b6af6176620c90c2b1be23”,
“EndpointID”: “”,
“Gateway”: “”,
“IPAddress”: “”,
“IPPrefixLen”: 0,
“IPv6Gateway”: “”,
“GlobalIPv6Address”: “”,
“GlobalIPv6PrefixLen”: 0,
“MacAddress”: “”,
“DriverOpts”: null
}
}
}
}
]
分析

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

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

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

# python3里包含pip工具
sudo apt-get install python3;
# 使用pip安装runlike
sudo pip install runlike
runlike命令格式:runlike -p [容器名称]/[容器ID]

执行命令并查看结果

执行命令

runlike -p 3cc9
结果

docker run \
–name=container_redis \
–hostname=3cc96a16754f \
–env=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
–env=GOSU_VERSION=1.12 \
–env=REDIS_VERSION=6.0.10 \
–env=REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-6.0.10.tar.gz \
–env=REDIS_DOWNLOAD_SHA=79bbb894f9dceb33ca699ee3ca4a4e1228be7fb5547aeb2f99d921e86c1285bd \
–volume=/work/docker/redis/config:/etc/redis/config \
–volume=/work/docker/redis/data:/data \
–volume=/data \
–workdir=/data \
-p 6379:6379 \
–restart=no \
–runtime=runc \
–detach=true \
redis \
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]
可以将其封装为一个别名:

echo “alias get_run_command=’docker run –rm -v /var/run/docker.sock:/var/run/docker.sock cucker/get_command_4_run_container'” >> ~/.bashrc \
&& \
. ~/.bashrc
这样就可以这样用了:get_run_command [容器名称]/[容器ID]

执行命令并查看结果

执行命令

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

 

分析

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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/feiying0canglang/article/details/126435646