How to pass arguments to a Dockerfile?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Understanding Dockerfile Arguments
Docker is a powerful tool for creating containerized applications. One important aspect of Docker is the ability to pass arguments to a Dockerfile, providing flexibility and dynamics at build time. This article will guide you through the different methods of passing arguments to a Dockerfile, enhancing your understanding of this crucial component.
What Are Dockerfile Arguments?
A Dockerfile is a script containing a series of instructions on how to build a Docker image. Arguments allow you to inject dynamic values into a Dockerfile at build time, enabling customizable and parameterized image builds.
Using the ARG Instruction
The ARG instruction in Dockerfile declares a variable that users can pass at build-time to the builder with the docker build command using the --build-arg flag.
Basic Syntax
Passing Arguments at Build Time
To pass a value to my_variable during the build, use the --build-arg option with docker build:
Default Values for Arguments
You can also set default values for arguments inside a Dockerfile:
Scope of Arguments
The scope of an ARG variable is limited to the part of the Dockerfile following its declaration. Any RUN command that follows can use the argument, but subsequent image layers created do not store the argument.
Environment Variables vs. Arguments
While arguments are available at build time, environment variables defined with the ENV instruction are available both at build-time and runtime:
Conditional Builds with Arguments
Arguments can be effectively used to build different variants of an image. For instance, compiling an application for different environments (production, testing) or varying dependencies:
Special Considerations
- Arguments remain in the image metadata and are not secure for sensitive data such as passwords.
- If you define an argument in a Dockerfile but do not utilize it, Docker will not throw an error.
Troubleshooting Argument Usage
- Ensure all
RUN,ENV, or any other instructions that utilize arguments are placed after the argument is declared. - Check whether the Docker engine is updated, as argument support varies with versions.
Summary Table
| Aspect | Description |
| Instruction | ARG <name>[=<default>] |
| Build Syntax | docker build --build-arg <name>=<value> -t <image-name> . |
| Default Value | Use ARG <name>=<default_value> |
| Scope | Available only for the stage they are declared in |
| Comparison | ARG for build-time, ENV for runtime and build-time |
| Security | Not secure for sensitive data (remains in image metadata) |
| Key Benefit | Allows dynamic and customizable image building |
Conclusion
By using Dockerfile arguments, you can create more flexible and dynamic builds, allowing you to tailor Docker images according to specific needs or environments. Whether you're building for production or staging, using arguments offers a streamlined and efficient approach to managing Docker images.
Related reading
- How to pass arguments to Shell Script through docker run
- How to pass Docker CLI --gpus Options in Kubernetes or enable GPU support without installing nvidia-docker2 Docker 19.03
- How to pass docker run parameter via kubernetes pod
- How to pass environment variable to docker-compose up
- How to pass image pull secret while using 'kubectl run' command?
- How to perform kaniko Docker build and push in separate GitLab CI stages?
- How to persist data in a dockerized postgres database using volumes
- How to persist data using a postgres database, Docker, and Kubernetes?

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.