Docker
Dockerfile
Environment Variables
Docker Build
DevOps

How to get an environment variable value into Dockerfile during docker build?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Incorporating environment variables into a Dockerfile during the docker build process is a useful technique for creating more versatile and configurable Docker images. This capability allows you to inject configuration details that may vary between environments, such as database credentials, API keys, or external service endpoints.

Understanding Docker Build and Environment Variables

The docker build command interprets a Dockerfile to construct an image. By default, environment variables defined on your host system are not directly available to the Dockerfile during the build process, as each command in the Dockerfile runs in a new environment. However, Docker provides mechanisms to pass these variables to alter build-time behavior or configuration.

Passing Build-time Variables

Docker offers the --build-arg flag with docker build to pass environment variables. These variables are consumed as build arguments within the Dockerfile.

Using Build Arguments in Dockerfile

To utilize environment variables during the docker build, follow these steps:

  1. Define an ARG placeholder in your Dockerfile: You must define each build argument you intend to use in your Dockerfile using the ARG instruction.
dockerfile
1   # Dockerfile
2   FROM ubuntu:20.04
3
4   ARG MY_VARIABLE
5   RUN echo "The value of MY_VARIABLE is: $MY_VARIABLE"
  1. Pass the variable during build: When you build the image, use the --build-arg option to pass a value for the build argument.
bash
   docker build --build-arg MY_VARIABLE=HelloWorld -t myimage:latest .

Environment Variables vs. Build Arguments

While build arguments are great for altering the build process, they are not preserved in the final image. They are only available during the build process, enabling different image customizations without modifying the Dockerfile.

For runtime configuration, you should use the ENV instruction:

dockerfile
1# Dockerfile
2FROM ubuntu:20.04
3
4ARG MY_VARIABLE
5ENV MY_VARIABLE_ENV=$MY_VARIABLE
6RUN echo "This is available at runtime: $MY_VARIABLE_ENV"

This setup makes MY_VARIABLE_ENV available in containers created from the image but requires MY_VARIABLE to be set during build via --build-arg.

Practical Example

Here's a practical example that demonstrates both a build-time and runtime environment variable:

dockerfile
1# Dockerfile
2FROM node:14
3
4# Build-time argument
5ARG NODE_ENV
6
7# Runtime environment variable
8ENV NODE_ENV=$NODE_ENV
9
10WORKDIR /app
11
12COPY . .
13
14RUN npm install
15
16CMD ["node", "app.js"]

To build this Docker image while setting a build-time variable, you’d use:

bash
docker build --build-arg NODE_ENV=production -t mynodeapp:latest .

The NODE_ENV variable is set initially at build time and then propagated as a runtime environment variable, which can be verified when you run a container:

bash
docker run --rm mynodeapp:latest node -e "console.log(process.env.NODE_ENV)"

This outputs production, confirming the correct propagation of the environment variable.

Table: Summary of Key Points

FeatureDescriptionExample
Build Argument (ARG)Used to define variables during the image build processARG MY_VARIABLE docker build --build-arg MY_VARIABLE=value -t myimage:latest .
Runtime Environment (ENV)Preserves variables to use within running containersENV MY_VARIABLE=value Used directly in app as process.env.MY_VARIABLE in Node.js
Build-time vs RuntimeARG is for build-time only; ENV survives into runtimeARG doesn't exist in container; ENV accessible after the build

Adding Security and Best Practices

Protecting sensitive data

Use build arguments to inject sensitive data at build time carefully, as values passed via --build-arg can be visible in Docker images under layers using docker history. For sensitive data, consider:

  • Utilizing Docker secrets or encrypted build arguments.
  • Keeping sensitive data strictly for runtime using environment variables injected when containers start.

Version Control and Documentation

Maintain a clear version-controlled history of changes in the Dockerfile, especially when environment variables affect the build process. Annotate each ARG and ENV with comments to clarify its role and use.

Automating Builds

For continuous integration pipelines, manage build variables via CI/CD environment variable settings rather than hardcoding them into scripts and configurations, allowing more flexible deployment configurations.

By effectively leveraging Docker build arguments and environment variables, you can create robust and configurable container images suitable for a variety of use cases while maintaining best practices for security and maintainability.


Course illustration
Course illustration

All Rights Reserved.