Docker file, ARG

How to Pass Environment Variable Value into Dockerfile

Using environment variables offers a convenient method to externalize application configuration, making them valuable for constructing Docker containers. Nonetheless, incorporating and utilizing these variables in the Dockerfile is not as straightforward as it might seem.


In this brief tutorial, we will explore the process of conveying a value for an environmental variable into a Dockerfile.

Initially, we will showcase scenarios in which providing an environmental variable to a build process proves advantageous. Subsequently, we will delve into the utilization of the ARG command to achieve this goal. Lastly, we will examine a functional example.

In a Dockerfile, you can access environment variables using the ENV instruction or by referencing them directly (hardcoded). Here are a few ways to access environment variables in a Dockerfile:

Using the ENV Instruction:

You can use the ENV instruction to set environment variables in your Dockerfile. Here’s an example:

# Set environment variable
ENV MY_VARIABLE=my_value

# Use the environment variable
RUN echo $MY_VARIABLE

In this example, $MY_VARIABLE will be replaced with its value during the build process.

Passing Environment Variables during Build:

You can pass environment variables to the docker build command using the --build-arg option:

docker build --build-arg MY_VARIABLE=my_value -t my_image .

In the Dockerfile, you can use ARG to declare the build argument, and then set an environment variable with the same name:

ARG MY_VARIABLE
ENV MY_VARIABLE=${MY_VARIABLE}

RUN echo $MY_VARIABLE

This allows you to set the value of the environment variable during the build process.

Using ARG Directly:

You can use ARG directly without setting it as an environment variable:

ARG MY_VARIABLE
RUN echo $MY_VARIABLE

In this case, you can pass the value during the build process:

docker build --build-arg MY_VARIABLE=my_value -t my_image .

However, keep in mind that ARG values are only available during the build process and are not persisted in the final image.

Hardcoding vs Environment Variables

You may be pondering why the use of environment variables is essential and why we shouldn’t simply embed configuration parameters directly into our code or Dockerfile. The rationale behind this lies in adhering to sound software development practices and ensuring security.

To begin with, incorporating sensitive information like database passwords or API keys directly into your code is considered poor practice. This approach compromises the security of your application because anyone with access to your codebase can easily view these sensitive details. This is where the significance of environment variables becomes evident. Storing such sensitive information as environment variables allows you to segregate them from your code, thereby enhancing the security of your application.


// Unrecommended approach
const dbPassword = 'mypassword';

// Preferred approach
const dbPassword = process.env.DB_PASSWORD;

Example of setting environment variables in Docker:

# Dockerfile

ENV DB_PASSWORD=mypassword

Advantages of Using Environment Variables inĀ Dockerfile


The primary benefit of utilizing environment variables is the enhanced flexibility they offer. A single Dockerfile can be crafted to configure the container differently based on the environment used for its construction. To illustrate, consider an application with debug options activated in the development environment but deactivated in the production environment. Leveraging environment variables allows us to employ a single Dockerfile, passing an environment variable containing the debug flag to the container and the enclosed application.

Another crucial advantage is the mitigation of security concerns. It is generally not advisable to store passwords or other sensitive information directly in the Dockerfile. Environment variables serve as a solution to this issue.

Ways to set environment variables with Compose

With Compose, there are multiple ways you can set environment variables in your containers. You can use either your Compose file, or the CLI.

Compose file

In Docker Compose, an .env file serves as a text file designed to specify environment variables that should be accessible to Docker containers when executing docker compose up. Typically, this file comprises key-value pairs representing environment variables, offering a centralized means of organizing and managing configuration. The .env file proves advantageous when dealing with multiple environment variables that require storage.

As the default approach for configuring environment variables within containers, the .env file is expected to reside at the root of the project directory, alongside the compose.yaml file.

cat .env
TAG=v1.5

cat compose.yml
services:
  web:
    image: "webapp:${TAG}"

CLI

Default values for various environment variables can be established in an environment file, which can then be provided as a parameter in the command-line interface (CLI).

This approach offers flexibility, allowing you to store the file in any location with a suitable name. For instance,

The file path is relative to the present working directory where the Docker Compose command is initiated. To pass the file path, you can utilize the –env-file option:

docker compose --env-file ./config/.env.dev up

Conclusion

In this post, we explored the process of configuring environment variables in the construction of a Dockerfile.

Initially, we highlighted the benefits of making our Dockerfile parameterized. Subsequently, we provided a tutorial on employing the ENV command to establish an environment variable and showcased the application of ARG to enable the modification of this value during the build phase.