Docker is an open-source application that provides lightweight operating-system-level virtualization through the use of containers. It is a kind of virtualization technology that is specially designed to easily develop and deploy applications inside neatly packaged virtual containerized environments. Docker containers are in essence a set of software packages that run as one application that’s isolated from others. We can deploy it to any machine without any compatibility issues. By using this, the software stays system agnostic, simpler to use, less work to develop, and easy to maintain.
Table of Contents
What is environment variable?
An environment variable is a dynamic named value, containing an editable value and it could affect the program or services running on a computer/machine. It is made up of a name-value pair and set through a functionality built into the operating system or service. For example, on a Linux machine, you can run the command ‘env’ to display all available environment variables.
The picture above shows us that we need to use the following syntax to create environment variables:
Please note, the variables are case sensitive, the variable names are usually in UPPER CASE and the variable values are in lower case.
In docker, if we do not set an environment variable, it will not have any value and docker compose substitutes them with an empty string. When working in docker, sometimes we might need to pass environment information to the operating container. To achieve this, we can employ both ENV and ARG variables. And in this article, we will only show you how to set environment variables in docker.
Set Environment Variables in Docker
To pass your environment variable to a container, we need to set it first. In this article, we are using Ubuntu, you can follow the demonstration if you are using Linux operating system for your container development.
As previously explained, an environment variable consists of a variable name and its value. Let’s say we are going to create a variable named “POSTGRES_USER” and the variable value set to “masteruser”. To do so we can run this command:
$ export POSTGRES_USER=masteruser $ export POSTGRES_PASSWORD=m0d1fyth15
To verify whether the variable is running or not, we can invoke this command:
$ echo $POSTGRES_USER $ echo $POSTGRES_PASSWORD
By invoking the command above, you will see the variable value “masteruser”
Pass the variable to a container
In the previous section, we showed you how to create an environment variable. Now, there are three ways to set these variables for a docker container: with CLI arguments, use .env file, or through docker-compose.
- CLI arguments
We can run a command to launch a docker container, docker run as arguments by adding an -e flag, or a shorthand for –env to pass the environment variable
For example, we can run the following command to pass variables to a container.
$ docker run --name postgresql -e $POSTGRES_PASSWORD -e $POSTGRES_USER -d postgres
Once finished, the container will automatically run. And, you can go to the PostgreSQL console within the container by simply running this command:
$ docker exec -it postgresql psql -U $POSTGRES_USER
- Use .env file
Besides adding an -e flag in the command, we can also use an .env file to pass the variables.
First, let’s create a .env file using nano as editor, you can use any other editor you like.
$ nano .env
And paste the following lines in that file
Then, press CTRL + O and CTRL + X to save and close the editor
Next, we can run the command below to pass the variables in the .env file we just created
$ docker run --name postgresql --env-file .env -d postgres
- Docker compose
Some people prefer not to launch Docker containers directly with the docker run command. They opt to use a docker-compose file instead to pass the environment variables.
Using this option, you will need to configure the docker compose file to pass the session’s variables through to the Docker container. This configuration here passes the POSTGRES_USER variable to both the built environment and the runtime environment and sets a default value if it does not exist.
Override Environment Variables in Docker
Let’s assume that we have an image built from a Dockerfile, and it provides default environment values. The containers started from it, they have access to all environment variables defined in the Dockerfile. The values can be overridden by simply providing single environment variables or env_files, from which the environment variables are parsed and passed into the containers.
Once a process runs inside the container, or when a command is running, they can change the environment values for themselves. For example:
$ docker run mypythonimage VARIABLE_NAME=hello python myapp.py
Running the command above will override any VARIABLE_NAME you might have set for the myapp.py script, even if there were some value using a -e flag in the command line.
With the completion of this tutorial, you have just successfully learned How to Set Environment Variables in Docker. If you have applications running in a Docker environment and a Linux VPS hosting plan with us, you don’t have to learn how to set environment variables in Docker. You can simply ask our system administrators to help you with any aspect of managing your Linux server. They are available 24/7 and will take care of your request immediately.
PS. If you liked this post on How to Set Environment Variables in Docker, please share it with your friends on the social networks using the buttons below or simply leave a comment in the comment section. Thanks.