How to get ENV variable when doing Docker Inspect

You can get directly with a command similar to

docker inspect --format '{{ index (index .Config.Env) 1 }}' 797

which shows for me

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

You will notice that replacing the 1 by 0 like

docker inspect --format '{{ index (index .Config.Env) 0 }}' 797

gives

DISPLAY=:0

In fact I had noticed the following for various docker inspect from a more general to a more precise display

docker inspect --format '{{ (.NetworkSettings.Ports) }}' 87c
map[8000/tcp:[map[HostIp:0.0.0.0 HostPort:49153]]]
docker inspect --format '{{ ((index .NetworkSettings.Ports "8000/tcp") 0) }}' 87c
[map[HostIp:0.0.0.0 HostPort:49153]]
docker inspect --format '{{ index (index .NetworkSettings.Ports "8000/tcp") 0 }}' 87c
map[HostIp:0.0.0.0 HostPort:49153]
docker inspect --format '{{ (index (index .NetworkSettings.Ports "8000/tcp") 0).HostIp }}' 87c
0.0.0.0
docker inspect --format '{{ (index (index .NetworkSettings.Ports "8000/tcp") 0).HostPort }}' 87c
49153

Note: docker inspect -f works and is shorter, of course, I posted the “old” syntax.

Leave a Comment