Wednesday, October 5, 2022
Home CONTAINERIZATION How to pass environment variables to Docker containers?

How to pass environment variables to Docker containers?

Intro

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

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.

POSTGRES_PASSWORD=mustafa
POSTGRES_USER=mus123

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

Conclusion

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

Thanks.

Mustafa Gonen
DevOps engineer, helps companies to balance their needs throughout the software development life cycle, from coding and deployment to maintenance and updates by automation. He believes the beauty of diversity. Working in DevOps culture and being a part of this harmony makes him highly motivated and satisfied.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular

How to query EC2 tags from within the instance?

Intro To help you manage your instances, images, and other Amazon EC2 resources, you can assign your own...

How to install Python3 on Amazon Linux 2?

Intro Python is an object-oriented programming language that is widely used. In addition to that, it's free and...

How to connect an AWS EC2 instance with FileZilla and do SFTP?

Intro EC2 is one of the essential services of Amazon Web Services (AWS), providing highly available and scalable...

How to install AWS CLI v2 on Linux?

Intro Whether you’ve been using AWS for some time or you are a novice, you may have noticed...

Recent Comments