Docker

How to Pass Environment Variables to a Docker Container

Docker is an open-source program that executes server applications using containers rather than virtual machines. Docker is a development and hosting configuration that utilizes fewer resources, so it’s no surprise that it is sweeping the development world. Docker environment variables are significant to this procedure because they store data that is unique to each user account accessing the software.

Using any modern JavaScript framework to create a container for a single app, you may discover that the configuration settings vary depending on where the container will execute. The basic URL for your API, for example, can vary based on whether you are testing or publishing the app. Environment variables are commonly used by developers to tackle this problem because code operates on the backend, environment variables are usually used there.

During the image construction, we may need to pass environment information to the operating container. To do so, we employ both ENV and ARG commands. The ARG command creates a variable that can be provided during the build process. Once it’s defined in the Dockerfile, you can use the parameter —build-arg to provide it to the image builder. In the Dockerfile, we can have numerous ARG instructions. In the Dockerfile, the ARG command is the only one that can come before the FROM instruction.

However, The ENV instruction sets the environment variable, which in turn specifies the environment for the construction instructions that follow. The ENV instruction is accessible during the construction process as well as when the container is started with the —env flag. However, we are unable to pass the ENV instruction while constructing the image. The ARG directive has the drawback of being unable to persist after the image has been built. To pass the environment data inline when creating the image, we will just utilize both ENV and ARG. In this guide, we will teach you how to pass an environment variable to a docker container.

Pre-requisites

To pass the environment variables to a docker container, you must have Ubuntu 20.04 Linux system installed. Also, make sure to install the up-to-date version of Docker on your system. In case, it is not present in the system, you can do its installation by using the below-cited command.

$ sudo apt install docker.io

In this whole tutorial, we have used the “Sudo” keyword which means you must have sudo privileges to run this article.

Method to Pass the Environment Variables

To pass the environment variables to a docker container, you have to open the command line terminal by using the shortcut key of “Ctrl+Alt+T” or by searching it in the application menu using its search bar. Now, follow the below-appended steps:

Step 1: Pull image
Throughout this guide, we will be using Alpine, a tiny (5MB) Linux image. To begin, download the image locally by using the following appended command:

$ sudo docker pull alpine:3

The output shows the successful image pulling.

Step 2: Passing environment variables
Using the –env flag, we may input environment variables as key-value pairs right into the command prompt when launching our Docker container. Let’s use the following appended command as an illustration:

$ sudo docker run --env VARIABLE1=foobar alpine:3

There will be no specific output for this command.

Step 3: Reflecting environment variables
To reflect the environment variables, use the following appended command along with the keyword “export”.

$ export VARIABLE2=foobar2

There will be no specific output for this command.

Step 4: Specify environment variables
Now, without specifying the value of the environment variable, let’s define the environment variable by using the below-listed command in the terminal:

$ sudo docker run --env VARIABLE2 alpine:3 env

In the output, you can see that the environment variable has been successfully passed to the docker container.

Conclusion

In this article, we have deliberated the concept and importance of environment variables in docker. Also, we have provided you the step-by-step guide to passing an environment variable to a docker container. I hope you will have no problem while passing the environment variable to a docker container.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content