Docker

How to Change Pre-defined Environment Variables

Docker is one of the famous containerize platforms that is utilized to share and execute the program, projects, and software in containers. Docker also provides us with an isolated environment to build the program or software from scratch. For this instance, the Docker environment already comes with pre-defined environment variables and other configuration settings of containers that may not be required by the user or may user want to change them.

This blog will demonstrate:

How to Change Docker Pre-defined Environment Variables?

The Docker environment variables are helpful to add some configuration settings. Some of the Docker variables are pre-defined such as DOCKER_HOST, DOCKER_CONTEXT, and DOCKER_BUILDKIT. Some environment variables are defined when users define the Dockerfile to create the Docker image. These variables can also be changed through the “-e” option.

For the proper demonstration, follow the provided instructions.

Step 1: Create Dockerfile

First, create the Dockerfile to execute simple “app.py” python program. In Dockerfile, set the Docker environment variable for container configuration using the “ENV” Dockerfile command. For instance, we have set “MESSAGE” and “USER” environment variables:

FROM python
WORKDIR /app
COPY . /app
ENV MESSAGE="Hello! This is Docker Environment variable!"
ENV USER="Docker-User"
CMD ["python", "app.py"]

 
Step 2: Create Program File

Now, write a Python program in “app.py” file. Access and print the environment variable. For this purpose, we are using Python “os” module:

import os
message = os.environ.get("MESSAGE")
user = os.environ.get("USER")
print(message)
print(user)

 
Step 3: Build Docker Image/Snapshot

Next, generate the new Docker image or snapshot by utilizing the “docker build -t <image-name>” command. The “-t” flag is utilized to specify the image/snapshot name:

docker build -t python-img .

 

Step 4: Execute Docker Image/Snapshot

Execute the newly created image to create and fire up the container through “docker run” command:

docker run python-img

 

Step 5: Change Environment Variables

In order to change environment variables through command, utilize the “-e” option along with environment variable. For instance, we are changing the “USER” environment variable:

docker run --rm -e USER="Linuxhint" python-img

 

That’s how pre-defined environment variable can be changed in “docker run” command.

How to Change Pre-defined Environment Variables in Docker Compose?

Users can also change the pre-defined environment variables in Docker compose. The Docker compose is a well-known Docker tool that is mostly utilize to configure and run multi container applications or services. To change the pre-defined environment variables in Docker Compose, go through the provided instructions.

Step 1: Create “docker-compose.yml” File

First, create the “docker-compose.yml” file and add the following configurations into the compose file. In the provided snippet, the “environment” key is basically used to change the environment variables that is pre-defined in Dockerfile. For instance, we have modify the “MESSAGE” and “USER” environment variables:

version: '3'
services:
  py-app:
    build: .
    environment:
      - MESSAGE="Compose Service"
      - USER="Linuxhint"

 
However, users can change other Docker pre-defined environment variable in a same way like “COMPOSE_FILE”, “COMPOSE_PARALLEL_LIMIT”, and “DOCKER_HOST”.

Step 2: Start the Compose Service

Next, fire up the service in container using “docker-compose up” command:

docker-compose up

 
The output shows that we have changed the environment variables successfully:


That’s all about changing pre-defined Docker environment variables.

Conclusion

To change the Docker pre-defined environment variables, users can use different approaches such as change variable in Dockerfile, using “-e” option in “docker run” command, or using “environment” key in Docker compose. To change the environment variable in command, use “docker run -e <Variable-name=“Value”> image-name” command. This article has demonstrated the techniques to change the pre-defined Docker environment variables.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.