实战
虚拟机一
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9fb1ff8de577 6a "sh" 2 hours ago Up About an hour hello
[root@localhost ~]# docker exec -it 9f /bin/sh //容器执行命令事件1
/ # ls
bin dev etc home proc root sys tmp usr var
/ # cd etc
/etc # read escape sequence
[root@localhost ~]# docker attach 9f
~ # ls
33.tar
~ # exit //退出容器
[root@localhost ~]#
虚拟机二
[root@localhost ~]# docker events //捕获容器执行命令一些事件1,Get real time events from the server
2017-11-11T12:18:40.304226574+08:00 container exec_create: /bin/sh 9fb1ff8de577d8cab4751c68c7194bd43b9d2216e90b760c3a13af62accc4ed4 (image=6a, name=hello)
2017-11-11T12:18:40.306834131+08:00 container exec_start: /bin/sh 9fb1ff8de577d8cab4751c68c7194bd43b9d2216e90b760c3a13af62accc4ed4 (image=6a, name=hello)
^C
[root@localhost ~]# docker wait 9f //当退出容器时捕获,Block until one or more containers stop, then print their exit codes
0
[root@localhost ~]# docker history 6a //Show the history of an image
IMAGE CREATED CREATED BY SIZE COMMENT
6ad733544a63 7 days ago /bin/sh -c #(nop) CMD ["sh"] 0B
<missing> 7 days ago /bin/sh -c #(nop) ADD file:264af0c48e23e8b... 1.13MB
[root@localhost ~]# docker inspect 6a //Return low-level information on Docker objects
[
{
"Id": "sha256:6ad733544a6317992a6fac4eb19fe1df577d4dec7529efec28a5bd0edad0fd30",
"RepoTags": [
"busybox:latest"
],
"RepoDigests": [
"busybox@sha256:bbc3a03235220b170ba48a157dd097dd1379299370e1ed99ce976df0355d24f0"
],
"Parent": "",
"Comment": "",
"Created": "2017-11-03T22:39:17.345892474Z",
"Container": "3fbce8bb8947b036ee7ff05a86c0574159c04fc10a3db7485ab7bf4f56fd4020",
"ContainerConfig": {
"Hostname": "3fbce8bb8947",
"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",
"#(nop) ",
"CMD [\"sh\"]"
],
"ArgsEscaped": true,
"Image": "sha256:7def3adf6786f772d2f02fc74c2d3f3334228416760aee45d3b6e561ce1c1dd3",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "17.06.2-ce",
"Author": "",
"Config": {
"Hostname": "",
"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": [
"sh"
],
"ArgsEscaped": true,
"Image": "sha256:7def3adf6786f772d2f02fc74c2d3f3334228416760aee45d3b6e561ce1c1dd3",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": null
},
"Architecture": "amd64",
"Os": "linux",
"Size": 1129289,
"VirtualSize": 1129289,
"GraphDriver": {
"Data": {
"RootDir": "/var/lib/docker/overlay/ed5d277761174e507cead0c30199fcfcf1e19e1cc476bbf70ea401fffda8a0ca/root"
},
"Name": "overlay"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:0271b8eebde3fa9a6126b1f2335e170f902731ab4942f9f1914e77016540c7bb"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1fb916fb0e13 6a "sh" 2 hours ago Created hello2
9fb1ff8de577 6a "sh" 2 hours ago Exited (0) 12 minutes ago hello
[root@localhost ~]# docker inspect 9f
[
{
"Id": "9fb1ff8de577d8cab4751c68c7194bd43b9d2216e90b760c3a13af62accc4ed4",
"Created": "2017-11-11T02:37:02.395276969Z",
"Path": "sh",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
"StartedAt": "2017-11-11T02:59:13.433210847Z",
"FinishedAt": "2017-11-11T04:21:51.287346478Z"
},
"Image": "sha256:6ad733544a6317992a6fac4eb19fe1df577d4dec7529efec28a5bd0edad0fd30",
"ResolvConfPath": "/var/lib/docker/containers/9fb1ff8de577d8cab4751c68c7194bd43b9d2216e90b760c3a13af62accc4ed4/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/9fb1ff8de577d8cab4751c68c7194bd43b9d2216e90b760c3a13af62accc4ed4/hostname",
"HostsPath": "/var/lib/docker/containers/9fb1ff8de577d8cab4751c68c7194bd43b9d2216e90b760c3a13af62accc4ed4/hosts",
"LogPath": "/var/lib/docker/containers/9fb1ff8de577d8cab4751c68c7194bd43b9d2216e90b760c3a13af62accc4ed4/9fb1ff8de577d8cab4751c68c7194bd43b9d2216e90b760c3a13af62accc4ed4-json.log",
"Name": "/hello",
"RestartCount": 0,
"Driver": "overlay",
"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/overlay/ed5d277761174e507cead0c30199fcfcf1e19e1cc476bbf70ea401fffda8a0ca/root",
"MergedDir": "/var/lib/docker/overlay/474b1ecc8826cefd57a66a0902c89cdb157680e97db85541625d838499698862/merged",
"UpperDir": "/var/lib/docker/overlay/474b1ecc8826cefd57a66a0902c89cdb157680e97db85541625d838499698862/upper",
"WorkDir": "/var/lib/docker/overlay/474b1ecc8826cefd57a66a0902c89cdb157680e97db85541625d838499698862/work"
},
"Name": "overlay"
},
"Mounts": [],
"Config": {
"Hostname": "9fb1ff8de577",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"sh"
],
"ArgsEscaped": true,
"Image": "6a",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "b5b8c9b19af54bb5676f343b52fbf9a1a8e69549858f57a5606022cc10b6232f",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/b5b8c9b19af5",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "e7d22476bef49eea8d0d5e296b26a241ab63f6108ccf00a2875f24f0214e0a51",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
[root@localhost ~]# docker logs 9f
/ # ls
bin dev etc home proc root sys tmp usr var
/ # top
Mem: 938636K used, 78224K free, 2988K shrd, 0K buff, 346452K cached
CPU: 0.0% usr 0.0% sys 0.0% nic 100% idle 0.0% io 0.0% irq 0.0% sirq
Load average: 0.00 0.03 0.05 3/404 6
PID PPID USER STAT VSZ %VSZ CPU %CPU COMMAND
1 0 root S 1220 0.1 0 0.0 sh
6 1 root R 1208 0.1 0 0.0 top
Mem: 938776K used, 78084K free, 2988K shrd, 0K buff, 346452K cached
CPU: 0.2% usr 0.0% sys 0.0% nic 99.7% idle 0.0% io 0.0% irq 0.0% sirq
Load average: 0.00 0.03 0.05 3/404 6
PID PPID USER STAT VSZ %VSZ CPU %CPU COMMAND
1 0 root S 1220 0.1 0 0.0 sh
6 1 root R 1208 0.1 0 0.0 top
/ # ls
bin dev etc home proc root sys tmp usr var
/ # ls
bin dev etc home proc root sys tmp usr var
/ # exit
/ # ls
bin dev etc home proc root sys tmp usr var
/ # ls
bin dev etc home proc root sys tmp usr var
/ # cd root
~ # ls
33.tar
~ # ls
33.tar
~ # exit
[root@localhost ~]# docker logs -t 9f
/ # ls
2017-11-11T02:37:18.422507320Z bin dev etc home proc root sys tmp usr var
2017-11-11T02:37:28.862083757Z / # top
2017-11-11T02:37:28.964721546Z
Mem: 938636K used, 78224K free, 2988K shrd, 0K buff, 346452K cached
2017-11-11T02:37:28.964917386Z CPU: 0.0% usr 0.0% sys 0.0% nic 100% idle 0.0% io 0.0% irq 0.0% sirq
2017-11-11T02:37:28.964954253Z Load average: 0.00 0.03 0.05 3/404 6
2017-11-11T02:37:28.964967724Z PID PPID USER STAT VSZ %VSZ CPU %CPU COMMAND
2017-11-11T02:37:28.964977292Z 1 0 root S 1220 0.1 0 0.0 sh
2017-11-11T02:37:33.970588045Z 6 1 root R 1208 0.1 0 0.0 top
Mem: 938776K used, 78084K free, 2988K shrd, 0K buff, 346452K cached
2017-11-11T02:37:33.970646114Z CPU: 0.2% usr 0.0% sys 0.0% nic 99.7% idle 0.0% io 0.0% irq 0.0% sirq
2017-11-11T02:37:33.970655365Z Load average: 0.00 0.03 0.05 3/404 6
2017-11-11T02:37:33.970663304Z PID PPID USER STAT VSZ %VSZ CPU %CPU COMMAND
2017-11-11T02:37:33.970671634Z 1 0 root S 1220 0.1 0 0.0 sh
2017-11-11T02:37:36.225271859Z 6 1 root R 1208 0.1 0 0.0 top
2017-11-11T02:37:42.442123636Z / # ls
2017-11-11T02:37:42.442191749Z bin dev etc home proc root sys tmp usr var
/ # ls
2017-11-11T02:50:34.905652668Z bin dev etc home proc root sys tmp usr var
2017-11-11T02:50:38.074578335Z / # exit
/ # ls
2017-11-11T04:06:09.998035984Z bin dev etc home proc root sys tmp usr var
/ # ls
2017-11-11T04:09:52.216426784Z bin dev etc home proc root sys tmp usr var
2017-11-11T04:10:04.580124217Z / # cd root
2017-11-11T04:10:05.767404121Z ~ # ls
2017-11-11T04:10:05.767659836Z 33.tar
~ # ls
2017-11-11T04:21:47.906764348Z 33.tar
2017-11-11T04:21:51.125377934Z ~ # exit
[root@localhost ~]# docker logs -t --tail 5 9f
2017-11-11T04:10:05.767404121Z ~ # ls
2017-11-11T04:10:05.767659836Z 33.tar
~ # ls
2017-11-11T04:21:47.906764348Z 33.tar
2017-11-11T04:21:51.125377934Z ~ # exit
[root@localhost ~]# docker version
Client:
Version: 17.09.0-ce
API version: 1.32
Go version: go1.8.3
Git commit: afdb6d4
Built: Tue Sep 26 22:41:23 2017
OS/Arch: linux/amd64
Server:
Version: 17.09.0-ce
API version: 1.32 (minimum version 1.12)
Go version: go1.8.3
Git commit: afdb6d4
Built: Tue Sep 26 22:42:49 2017
OS/Arch: linux/amd64
Experimental: false