一. 检查容器
1. 检查容器相关的详细信息
[root@c720120 ~]# docker container inspect quotes
[
{
"Id": "75bc8db9fd99e6cde930e83e8e24140d2116f6edb9847970f40fafdd75f292f6",
"Created": "2018-06-05T09:15:42.607736399Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while :; do wget -qO- https://talaikis.com/api/quotes/random; printf '\n'; sleep 5; done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 31425,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-06-05T09:18:17.902466732Z",
"FinishedAt": "2018-06-05T09:16:41.911912537Z"
},
"Image": "sha256:3fd9065eaf02feaf94d68376da52541925650b81698c53c6824d92ff63f98353",
"ResolvConfPath": "/var/lib/docker/containers/75bc8db9fd99e6cde930e83e8e24140d2116f6edb9847970f40fafdd75f292f6/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/75bc8db9fd99e6cde930e83e8e24140d2116f6edb9847970f40fafdd75f292f6/hostname",
"HostsPath": "/var/lib/docker/containers/75bc8db9fd99e6cde930e83e8e24140d2116f6edb9847970f40fafdd75f292f6/hosts",
"LogPath": "/var/lib/docker/containers/75bc8db9fd99e6cde930e83e8e24140d2116f6edb9847970f40fafdd75f292f6/75bc8db9fd99e6cde930e83e8e24140d2116f6edb9847970f40fafdd75f292f6-json.log",
"Name": "/quotes",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"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,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/863d86a2f3d00ec5f02391d1e4a11da52b582472307273041b797d46495be275-init/diff:/var/lib/docker/overlay2/3df1a8ecefd1dc7db7d079f4f9f4008d5efdfc346f2ca409118c7fa4ea172b78/diff",
"MergedDir": "/var/lib/docker/overlay2/863d86a2f3d00ec5f02391d1e4a11da52b582472307273041b797d46495be275/merged",
"UpperDir": "/var/lib/docker/overlay2/863d86a2f3d00ec5f02391d1e4a11da52b582472307273041b797d46495be275/diff",
"WorkDir": "/var/lib/docker/overlay2/863d86a2f3d00ec5f02391d1e4a11da52b582472307273041b797d46495be275/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "75bc8db9fd99",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"while :; do wget -qO- https://talaikis.com/api/quotes/random; printf '\n'; sleep 5; done"
],
"Image": "alpine",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "2781e53feacf977629b5792f51df9ce2af180d4b2fd7678d9d2c415d507621d3",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/2781e53feacf",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "f1b1595dac56ffa940fcb6d42dd3769ed8a7e1990e76e7ee702bbb38bbfc0b8a",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "8d4ce133354ba6e3ab01684cf18b88535230647e78a6d698f1ef8d79f767b169",
"EndpointID": "f1b1595dac56ffa940fcb6d42dd3769ed8a7e1990e76e7ee702bbb38bbfc0b8a",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
2. 如果有时我们只需要输出相对应的信息,可以合适grep 工具或者filter进行过滤。
[root@c720120 ~]# docker container inspect -f "{{json .State}}" quotes
{"Status":"running","Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":31425,"ExitCode":0,"Error":"","StartedAt":"2018-06-05T09:18:17.902466732Z","FinishedAt":"2018-06-05T09:16:41.911912537Z"}
二 . 在一个容器内部执行命令
1. 运行一个叫quotes的容器,并执行/bin/sh命令
[root@c720120 ~]# docker container exec -i -t quotes /bin/sh
/ #
(2)在容器内部执行ps命令
/ # ps
PID USER TIME COMMAND
1 root 0:00 /bin/sh -c while :; do wget -qO- https://talaikis.com/api
891 root 0:00 /bin/sh
926 root 0:00 sleep 5
927 root 0:00 ps
2. 也可以通过以下方式执行,和第1步的结果一样。
[root@c720120 ~]# docker container exec quotes ps
PID USER TIME COMMAND
1 root 0:00 /bin/sh -c while :; do wget -qO- https://talaikis.com/api/quotes/random; printf ' '; sleep 5; done
957 root 0:00 sleep 5
958 root 0:00 ps