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:
- Define an ARG placeholder in your Dockerfile: You must define each build argument you intend to use in your Dockerfile using the
ARGinstruction.
- Pass the variable during build: When you build the image, use the
--build-argoption to pass a value for the build argument.
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:
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:
To build this Docker image while setting a build-time variable, you’d use:
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:
This outputs production, confirming the correct propagation of the environment variable.
Table: Summary of Key Points
| Feature | Description | Example |
Build Argument (ARG) | Used to define variables during the image build process | ARG MY_VARIABLE
docker build --build-arg MY_VARIABLE=value -t myimage:latest . |
Runtime Environment (ENV) | Preserves variables to use within running containers | ENV MY_VARIABLE=value
Used directly in app as process.env.MY_VARIABLE in Node.js |
| Build-time vs Runtime | ARG is for build-time only; ENV survives into runtime | ARG 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.

