docker build
environment variables
dockerfile
docker
containerization

How do I set environment variables during the docker build process?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

When working with Docker, setting environment variables during the build process can be a key technique for configuring your applications or managing different build contexts. Environment variables are used to customize the environment in which a process runs. In the context of Docker, they can be critical for injecting application settings, secrets, or any configuration that might differ between environments like development, staging, and production.

This article will explore various methods for setting environment variables during the Docker build process, highlighting the technical nuances and providing practical examples.

Methods of Setting Environment Variables

1. Using the ENV Instruction in Dockerfile

The ENV instruction in a Dockerfile is the most straightforward way to set environment variables. This method sets a variable for the entire build and for any containers run from the resulting image.

Example:

dockerfile
1# Dockerfile
2FROM alpine:latest
3
4# Set environment variables
5ENV APP_ENV=production
6ENV DB_HOST=localhost
  • Scope: Variables set using ENV are available to all subsequent instructions in the Dockerfile and in any container created from the resulting image.
  • Persistence: They persist in the final image and can be viewed by inspecting the image or container.

2. Setting Build Arguments with ARG and --build-arg

ARG allows you to define variables that are used only during the build process. These are particularly useful for setting environment-specific values at build time without impacting runtime.

Example:

dockerfile
1# Dockerfile
2FROM alpine:latest
3
4# Define build-time environment variables
5ARG APP_VERSION
6ARG API_KEY
7
8RUN echo "Building version $APP_VERSION with API Key $API_KEY"

To pass the build arguments when building the image, use the --build-arg flag in the docker build command:

bash
docker build --build-arg APP_VERSION=1.0 --build-arg API_KEY=12345 -t myapp:latest .
  • Scope: ARG variables are only available during the build process and do not persist in the image.
  • Security: More secure for sensitive data since they do not appear in the final image.

3. The .env File

Docker can automatically load environment variables from a .env file during the build process. This method keeps your Dockerfile clean and organizes environment variables in a separate file.

Example of .env file:

 
DB_USER=root
DB_PASSWORD=secret

When you run the docker-compose command (if using docker-compose), it will automatically read this file and set those variables.

4. Using Docker Compose build Option

In a docker-compose.yml, you can specify build arguments using the build option. This is useful when coordinating related services during a multi-container application deployment.

Example:

yaml
1version: '3.8'
2services:
3  app:
4    build:
5      context: .
6      dockerfile: Dockerfile
7      args:
8        APP_VERSION: "1.0"
9    environment:
10      - APP_ENV=production
  • Flexibility: Simplifies managing multiple build-time variables across multiple services.
  • Organization: Keeps application configuration in one place.

Summary of Key Points

Here's a summary of the key methods to set environment variables during the Docker build process:

MethodScopePersistenceSecurity Considerations
ENV instructionEntire build and containersPersists in imageVisible in image/container
ARG with --build-argBuild process onlyDoes not persistMore secure for sensitive data
.env fileDuring docker-compose usageDeveloper-dependentSecurity depends on file handling
build option in ComposeMulti-service managementFlexible handlingManage sensitive variables globally

Additional Considerations

  • Best Practices: Avoid including sensitive information in your Dockerfile. Use ARG for build-time secrets and external secrets management tools, such as HashiCorp Vault, for runtime secrets.
  • Context-awareness: Be mindful of the build context as Docker sends the entire directory to the Docker daemon during the build process. Use .dockerignore to exclude unnecessary files to avoid leaking sensitive data inadvertently.
  • Inspecting Variables: You can inspect environment variables set for a container by running docker inspect <container_id>.

Setting environment variables correctly during the Docker build process is crucial for creating flexible, configurable, and secure Docker images. It is important to choose the right method depending on the scope and security demands of your application.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.