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.
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:
- Scope: Variables set using
ENVare 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:
To pass the build arguments when building the image, use the --build-arg flag in the docker build command:
- Scope:
ARGvariables 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:
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:
- 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:
| Method | Scope | Persistence | Security Considerations |
ENV instruction | Entire build and containers | Persists in image | Visible in image/container |
ARG with --build-arg | Build process only | Does not persist | More secure for sensitive data |
.env file | During docker-compose usage | Developer-dependent | Security depends on file handling |
build option in Compose | Multi-service management | Flexible handling | Manage sensitive variables globally |
Additional Considerations
- Best Practices: Avoid including sensitive information in your Dockerfile. Use
ARGfor 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
.dockerignoreto 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
- How do I set hostname in docker-compose?
- How do I specify nvidia runtime from docker-compose.yml?
- How do I specify the model_config_file variable to tensorflow-serving in docker-compose?
- How do I start tensorflow docker jupyter notebook
- How do I tell if my container is running inside a Kubernetes cluster?
- How do I update the args for a Kubernetes deployment
- How do I use Docker environment variable in ENTRYPOINT array?
- How do I work out why an ECS health-check is failing?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.