32

I wonder how I get an Environment variable from docker inspect.

when i run

docker inspect -f "{{.Config.Env.PATH}} " 1e2b8689cf06

i get the following

FATA[0000] template: :1:9: executing "" at <.Config.Env.PATH>: can't evaluate field PATH in type interface {}

10 Answers 10

34

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.

2
  • I created a PR for updating the docs for docker inspect github.com/docker/docker/pull/13405 Commented May 22, 2015 at 16:05
  • 1
    If you want to print out all environment variables use the following command: $docker inspect --format '{{ (index .Config.Env) }}' Commented Oct 23, 2017 at 16:01
27

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"
2
13

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..

2
  • Indeed not using inspect but, still : this answer is very simple, explicit -- we see the name of the var (PATH) we are looking for and not some magic index -- and it's understandable without diving in GO pattern language. Commented Sep 21, 2018 at 12:55
  • 1
    In this exact same way if you want to get ALL the environmental variables just replace echo $PATH with printenv
    – Alex D
    Commented Mar 20, 2019 at 16:28
11
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.

0
7

For those looking for a template-only solution using only docker inspect (when you can't just shell out and grep, etc.), the following example works (as of docker 1.11+):

> docker inspect -f '{{range $index, $value := .Config.Env}}{{if eq (index (split $value "=") 0) "SOME_VAR" }}{{range $i, $part := (split $value "=")}}{{if gt $i 1}}{{print "="}}{{end}}{{if gt $i 0}}{{print $part}}{{end}}{{end}}{{end}}{{end}}' *container_name*
  • it even handles environment variables with extra '='

Example container:

> docker run -d -e --name sleeper SOME_VAR=key=value alpine:3.4 -sh 'sleep 9999'

Extract SOME_VAR with:

> docker inspect -f '{{range $index, $value := .Config.Env}}{{if eq (index (split $value "=") 0) "SOME_VAR" }}{{range $i, $part := (split $value "=")}}{{if gt $i 1}}{{print "="}}{{end}}{{if gt $i 0}}{{print $part}}{{end}}{{end}}{{end}}{{end}}' sleeper
4

I find it better to parse for the environment variable name your interested than relying on index.

echo $(docker inspect --format '{{ .Config.Env }}' mysql) |  tr ' ' '\n' | grep MYSQL_ROOT_PASSWORD | sed 's/^.*=//'
4

Docker formatting options are quite limited, jq is much more powerful:

docker inspect 1e2b8689cf06 | jq '.[] | .Config.Env[] | select(test("^PATH=*"))'

or combine Docker format with jq while passing only Env variables to jq:

docker inspect -f '{{json .Config.Env }}' 1e2b8689cf06 | jq '.[] | select(test("^PATH=*"))'

Assuming there's a printenv in the container, you can use simple grep:

docker exec -i 1e2b8689cf06 printenv | grep -oP '(?<=PATH=).*'

-o would omit matching PATH= part from result. The syntax is much easier to read than the template magic.

1
  • jq is probably be best approach. To get the value, one could use jq '.[] | .Config.Env[] | select(test("^PATH=*")) | split("=") | .[1]'
    – heiner
    Commented Oct 20, 2023 at 13:06
2

If you want want to be able to check success of operation you could go with:

(
for env in $(docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' ${2}); do
    if [[ "${env}" == "${1}"* ]]; then
        echo ${env#${1}=}
        exit 0
    fi
done
exit 1
)

This will open a subshell and return depending if found:

$ docker-extract PATH docker_image || echo not found
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
$ docker-extract NON_EXISTENT_ENV docker_image || echo not found
not found
$
1
  • 1
    that's an great way, good to understand and to reuse. maybe you should have to add the information that the above code is shell script and is the content of docker-extract(.sh). Commented Mar 25, 2017 at 14:49
0

Print all env variables by specifying a docker container name:

docker exec <container-name> /bin/sh -c 'printenv'

Print all env variables by specifying a docker service name:

docker-compose exec <service-name> /bin/sh -c 'printenv'

Print a specific variable (PATH) by using docker container name:

docker exec <container-name> /bin/sh -c 'printenv' | grep '^PATH='

gives:

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

Print a specific variable value (PATH) by using docker container name:

docker exec <container-name> /bin/sh -c 'printenv' | grep '^PATH=' | cut -d= -f2-

gives:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Same examples as above but for the docker service name:

Specific env variable:

docker-compose exec <service-name> /bin/sh -c 'printenv' | grep '^PATH='

Specific env variable value:

docker-compose exec <service-name> /bin/sh -c 'printenv' | grep '^PATH=' | cut -d= -f2-
0

Use docker exec in combination with printenv. It should be on the most base images.

$ docker exec <container> printenv <desired_variable>

example

$ docker exec postgres printenv PGDATA
$ /var/lib/postgresql/data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.