Dockerfile
Docker arguments
passing arguments
Docker build
containerization

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.

Practice system design

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

dockerfile
1# Dockerfile
2FROM ubuntu:20.04
3
4# Declaring a build-time argument
5ARG my_variable
6
7# Using argument inside Dockerfile
8RUN echo "The value of my_variable is: $my_variable"

Passing Arguments at Build Time

To pass a value to my_variable during the build, use the --build-arg option with docker build:

bash
docker build --build-arg my_variable=hello -t myimage .

Default Values for Arguments

You can also set default values for arguments inside a Dockerfile:

dockerfile
1# Dockerfile
2FROM ubuntu:20.04
3
4# Declaring a build-time argument with default value
5ARG my_variable=default_value
6
7RUN echo "The value of my_variable is: $my_variable"

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:

dockerfile
1# Dockerfile
2FROM ubuntu:20.04
3
4ARG my_variable
5ENV my_env_variable=my_value
6
7RUN echo "ARG: $my_variable"
8RUN echo "ENV: $my_env_variable"

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:

dockerfile
1# Dockerfile
2FROM ubuntu:20.04
3
4ARG environment=production
5
6RUN if [ "$environment" = "production" ]; then \
7        echo "Building for production"; \
8    else \
9        echo "Building for testing"; \
10    fi

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

AspectDescription
InstructionARG <name>[=<default>]
Build Syntaxdocker build --build-arg <name>=<value> -t <image-name> .
Default ValueUse ARG <name>=<default_value>
ScopeAvailable only for the stage they are declared in
ComparisonARG for build-time, ENV for runtime and build-time
SecurityNot secure for sensitive data (remains in image metadata)
Key BenefitAllows 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
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.