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.


This doesn't seem to be possible, you can list the environment variables but not just one.

From the docker commit doc

$ sudo docker inspect -f "{{ .Config.Env }}" c3f279d17e0a
[HOME=/ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin]

$ sudo docker commit --change "ENV DEBUG true" c3f279d17e0a  SvenDowideit/testimage:version3
f5283438590d

$ sudo docker inspect -f "{{ .Config.Env }}" f5283438590d
[HOME=/ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin DEBUG=true]

You can print them:

docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}'

(as in)

$ docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' c3fa3ce1622b
HOME=/
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Add a grep PATH and you could get only the PATH=xxx value.


user2915097 mentions in the comments jq, a lightweight and flexible command-line JSON processor, used in the article "Docker Inspect Template Magic " to format nicely the needed field:

docker inspect -f '{{json .State}}' jenkins-data | jq '.StartedAt' 
  "2015-03-15T20:26:30.526796706Z"

A very convenient option that doesn't require any external tools is:

docker exec 1e2b8689cf06 sh -c 'echo $PATH'

Admittedly this is not using docker inspect, but still..


docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' CONTAINER-NAME | grep -P "^YOUR_VAR=" | sed 's/[^=]*=//'

This solution requires grep(with -P option), sed and ability to pipeline them but solves 2 problems which most of the other solutions do not.

Firstly, it performs exact match on variable name. For example if you have following variables:

YOUR_VAR=value
ANOTHER_YOUR_VAR=value2
OTHER_VAR=YOUR_VAR

You will properly receive value.

Secondly, it properly handles cases where variable value contains = characters. For example:

REBEL_OPTS=-Drebel.stats=false

Will properly get you -Drebel.stats=false instead of false.