Linux

How to Setup Docker Environment Variables? Explain With Examples

Introduction 

Did you know that Docker is responsible for managing the scaling of applications? It’s an open-source platform that works through containerization. Each container houses the dependencies of different applications, making it easier to update and upscale them without disturbing the others. 

Environment variables are responsible for transporting information to containers during runtime. They’re instrumental if you want to change the behavior of an application or software without editing the code. 

This article will delve into the various aspects of Docker’s environment variables, including their types and setup. Be sure to read the conclusion to understand the topic completely.

Types of Docker Environment Variables 

Types of Docker Environment Variables 

Docker allows you to set environment variables within containers to configure various aspects of your application or runtime environment. Here are the different types of Docker environment variables:

User-defined Variables

You define custom environment variables to suit your application’s needs. You can set them using the -e or –env flag when running a container or by specifying them in the Docker Compose file using the environment key.

Built-in Variables

Docker provides several built-in environment variables that you can use to access information about the container or runtime environment. Some commonly used built-in variables include:

  • HOSTNAME: The hostname of the container.
  • HOME: The home directory path.
  • PATH: The system’s default search path for executables.
  • PWD: The present working directory.
  • USER: The username of the user running the container.
  • TERM: The terminal type.

Image-specific Variables: 

Some Docker images define their environment variables, which are specific to the image’s functionality or configuration. These variables are typically documented by the image maintainer, and you can set them similarly to user-defined variables.

Container Linking Variables

When you link containers together using Docker’s deprecated container linking feature, Docker automatically sets environment variables in the linked containers. These variables provide information about the linked services, such as their IP addresses and exposed ports.

Global Variables: 

Docker supports global environment variables that apply to all containers on a Docker host. These variables are set in the /etc/environment file of the host and are automatically available within containers. However, modifying global environment variables requires host-level access.

Secret Variables

Docker Swarm mode supports secret variables, which allow you to securely store sensitive information, such as passwords or API keys, as environment variables. Secrets are stored securely and made available to containers without exposing the actual values in plaintext.

How to Set Docker Environment Variables? 

How to Set Docker Environment Variables? 

There are several ways to set environment variables in Docker containers. Here are the most common methods:

Using the -e or –env Flag

When running a container with the docker run command, you can set environment variables using the -e or –env flag followed by the variable name and its value.

Using a Docker Compose file

If you are using Docker Compose to manage your containers, you can set environment variables in the environment section of your Compose file.

Using An Environment File

Docker allows you to specify environment variables in a file and then pass that file to the container using the –env-file flag. Create a file with each variable on a new line in the format VARIABLE_NAME=variable_value, and then run the container with the –env-file flag.

How to Set Docker Environment Variables

Also Read: How to Use Docker Run Command – A Complete Guide

Benefits of Using Environment Variables in Dockerfile 

Benefits of Using Environment Variables in Dockerfile 

Using environment variables in a Dockerfile offers several benefits:

Configuration Flexibility: 

By using environment variables, you can parameterize your Docker image and make it more configurable. Instead of hard-coding values directly in the Dockerfile, you can use variables that can be easily modified based on different deployment environments or specific runtime configurations. This allows you to create a more flexible and reusable image.

Separation Of Concerns

Environment variables enable a separation of concerns between the application code and its configuration. By defining configuration values as environment variables, you can keep the Dockerfile focused on the image-building process while the configuration details are managed separately. This separation simplifies image maintenance and allows configuration changes without modifying the Dockerfile or rebuilding the image.

Easier Image Customization

Environment variables provide an easy way to customize the behavior of your containerized application without modifying the image itself. By setting environment variables when running a container, you can override default configuration values specified in the Dockerfile. This allows different instances of the same image to be customized based on specific requirements or individual deployment environments.

Improved Security

Environment variables can be used to securely store sensitive information, such as API keys or database credentials, without exposing them directly in the Dockerfile or container runtime command. Instead of embedding sensitive information in the image, you can pass them as environment variables at runtime or use Docker Swarm secrets for further security.

Enhanced Portability

Using environment variables in a Dockerfile makes your image more portable across different environments and platforms. By relying on variables instead of hard-coded values, you can create a Docker image that works consistently across development, staging, and production environments without modifications. This flexibility simplifies the deployment process and reduces the risk of configuration-related issues.

Version Control Friendliness

Storing configuration values as environment variables makes it easier to manage changes in version control systems. Since the Dockerfile does not contain specific configuration values but rather references to environment variables, it becomes easier to track and manage changes to the image and its configuration separately.

Benefits of Using Environment Variables in Dockerfile 

Passing Environment Variable to Docker 

Passing Environment Variable to Docker 

1. Pass env vars to a Container With Docker Run 

Passing Environment Variable to Docker 

Environment variables are accessible to processes running inside the container, allowing for configuration and customization of the containerized application.

The command “docker run -e VARIABLE_NAME=value IMAGE_NAME” has been implemented to run a Docker container that is based on a specified image (IMAGE_NAME). This command facilitates flexibility and ease of deployment in various environments.

2. Pass env Files to a Container with Docker Run 

Passing Environment Variable to Docker 

The above syntax helps launch a Docker container using the specified image (IMAGE_NAME). This approach simplifies the management of environment configurations by allowing variables to be stored separately from the Docker command. The user can get a seamless configuration of containerized applications through this command.

3. Pass env vars to a Container with Docker-Compose 

Passing Environment Variable to Docker 

The above configuration defines a service named “my_service”. It specifies an image (IMAGE_NAME) to be used for the service. The user can set an environment variable within the container named VARIABLE_NAME. 

4. Pass env Files to a Container with Docker-compose 

Passing Environment Variable to Docker 

In the above syntax, a service named “my_service” is defined. The file located at “path/to/env_file” is referenced to load environment variables into the service. Each variable in the file follows the format VARIABLE_NAME=value. 

Also Read: How to SSH into a Docker Container and Run Commands?

Conclusion

Customizing and configuring containerized applications becomes more flexible and secure with Docker environment variables. You can separate concerns, modify behavior, improve portability, and enhance security by utilizing environment variables. Regardless of whether you set environment variables through Docker Compose files, command-line flags, environment files, or Dockerfiles, the benefits remain the same. Ultimately, utilizing environment variables in a Dockerfile improves the maintainability, flexibility, security, and portability of your Docker images. This feature enables you to create more customizable and configurable containers to accommodate varying environments and requirements.

Arpit Saini

He is the Chief Technology Officer at Hostbillo Hosting Solution and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *