How to Pass Environment Variables to the Container in Docker

Isaac Tony Feb 02, 2024
  1. Use the -e and -env Tags Pass Environment Variables to the Container in Docker
  2. Use the Export Command to Pass Environment Variables to the Container in Docker
  3. Use an -env File to Pass Environment Variables to the Container in Docker
How to Pass Environment Variables to the Container in Docker

This tutorial will introduce the methods to pass environment variables to the container in Docker.

Use the -e and -env Tags Pass Environment Variables to the Container in Docker

We will begin by looking at how we can create environmental variables and pass them to a container. We’ll start by pulling the image that we will use from the Docker registry using the command below.

$ docker pull ubuntu~$ docker pull ubuntu

Output:

Using default tag: latest
latest: Pulling from library/ubuntu
08c01a0ec47e: Pull complete
Digest: sha256:669e010b58baf5beb2836b253c1fd5768333f0d1dbcb834f7c07a4dc93f474be
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

We will use the -e tag, the shorthand form of -env, whose usage can be checked by running the command docker run -help in the terminal. This argument allows us to create multiple key-value pairs of environment variables to pass to the container.

Here is how we can create a single environment variable and pass it to a container that we have created using the Ubuntu image.

$ docker run -it -e DEMO=sample_variable ubuntu bash

Output:

root@3a20bc21d3c8:/# echo $DEMO
sample_variable

We can also use multiple tags of -e to create environmental variables by simply chaining them alongside the run command while creating a new container, as shown below.

$ docker run -it -e DEMO1=sample_variable1 -e DEMO2=sample_variable2 ubuntu bash

Once we have passed on these two environmental variables, we can access their values using the commands below. We have named variable one as DEMO1, and we can access its value below.

root@9eee00d7ab01:/# echo $DEMO1

Output:

sample_variable1

Similarly, we can also print out variable two, as shown here.

root@9eee00d7ab01:/# echo $DEMO2

Output:

sample_variable2

Use the Export Command to Pass Environment Variables to the Container in Docker

Alternatively, we can also avoid the chaining together on environmental variables by using the export command. This command is a built-in utility tool for the Linux bash shell that can also be used with the WSL.

It allows us to create and pass environment variables to child processes without affecting pre-existing variables. We will begin by creating the environment variables, as shown below.

isaac@DESKTOP-HV44HT6:~$ export MYSQL_USER=isaactonyloi
isaac@DESKTOP-HV44HT6:~$ export MYSQL_PASS=fakepassword

Once we have created the environmental variables, we can then pass them to a container that we are creating, as shown below.

$ docker run -it -e MYSQL_USER -e MYSQL_PASS ubuntu bash

We can then access and display the respective values of the variables using the command echo command as shown below.

root@5b4dae06932d:/# echo $MYSQL_USER
isaactonyloi

Use an -env File to Pass Environment Variables to the Container in Docker

A better approach that gives you more room to debug is to use a Docker decompose file instead. This means that we now don’t have to pass environment variables directly when launching our docker container.

This method means that we have to create an external .env file that will contain our environmental variables. We can make this file in our home directory using the Linux command below.

This command opts into the Linux vi editor, creating our environment variables list.

$ vi env.list

While inside the vi editor, we will create the environment variables as key-value pairs with the keys in uppercase and their respective values in a small case. We will exit, saving the variable by pressing the full colon and typing the wq command.

MYSQL_USER=isaactonyloi
MYSQL_PASS=fakepassword

wq

We can use the ls command to confirm that the file env.list has been successfully saved. This should return a list of files in that directory with env.list among those listed.

We can also use the cat command to view the entries in that list, as shown below.

$ cat env.list

Output:

MYSQL_USER=isaactonyloi
MYSQL_PASS=fakepassword

Once we have that in place, we can now run the ubuntu image to create the new container and specify the path to where we have stored variables to pass them in.

The env.list file is stored in the home directory in this example. Therefore if yours is inside another directory, your path might look different.

We should run the command to pass these environmental variables to the new container.

$ docker run -it --env-file ./env.list ubuntu bash

We can verify that we have successfully passed the environment variables to the container by printing the variables as shown below.

/# echo $MYSQL_USER
/# echo $MYSQL_PASS

Output:

Isaactonyloi
fakepassword

Instead of the earlier methods, we can create a template file containing all the environmental variables we want to pass. This allows us to easily override the environment variables created in this file if we wish to make any changes.

Author: Isaac Tony
Isaac Tony avatar Isaac Tony avatar

Isaac Tony is a professional software developer and technical writer fascinated by Tech and productivity. He helps large technical organizations communicate their message clearly through writing.

LinkedIn

Related Article - Docker Environment Variable