JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Passing environment variables to containers in Docker

Author:JIYIK Last Updated:2025/03/26 Views:

This article will introduce how to pass environment variables to containers in Docker.

Passing environment variables to containers in Docker using the -eand tags-env

We will first see how to create environment variables and pass them to the container. We will first pull the image we will use from the Docker registry using the following command.

$ 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 short form of -etags -env, which can be checked by running the command in the terminal docker run -help. This parameter allows us to create multiple key-value pairs of environment variables to pass to the container.

This is how we can create a single environment variable and pass it to the container we create using the Ubuntu image.

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

Output:

root@3a20bc21d3c8:/# echo $DEMO
sample_variable

We can also -ecreate environment variables using multiple labels of by simply linking them with the run command when creating a new container as shown below.

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

Once we have passed these two environment variables, we can access their values ​​using the following command. We have named the variable one DEMO1and we can access its value below.

root@9eee00d7ab01:/# echo $DEMO1

Output:

sample_variable1

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

root@9eee00d7ab01:/# echo $DEMO2

Output:

sample_variable2

Passing environment variables to a container in Docker using the export command

Alternatively, we can avoid chaining environment variables together by using the export command. This command is a built-in utility of the Linux bash shell and can also be used with WSL.

It allows us to create environment variables and pass them to child processes without affecting pre-existing variables. We will start 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 environment variables, we can pass them to the container we are creating as shown below.

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

We can then access and display the corresponding value of the variable using the command echo command as shown below.

root@5b4dae06932d:/# echo $MYSQL_USER
isaactonyloi

-envPassing environment variables to containers in Docker using a file

A better approach that gives you more room for debugging is to use Docker exploded files instead. This means we now don't have to pass environment variables directly when starting the docker container.

This approach means that we have to create an external file that contains the environment variables .env. We can create this file in our home directory using the following Linux command.

This command enters the Linux vi editor and creates our list of environment variables.

$ vi env.list

In the vi editor, we will create the environment variable as a key-value pair where the key is in uppercase and its respective value is in lowercase. We will exit, wq commandsave the variable by pressing the full colon and enter.

MYSQL_USER=isaactonyloi
MYSQL_PASS=fakepassword

wq

We can lsconfirm that the file env.listwas saved successfully using the command. This should return a list of the files in that directory, which lists the env.list.

We can also catview the entries in this list using the command as shown below.

$ cat env.list

Output:

MYSQL_USER=isaactonyloi
MYSQL_PASS=fakepassword

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

In this example, env.listthe files are stored in your home directory. Therefore, if your directory is in another directory, your path may look different.

We should run the command to pass these environment 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 previous approach, we can create a template file that contains all of the environment variables we want to pass in. This allows us to easily override the environment variables created in this file if we wish to make any changes.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Docker deploys nginx php application

Publish Date:2025/03/26 Views:129 Category:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

Publish Date:2025/03/26 Views:94 Category:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Running a Docker instance from a Dockerfile

Publish Date:2025/03/26 Views:139 Category:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

Copy files from host to Docker container

Publish Date:2025/03/25 Views:127 Category:Docker

This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest

Get the IP address of the Docker container

Publish Date:2025/03/25 Views:102 Category:Docker

This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con

Uninstalling Docker on macOS

Publish Date:2025/03/25 Views:95 Category:Docker

Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial