Hello, friends. We already know that Docker is a recent technology that allows us to deploy applications in containers in the form of images. And in this post, we will help you with a Docker management topic that is vital to understand. So, in this post, you will learn How to Set Environment Variables on Docker.
Before we start, what is an environment variable?
Environment variables are needed to store some values that can be used when working, modifying values that the operating system has by default.
To better understand the definition we need to know what a variable is in computing and it is a memory space reserved for some value that is identified by a name called an identifier.
From the above then, we can define that an environment variable stores necessary system information that can be used by other programs.
If in your system, you want to know all the environment variables, you can execute the following command
printenv | less
There you will see a syntax similar to this one.
HOME=/home/angelo
Where the variable name is for example HOME
and what is after the =
sign is the value of the variable. So when an application requires the value HOME
it will actually get /home/angelo
and we can avoid having to write it all over again.
On the other hand, we can also create our own environment variables that will be used by any external application. And these external applications can also be Docker and an image.
So, this is the goal of this post, that we will be able to define environment variables in Docker and that can be system variables or even our own. This is important to do because otherwise Docker will fill these variables with empty values and the image may not work properly.
Let’s set Environment Variables in Docker
Before we do that, we need to create some environment variables on our system. In this post, I will use a Docker image of PostgreSQL so the environment variables I will create will be related to that. However, the process is the same in any case.
So, to create an environment variable on the system, you have to follow the following syntax
export [variable_name]=[value]
Since we said we were going to use a PostgreSQL image then I will create two variables. One for the user and one for the password.
export POSTGRES_USER=unixcop export POSTGRES_PASSWORD=password
To check that everything went fine, you can run
echo $POSTGRES_USER echo $POSTGRES_PASSWORD
And as output, you will see the values you have defined.
Now we have to pass this variable to the container.
One way to do it is via CLI and you can follow this methodology.
docker run --name postgresql -e $POSTGRES_PASSWORD -e $POSTGRES_USER -d postgres
In this case, thanks to the -e
option we will be able to set an environment variable for the PostgreSQL image, but as an argument, we assign the name of the variable we have created.
After the image has been downloaded to the system, you can run it using the system environment variable. For example:
docker exec -it postgresql psql -U $POSTGRES_USER
Remember again that this is an issue and we have to adapt our variables to the different images.
Another way to set environment variables in Docker
The previous way is quite convenient if you only need a few variables. But it may be possible that there are not only two but many more. In this case, it is convenient to use a .env
file.
So, create the file using any text editor you like.
nano .env
And inside the file set the variables that we have already created in the system.
export POSTGRES_USER=unixcop export POSTGRES_PASSWORD=password
Save the changes and close the editor.
Now run the Docker command again but use the .env
file as the environment variables argument.
docker run --name postgresql --env-file .env -d postgres
This is how simple it is to use the .env
file to define environment variables in Docker.
Conclusion
In this post, you have learned how to set environment variables in Docker quickly. This allows you to better manage your containers and make your work more manageable.