1. 依附容器docker
docker attach big_poincarejson
只能依附前臺型容器,後臺型容器是沒法依附終端的,由於它自己就不接受用戶交互輸入。centos
2. 查看容器日誌bash
docker logs -f deamon_logs :滾動查看日誌(tail -f)app
docker logs --tail=5 deamon_logs :查看最新5行日誌(tail -n 5)
ui
docker logs -f --tail=5 deamon_logs : 查看最新五行,並監視最新的日誌spa
docker logs -f -t deamon_logs : 添加-t標誌能夠打印產生日誌的時刻3d
3. 查看容器進程日誌
docker top deamon_logscode
4. 容器內執行命令(不本身總結了,書上寫的很清楚)
在容器啓動的時候,一般須要指定其須要執行的程序,然而有時候咱們須要在容器運行以後中途啓動另外一個程序。從Docker 1.3開始,咱們能夠用docker exec命令在容器中運行新的任務,它能夠建立兩種任務:後臺型任務和交互型任務。後臺型任務沒有用戶交互終端,交互型任務具備和用戶交互的輸入輸出終端。
讓咱們看一個後臺型任務的例子:
$ sudo docker exec -d daemon_dave touch /etc/new_config_file
這裏-d標誌表示要運行一個後臺型任務。接着須要指定要運行命令的容器名和要運行的命令。在這個例子裏,touch命令會在daemon_dave容器中建立一個new_config_file的文件。經過docker exec建立的後臺任務,咱們能夠執行維護、監視、管理等複雜而有意義的任務。
使用docker exec命令建立交互型任務時,須要加上-t -i標誌,示例以下:
$ sudo docker exec -t -i daemon_dave /bin/bash
-t和-i標誌的用法與咱們建立交互型容器時同樣,會建立一個交互終端,並捕捉進程的標準輸入和輸出。經過該交互終端,咱們能夠在容器內運行命令和查看信息等。
5. 查看容器信息
如下是我我的電腦上的例子:
docker inspect deamon_logs
[
{
"Id": "870412fbfd3dae8ea4a44b6b00583eb486eb43fded07b6972f9ecc85cc0db710",
"Created": "2016-03-23T08:51:17.723809515Z",
"Path": "/bin/bash",
"Args": [
"-c",
"for((i=0;1;i++));do echo $i;sleep 1;done;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 26897,
"ExitCode": 0,
"Error": "",
"StartedAt": "2016-03-23T08:51:17.861253283Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:d0e7f81ca65cdd391b6eb3dd3ce2454a575023156cd932ee4a58f188436bc5e0",
"ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/870412fbfd3dae8ea4a44b6b00583eb486eb43fded07b6972f9ecc85cc0db710/resolv.conf",
"HostnamePath": "/mnt/sda1/var/lib/docker/containers/870412fbfd3dae8ea4a44b6b00583eb486eb43fded07b6972f9ecc85cc0db710/hostname",
"HostsPath": "/mnt/sda1/var/lib/docker/containers/870412fbfd3dae8ea4a44b6b00583eb486eb43fded07b6972f9ecc85cc0db710/hosts",
"LogPath": "/mnt/sda1/var/lib/docker/containers/870412fbfd3dae8ea4a44b6b00583eb486eb43fded07b6972f9ecc85cc0db710/870412fbfd3dae8ea4a44b6b00583eb486eb43fded07b6972f9ecc85cc0db710-json.log",
"Name": "/deamon_logs",
"RestartCount": 0,
"Driver": "aufs",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"ShmSize": 67108864,
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"KernelMemory": 0,
"Memory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null
},
"GraphDriver": {
"Name": "aufs",
"Data": null
},
"Mounts": [],
"Config": {
"Hostname": "870412fbfd3d",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": null,
"Cmd": [
"/bin/bash",
"-c",
"for((i=0;1;i++));do echo $i;sleep 1;done;"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"build-date": "2016-03-04",
"license": "GPLv2",
"name": "CentOS Base Image",
"vendor": "CentOS"
},
"StopSignal": "SIGTERM"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "3e90239e760219fa9f0d72d112e8ad0bfb060d6c06b53bb0dc8a95db58714a3f",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/3e90239e7602",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "00a8a8bc2a5e3f6e8621df6e0958f1889dbca71c581ba9baf50b69a960ea898b",
"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": "b0aeedb86f56f2669dc6f4949d099c975a858595b626bd7b80f028ea2318015c",
"EndpointID": "00a8a8bc2a5e3f6e8621df6e0958f1889dbca71c581ba9baf50b69a960ea898b",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
}
}
]
docker inspect --format='{{ .State.Running }}' deamon_logs
參考文檔:《Docker開發實踐》第二章2.2 《容器內信息獲取和命令執行》