Docker inspect 命令
2016-07-04 by dongnan
问题描述
使用 docker exec xxx ifconfig
获取容器IP失败,提示环境变量中,找不到 ifconfig
命令。
这是因为好多镜像为了减少体积,没有安装 ifconfig
等基础命令:
# 执行命令
docker exec 4fb49ce6141d ifconfig
exec: "ifconfig": executable file not found in $PATH
我们可以使用 docker inspect
命令来查看容器IP。
举个栗子
对于容器
针对 exec
命令错误,可以使用 docker inspect
参数,获取容器的全部变量,这其中也有IP地址,例如:
docker inspect 4fb49ce6141d | grep '"IPAddress'
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
成功查到容器IP地址。
对于镜像
除了获取容器相关变量,inspect
还可以获得 images
相关变量,例如:
docker inspect ubuntu-supervisor
[
{
"Id": "0bd51a6e54a8247e2843c8fc81d925fa81ff426160b22d6384b4f00435bf3774",
"RepoTags": [
"ubuntu-supervisor:latest"
],
"RepoDigests": [],
"Parent": "b2755d5841ab2b41821abe8a75858b787e4ff0f51b93cd249ebd2e27c10942d0",
"Comment": "",
"Created": "2016-03-24T05:49:16.722637969Z",
"Container": "a38e5a1f80fd6f6a2234f857aa2da853af8748087182a06b16b3f039995a2768",
"ContainerConfig": {
"Hostname": "e611e15f9c9d",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"22/tcp": {},
"80/tcp": {}
},
"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 [\"/usr/bin/supervisord\"]"
],
"Image": "b2755d5841ab2b41821abe8a75858b787e4ff0f51b93cd249ebd2e27c10942d0",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": [],
"Labels": {}
},
"DockerVersion": "1.9.1",
"Author": "dongnan \u003chttp://ywwd.net\u003e",
"Config": {
"Hostname": "e611e15f9c9d",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"22/tcp": {},
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/usr/bin/supervisord"
],
"Image": "b2755d5841ab2b41821abe8a75858b787e4ff0f51b93cd249ebd2e27c10942d0",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": [],
"Labels": {}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 0,
"VirtualSize": 247670721,
"GraphDriver": {
"Name": "aufs",
"Data": null
}
}
]
帮助
# 执行命令
docker inspect --help
Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]
Return low-level information on a container or image
-f, --format= Format the output using the given go template
--help=false Print usage
-s, --size=false Display total file sizes if the type is container
--type= Return JSON for specified type, (e.g image or container)