docker inspect命令-显示更底层的容器或image信息

inspect 显示更底层的容器或image信息

Usage: docker inspect CONTAINER|IMAGE [CONTAINER|IMAGE...]Return low-level information on a container or image

-f,--format=""Format the output using the given go template.


Go's text/template package describes all the details of the format.



For the most part, you can pick out any field from the JSON in a fairly straightforward manner.

$ sudo docker inspect --format='{{.NetworkSettings.IPAddress}}' $INSTANCE_ID


One can loop over arrays and maps in the results to produce simple text output:

$ sudo docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' $INSTANCE_ID


The .Field syntax doesn't work when the field name begins with a number, but the template language'sindex function does. The .NetworkSettings.Ports section contains a map of the internal port mappings to a list of external address/port objects, so to grab just the numeric public port, you use indexto find the specific port map, and then index 0 contains first object inside of that. Then we ask for theHostPort field to get the public address.

$ sudo docker inspect --format='{{(index (index .NetworkSettings.Ports "8787/tcp") 0).HostPort}}' $INSTANCE_ID


The .Field syntax doesn't work when the field contains JSON data, but the template language's customjson function does. The .config section contains complex json object, so to grab it as JSON, you usejson to convert config object into JSON

$ sudo docker inspect --format='{{json .config}}' $INSTANCE_ID