Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Docker is a containerized platform that packages an application along with all its dependencies. Ideally, these applications require a certain environment to start. In Linux, we use environment variables to fulfil this requirement. These variables determine the behaviour of the application.

In this tutorial, we'll learn to retrieve all the environment variables that were set while running the Docker container. Just like there are multiple ways to pass environment variables to a Docker container, there are different ways to fetch these variables once set.

Before we move further, let's first understand the need for environment variables.

2. Understand Environment Variables in Linux

Environment variables are a dynamic set of key-value pairs that are accessible system-wide. These variables may help the system in locating a package, configuring the behaviour of any server or even making the bash terminal output intuitive.

By default, the environment variables present on the host machine are not passed on to the Docker container. The reason is that the Docker container is supposed to be isolated from the host environment. So, if we want to use an environment within the Docker container, then we have to set it explicitly.

Let's now look into different approaches to get the environment variables from inside a Docker container.

3. Fetch Using docker exec Command

For the demonstration purpose, let's first run an Alpine Docker container and pass some environment variables to it:

docker run -itd --env "my_env_var=baeldung" --name mycontainer alpine
9de9045b5264d2de737a7ec6ba23c754f034ff4f35746317aeefcea605d46e84

Here, we are passing the my_env_var with value baeldung inside the Docker container named mycontainer.

Lets now use the docker exec command to fetch the environment variable named my_env_var:

$ docker exec mycontainer /usr/bin/env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=9de9045b5264
my_env_var=baeldung
HOME=/root

Here, we are executing the /usr/bin/env utility inside the Docker container. Using this utility, you can view all the environment variables set inside Docker containers. Notice that our my_env_var is also present in the output.

We can also use the following command to achieve similar results:

$ docker exec mycontainer /bin/sh -c /usr/bin/env
HOSTNAME=9de9045b5264
SHLVL=1
HOME=/root
my_env_var=baeldung
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/

Notice that there are more environment variables compared to the previous output. This is so because this time we are executing the command with the help of /bin/sh binary. This binary sets some extra environment variables implicitly.

Also, /bin/sh shell is not mandatory to be present in all the Docker images. For example, in the centos Docker image, which contains the /bin/bash shell, we'll retrieve the environment variables using the following command:

$ docker run -itd --env "container_type=centos" --name centos_container centos
aee6f2718f18723906f7ab18ab9c37a539b6b2c737f588be71c56709948de9eb
$ docker exec centos_container bash -c /usr/bin/env
container_type=centos
HOSTNAME=aee6f2718f18
PWD=/
HOME=/root
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env

We can also fetch the value of a single environment variable using the docker exec command:

$ docker exec mycontainer printenv my_env_var
baeldung

printenv is another command-line utility that displays the environment variables in Linux. Here, we are passing the env variable name, my_env_var, as an argument to printenv. This will print the value of my_env_var.

The drawback of this approach is that it is necessary for the Docker container to be in the running state in order to retrieve the environment variables.

4. Fetch Using docker inspect Command

Let's now look into another approach to get environment variables when the Docker container is in the stopped state. We'll use the docker inspect command for this purpose.

docker inspect provides detailed information for all the Docker resources. The output is in JSON format. Hence, we can filter the output as per our requirements.

Let's manipulate the docker inspect command to display only the environment variables of the container:

$ docker inspect mycontainer --format "{{.Config.Env}}"
[my_env_var=baeldung PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin]

Here, we have filtered the environment variables from the docker inspect output using the –format option. Again, the my_env_var is present in the output.

We can also fetch a single environment variable using the docker inspect command:

$ docker inspect mycontainer | jq -r '.[].Config.Env[]|select(match("^my_env_var"))|.[index("=")+1:]'
baeldung

jq is a lightweight JSON processor that can parse and transform the JSON data. Here, we are passing the JSON output of the docker inspect to the jq command. It then searches for the my_env_var variable and displays its value by splitting it over “=”.

Note that we can use the container id as well with the docker exec and docker inspect commands.

Unlike docker execdocker inspect command works with both stopped and running containers.

5. Conclusion

In this article, we learned how to retrieve all the environment variables from a Docker container. We began by discussing the significance of environment variables in Linux. We then looked at docker exec and docker inspect commands to retrieve the environment variables.

The docker exec approach has some restrictions while the docker inspect command runs under all the circumstances.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!