docker自定義docker ps及常用組合命令
我們開發過程中, 有部分是擷取docker ps的監控, 但是dockerps 的所屬資訊較少,比如父程序號, 子程序號, 當前掛載資訊等沒有辦法能夠獲取,故此我們需要自定義輸出我們定製的docker ps.
docker inspect 容器號或容器名稱能夠輸出容器的詳細資訊, 如:
[{
"Architecture":"amd64",
"Author":"wangzhehan",
"Comment":"Added hadoop hive client",
"Config": {
"AttachStderr": false,
"AttachStdin": false,
"AttachStdout": false,
"Cmd": [
"/bin/bash"
],
"CpuShares": 0,
"Cpuset": "",
"Domainname": "",
"Entrypoint": null,
"Env": null,
"ExposedPorts": {
"22/tcp": {}
},
"Hostname": "",
"Image": "",
"MacAddress": "",
"Memory": 0,
"MemorySwap": 0,
"NetworkDisabled": false,
"OnBuild": null,
"OpenStdin": false,
"PortSpecs": null,
"StdinOnce": false,
"Tty": false,
"User": "root",
"Volumes": null,
"WorkingDir": ""
},
"Container":"42062e85e250e46fdc1867da1e5a53bfea16a770515e94d81fd6cfc7ae9ba675",
"ContainerConfig": {
"AttachStderr": true,
"AttachStdin": true,
"AttachStdout": true,
"Cmd": [
"/bin/bash"
],
"CpuShares": 0,
"Cpuset": "",
"Domainname": "",
"Entrypoint": null,
"Env": null,
"ExposedPorts": {
"22/tcp": {}
},
"Hostname": "42062e85e250",
"Image": "jdclient/centos:7",
"MacAddress": "",
"Memory": 0,
"MemorySwap": 0,
"NetworkDisabled": false,
"OnBuild": null,
"OpenStdin": true,
"PortSpecs": null,
"StdinOnce": true,
"Tty": true,
"User": "root",
"Volumes": null,
"WorkingDir": ""
},
"Created":"2015-06-18T02:50:53.120470388Z",
"DockerVersion":"1.5.0",
"Id":"f0f749cfd553c13842b1c0bf4aadec7ce4b769142c58548cce484e24a6287c95",
"Os":"linux",
"Parent":"9b6f4bdec10036c3966c1f2e81bf3d3b3570a277a40d860d669557476d1211ec",
"Size": 16191,
"VirtualSize":1109989744
}
]
自定義格式則.可以是:
定製顯示docker列:
docker inspect --format='{{.State.Pid}}{{.Name}}' `docker ps -a -q`
顯示則是 pid 與容器名稱
對應個格式為 一級屬性{{.屬性}} 二級屬性 {{.屬性.屬性}} 三級屬性 {{.屬性.屬性.屬性}}
比如{{.State.Pid}} , {{.Name}} , {{.Os}}
顯示: 1064 , 映象名 , linux
常用監控命令:
docker ps -a -q | wc -l 監控容器數量
docker ps -q | wc -l 正在執行的容器的數量
docker ps -a | grep -v 'Up ' | grep -v'CONTAINER' | wc -l 非執行狀態的容器的數量
獲取docker執行程序
docker top `docker ps -q`
刪除docker容器
docker rm -f `docker ps -a -q` 刪除所有容器
docker rm `docker ps -a -q` 刪除非執行的容器
docker rm `docker ps -a | grep Exited | awk'{print $1}'` 刪除異常退出的容器
刪除docker無引用的映象
docker rmi -f `docker images | grep'<none>' | awk '{print $3}'`