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?
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.
Thanks.
To view all env variables:
docker exec container env
To get one:
docker exec container env | grep VARIABLE | cut -d'=' -f2
The proper way to run echo "$ENV_VAR"
inside the container so that the variable substitution happens in the container is:
docker exec <container_id> 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