Open In App

What Docker Environmental Variables ?

Last Updated : 30 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In a Docker has entered into the developer's working culture today as a forefront system of development, testing, and deployment of applications. Docker is an important feature as it provides the users or developers with the ability to not only monitor and control the environment but to also build the application environment efficiently and securely In this article we will discuss the Docker environment switches in detail, how they work and what are the things to be followed while using them.


What are Docker Environment Variables?

The normal Docker environment variables are fixed values that are normally set for all the applications running in the Docker environment. People typically use these variables for characterizing the nature of an application or a script, when setting the Docker image to use, or for storing other credentials such as the database credentials or API keys.

Likewise, environment variables in Docker are also associated with the use of the portability degrees and flexibility of the container. Environment variables allow the change of some parameters when placing the container somewhere without losing the entire image, reconstructing the configuration at runtime and so make the container portable.

Step-By-Step Guide to Set Docker Environment Variables

Step 1: How to set environment variables in a Dockerfile

1. A Dockerfile contains all the facets that are required to build a Docker image. In Dockerfile you can define two types of variables: In Dockerfile you can define two types of variables:

  • Type 1: ENV - environment variables
  • Type 2: ARG - build time variables

2. In ENV of a container holds environment variables to be imposed and used by every container that is adopted from the image. for The variables you can specify are expressed in the following format.

ENV: env_variable_name=value

3. The following presents an example of how to use environment variables in a Dockerfile:

Example of how to use environment variables in a Dockerfile

Step 2: How to a view environment variables in a Docker

1. Use a docker for inspect command

1. You can look the environment variables which you have set in the container or the docker image by using the docker inspect command and by searching for the Env portion of the JSON. If you do not provide repository or image ID you will get error message.

docker inspect <containerId>/<Image_id>

2. Use the following format of the inspect command and inspect only the environment at any given level.

docker inspect --format '{{.Confing.Env}}' <containerId>/<Image_id>
docker inspect command
docker images

2. Use the docker exec command

1. In Another way of looking at the system environment changes is the Docker exec command To save time, I will present some changes in tabular form as mentioned in the description below. By a using the docker exec command in the format.

docker exec <containerID> env
Docker exec command

Step 3: How to a set and override Docker environment variables using the docker run command

1. Use the -e option

1. You can be use the -e flag to set or override environment variables when creating object. In a Number You can use the -e flag specify multiple variables.

docker run -e env_var=value <imageName>
Use the -e option

2. You can check that it is actually set by checking the images container id as mentioned in the previous section.

Env file

2. Use the --env-file option

1. In Another way to force or override in environment variables is to be use the --env-file option.

docker run --env-file <env_file_name> <imageName>

2. For a example, we can defined a following environment in dev.env file.

API_KEY=WENMCOMnhfwDWER
MONGODB_URL=https://monogodbURL:uname:pw
dev.env file
Env file

Step 4: How to a set and override to Docker environment variables using the Docker Compose

In a Setting Docker environment variables at run time using the CLI is not a good practice for multi-container applications. for Instead we can use a special configuration file docker-compose.yaml to define all environment variables. for this file contains all the settings for the backup, and can be used with a single command when creating the backup.

Set and override to docker environment
Env file3

1. Use the environment attribute in docker-compose.yaml file.

1. In a typical Docker-compose.yaml file looks like this. You can be define environment variables in the Environment section.

services:
web:
build:
environment:
- key1="value1"
- key1="value1"
11

2. In a Run the application from command prompt and execute following commands. It will works like docker run -e key=value command.

docker compose up

3. In a open new terminal window and try docker compose run command with following mentioned command.

docker compose run web env
docker compose run command

2. For Specify separate environment variable for a file in docker-compose.yaml file.

1. In a Environment variables can be defined in a separate file and that file can be mapped in the docker-compose through the env_file section. yaml file. As example, so as to illustrate let us develop an . In env file provide the following environment variables with respective values.

REDIS_PASSWORD=test
LOG_LEVEL=INFO

3. for Include by method in docker-compose. yaml file as a follow..

services:
web:
build:
ports:
- "8000:5000"
env_file:
- .env
redis:
image: "redis:alpine"
docker compose run web env

Conclusion

In a Docker environment variables help in controlling layout of applications that are created using containers. Learning how to be apply it correctly helps you make your applications portable, safe and less prone to require constant updates


Next Article
Article Tags :

Similar Reads