In application development, it is best practice to separate services from configurations. The Twelve-factor app methodology recommends storing configurations in environments. That means we’ll need a way to inject them into our containers. This article will show you how to pass environment variables to the Docker containers.
Environment variables are a set of dynamic named values stored within the system. These variables allow you to customize how specific applications and services behave within the system. Each variable contains a name and an associated value. Usually, variable names are in the upper case, and the values are case-sensitive.
Here in this blog posts we will give you two options to be able to define environment variables in your container.
1- Defining an environment variable in your shell and referring to that with -e option
2- Defining environment variables in a file called .env and referring to that with –env-file .env option.
How to set an environment variable in your shell?
To pass environment variables to Docker containers, we first have to set them. Let’s explain with an example on Linux. Say you want to set variables on your Linux host. Thus, you will use it in a PostgreSQL database container later. POSTGRES_USER and POSTGRES_PASSWORD are expected valid variables by PostgreSQL. Let’s set the values for these environment variables on our Linux environment. To put that variable, the command:
export POSTGRES_USER=mustafa POSTGRES_PASSWORD=mus123
To verify that variable has been set, issue the command:
echo $POSTGRES_USER $POSTGRES_PASSWORD
How to pass environment variables to Docker containers?
Now that you understand how environment variables work. Let us see how you can pass to your containers. Here is the point. You can define environment variables however you like in the Linux system. Whereas, container images expect certain variables. For example, for the user and the password, this PostgreSQL database image can’t use anything other than POSTGRES_USER and POSTGRES_PASSWORD variable names. We have already given appropriate names to these variables. So, our command to pass environment variables to Docker containers will be with -e ENVIRONMENTVARIABLENAME option:
docker run --name postgresql -e POSTGRES_PASSWORD -e POSTGRES_USER -d postgres
The command will succeed and the container will remain running. You can test it by accessing the PostgreSQL console within the container by issuing:
docker exec -it postgresql psql -U $POSTGRES_USER
How to pass variables with an .env file?
One of the problems with passing environment variables is that they live in memory. When you unset them with the unset command it disappears. To avoid this, we use an environment variable file. Let’s stick with the same variables above. Then create a new .env file with the command
nano .env In that file paste the following. Don’t forget to save and close the file.
Now, how do we pass those variables to our container? Simple, we’d use the command to give environment variables as an option
like –env-file .env :
docker run --name postgresql --env-file .env -d postgres
Make sure to use the full path to your .env file unless you are running the Docker command from the same directory housing the file. Your container will deploy and be ready to use. You can test it by accessing the PostgreSQL console. The only difference is, you have to manually type out the user. The reason is we didn’t set the POSTGRES_USER variable in the host system.
docker exec -it postgresql psql -U mustafa
And that’s how you pass environment variables to Docker containers, either from the command line or using a .env file. Hopefully, it makes your life a bit easier in your developer workflow.
If you want to read environment variables from official Docker documentation here is the link