Get Environment Variable from Docker Container

What’s the simplest way to get an environment variable from a docker container that has not been declared in the Dockerfile?

For instance, an environment variable that has been set through some docker exec container /bin/bash session?

  • Why does AWS have two services to deploy Docker (one “Infra” and one “Platform”)?
  • Debian httpredir mirror system unreliable/unusable in Docker?
  • Docker push to AWS ECR fails on Windows: no basic auth credentials
  • Connect Docker Containers with remote debugger
  • Extend docker devicemapper loop-lvm sparse file
  • Docker - what does `docker run --restart always` actually do?
  • I can do docker exec container env | grep ENV_VAR, but I would prefer something that just returns the value.

    I’ve tried using docker exec container echo "$ENV_VAR", but the substitution seems to happen outside of the container, so I don’t get the env var from the container, but rather the env var from my own computer.


  • Docker --link not adding two entries to hosts file
  • How can I access the Kubernetes service through ClusterIP
  • Docker vfs as default storage driver for docker 1.10.0
  • Does sbt-native-package docker plugin support to create a tag
  • is it possible to run shell script and then node app.js inside docker container
  • docker cp to container: file name too long
  • 5 Solutions collect form web for “Get Environment Variable from Docker Container”

    The proper way to run echo "$ENV_VAR" inside the container so that the variable substitution happens in the container is:

    docker exec container bash -c 'echo "$ENV_VAR"'

    You can use printenv VARIABLE instead of /bin/bash -c 'echo $VARIABLE. It’s much simpler and it does not perform substitution:

    docker exec container printenv VARIABLE

    To view all env variables:

    docker exec container env

    To get one:

    docker exec container env | grep VARIABLE | cut -d'=' -f2

    The downside of using docker exec is that it requires a running container, so docker inspect -f might be handy if you’re unsure a container is running.

    Example #1. Output a list of space-separated environment variables in the specified container:

    docker inspect -f \
       '{{range $index, $value := .Config.Env}}{{$value}} {{end}}' container_name

    the output will look like this:

    ENV_VAR1=value1 ENV_VAR2=value2 ENV_VAR3=value3

    Example #2. Output each env var on new line and grep the needed items, for example, the mysql container’s settings could be retrieved like this:

    docker inspect -f \
        '{{range $index, $value := .Config.Env}}{{println $value}}{{end}}' \
        container_name | grep MYSQL_

    will output:


    Example #3. Let’s modify the example above to get a bash friendly output which can be directly used in your scripts:

    docker inspect -f \
       '{{range $index, $value := .Config.Env}}export {{$value}}{{println}}{{end}}' \
       container_name | grep MYSQL

    will output:

    export MYSQL_PASSWORD=secret
    export MYSQL_ROOT_PASSWORD=supersecret
    export MYSQL_USER=demo
    export MYSQL_DATABASE=demodb
    export MYSQL_MAJOR=5.5
    export MYSQL_VERSION=5.5.52

    If you want to dive deeper, then go to Go’s text/template package documentation with all the details of the format.

    @aisbaa’s answer works if you don’t care when the environment variable was declared. If you want the environment variable, even if it has been declared inside of an exec /bin/bash session, use something like:

    IFS="=" read -a out <<< $(docker exec container /bin/bash -c "env | grep ENV_VAR" 2>&1)

    It’s not very pretty, but it gets the job done.

    To then get the value, use:

    echo ${out[1]}
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.