site logo

Output Environment Variables of a Docker Container

One time, I set up a Docker container with my Cloudflare API token as the environment variable.

docker run -e API_KEY=<my_api_key> <some_user/some_container>

I forgot the token in the process, and now I want to retrieve the API token. It should be as easy as listing all manually added environment variables. Apparently Docker does not have such function? There is no such command as docker container env ls.

Well, turns out we have to do it the old-fashioned way.

Listing Env Var

We jsut need to spawn a shell inside the container, and then listing environment variables is a piece of cake. But still, wish there would be a more “docker” way of handling this.

First, obtain the container ID.

docker container ls

Example output:

CONTAINER ID   IMAGE                  COMMAND   CREATED        STATUS        PORTS     NAMES
feda6ca0a478   oznu/cloudflare-ddns   "/init"   3 months ago   Up 18 hours             cloudflare-ddns

Find your container name, and the random string on the left is the container ID.

Then, shell into the container and list all variables.

docker exec <container_id> sh -c 'printenv'

Note that it lists ALL environment variables, not only the one you manually created. Finding the specific variable might be a bit difficult.

If you do know the name for the variable, use

docker exec <container_id> sh -c 'printenv <ENV_VAR>'

Hope that helped. It definitely helped me. ¯\_(ツ)_/¯