Docker

How to pass arguments to a Dockerfile

25 September 2026 · 5 min read

How to pass arguments to a Dockerfile

Building efficient and reproducible Docker images is crucial for modern software development. One of the most powerful techniques for achieving this is by passing arguments to your Dockerfile. This allows you to customize the image build process based on various needs, such as different environments, dependencies, or build configurations. This article will delve into the intricacies of passing arguments to a Dockerfile, empowering you to create more flexible and dynamic images.

Understanding Dockerfile Arguments

Dockerfile arguments, also known as build arguments, are variables declared within the Dockerfile using the ARG instruction. They act as placeholders that can be populated with values during the image build process. This mechanism provides a way to inject dynamic information into the build, avoiding the need to hardcode values directly in the Dockerfile. This promotes maintainability and makes your images more versatile.

Using arguments offers significant advantages in terms of image customization and security. For instance, you can use arguments to specify different versions of software dependencies or to configure environment-specific settings without rebuilding the entire image from scratch. This not only saves time but also reduces the risk of introducing errors.

For example, you might define an argument for the version of Node.js you want to install in your image, allowing you to easily switch between different Node.js versions during the build process without modifying the Dockerfile itself.

Defining Arguments with ARG

The ARG instruction is used to declare arguments within the Dockerfile. Its syntax is straightforward: ARG name=defaultValue. The name is the identifier for your argument, and the optional defaultValue is used if no value is provided during the build. Best practice dictates that you always provide a default value. This ensures predictable behavior and prevents build failures when an argument is not explicitly provided.

Consider a scenario where you want to specify the version of a particular library. You could define an argument like this: ARG LIBRARY_VERSION=1.0.0. This sets a default value of 1.0.0, which will be used unless overridden during the image build.

Understanding the scope of arguments is important. Arguments defined before the FROM instruction are only available during the build of the base image and not during the subsequent steps. Arguments defined after the FROM instruction are accessible in all subsequent steps. This distinction allows for fine-grained control over the build process.

Passing Arguments During the Build

You can pass arguments to the Dockerfile during the docker build process using the --build-arg flag. The syntax is: docker build --build-arg name=value . This overrides the default value set in the Dockerfile with the provided value.

Let’s revisit our library version example. To build the image with a different version, say 1.2.0, you would execute: docker build --build-arg LIBRARY_VERSION=1.2.0 . This command instructs Docker to use 1.2.0 for the LIBRARY_VERSION argument during the build process.

Using build arguments effectively makes your Dockerfiles much more flexible. They allow you to tailor the resulting image to specific requirements without altering the Dockerfile itself, a key principle in maintaining clean and reusable Docker images. This makes it much easier to manage different configurations and deploy to various environments.

Best Practices and Advanced Techniques

Using arguments effectively enhances the flexibility and maintainability of your Dockerfiles. However, some best practices should be followed to ensure optimal results. Avoid using arguments for sensitive information like passwords or API keys. These should be handled through secure mechanisms like Docker secrets.

You can use arguments in combination with environment variables within your Dockerfile. However, it’s important to understand their differences. Arguments are used during the build process to customize the image creation, while environment variables are accessible within the running container. Combining these two features allows you to create highly configurable images tailored for various deployment environments.

Multi-stage builds in Docker, coupled with build arguments, provide a powerful way to create optimized images. You can use arguments to control which stages are executed, allowing you to create different variants of your image from a single Dockerfile. For example, you might have a stage for development with debugging tools and a separate stage for production without these tools. Build arguments can determine which stage is used for the final image.

  • Use ARG for build-time configurations.
  • Avoid using ARG for sensitive data.
  1. Define ARG in your Dockerfile.
  2. Use –build-arg during docker build.
  3. Access the argument value within the Dockerfile.

“Docker’s build arguments provide a powerful mechanism for creating highly customizable and maintainable images, streamlining your development workflow.” - John Doe, Docker Expert

See how Courthouse Zoological leverages Docker to manage their complex infrastructure.

[Infographic Placeholder]

FAQ

Q: What is the difference between ARG and ENV in a Dockerfile?

A: ARG instructions set build-time variables that are used during the image build process, while ENV instructions set environment variables that persist within the running container.

By effectively utilizing Dockerfile arguments, you can significantly improve the flexibility and maintainability of your Docker images, allowing for easier customization and deployment across various environments. Employing best practices and understanding the nuances of build arguments empowers you to create more efficient and robust Docker workflows.

Ready to streamline your Docker builds? Explore our comprehensive guide on Docker best practices and take your containerization skills to the next level! Learn more about advanced Docker techniques and how to optimize your Docker images for performance and security. Discover resources and tools to enhance your Docker workflow. Dive deeper into the world of containerization and unlock the full potential of Docker.

External Resources:

Question & Answer :
I am using RUN instruction within a Dockerfile to install a rpm

RUN yum -y install samplerpm-2.3 

However, I want to pass the value “2.3” as an argument. My RUN instruction should look something like:

RUN yum -y install samplerpm-$arg 

where $arg=2.3

As of Docker 1.9, You are looking for --build-arg and the ARG instruction.

Check out this document for reference. This will allow you to add ARG arg to the Dockerfile and then build with

docker build --build-arg arg=2.3 .