Complete Guide to Multiple Argument Passing in Docker Build: Correct Usage of --build-arg

Nov 19, 2025 · Programming · 39 views · 7.8

Keywords: Docker Build | Build Arguments | Multi-Argument Passing | ARG Instruction | Environment Variables

Abstract: This article provides an in-depth exploration of how to correctly use the --build-arg parameter for passing multiple build-time variables during Docker image construction. By analyzing common error cases, it explains the proper syntax for multi-argument passing and combines this with the declaration requirements of ARG instructions in Dockerfiles to offer comprehensive solutions. The discussion extends to the distinction between build-time arguments and runtime environment variables, along with optimization strategies for large-scale parameter scenarios, helping developers build more efficient and maintainable Docker images.

Fundamental Principles of Docker Build Argument Passing

In the Docker image building process, build arguments serve as a crucial mechanism for dynamically configuring image content. Through the --build-arg flag, developers can pass specific values in build commands, which can then be referenced in Dockerfiles via the ARG instruction. This mechanism is particularly useful for scenarios requiring different image versions based on various environments or configurations.

Analysis of Common Errors in Multi-Argument Passing

Many developers encounter syntax errors when attempting to pass multiple build arguments. Below are some typical incorrect usages and their underlying issues:

Error Example 1: Missing Repeated Flags

docker build -t essearch/ess-elasticsearch:1.7.6 --build-arg number_of_shards=5 number_of_replicas=2 --no-cache .

This approach causes Docker to misinterpret number_of_replicas=2 as a build context path rather than a parameter definition, resulting in an error.

Error Example 2: Incorrect Separator Usage

docker build -t essearch/ess-elasticsearch:1.7.6 --build-arg number_of_shards=5,number_of_replicas=2 --no-cache .

Using commas to separate arguments leads Docker to treat the entire string "5,number_of_replicas=2" as the value of a single parameter, rather than two distinct arguments.

Correct Method for Multi-Argument Passing

Docker official documentation explicitly requires each build argument to have its own --build-arg flag. The correct syntax for multi-argument passing is as follows:

docker build \
-t essearch/ess-elasticsearch:1.7.6 \
--build-arg number_of_shards=5 \
--build-arg number_of_replicas=2 \
--no-cache .

Advantages of this approach include:

Parameter Declaration in Dockerfile

Merely passing arguments in the build command is insufficient; they must be explicitly declared in the Dockerfile to be usable:

ARG number_of_shards
ARG number_of_replicas

The placement of parameter declarations is critical:

Distinction Between Build Arguments and Runtime Environment Variables

Understanding the difference between ARG and ENV is essential for designing reasonable Docker images:

Build Arguments (ARG)

Runtime Environment Variables (ENV)

Optimization Strategies for Large-Scale Parameter Scenarios

When dealing with a large number of build arguments, consider the following optimization approaches:

Approach 1: Parameter Grouping and Default Values

ARG app_version=1.0.0
ARG db_config=production
ARG cache_size=100

Approach 2: Using Build Scripts

For complex parameter combinations, create build scripts to automate argument passing:

#!/bin/bash
build_args=""
build_args+=" --build-arg version=$1"
build_args+=" --build-arg environment=$2"
build_args+=" --build-arg replicas=$3"

docker build -t myapp:$1 $build_args .

Approach 3: Dockerfile Templating

For performance-critical scenarios, consider using template tools to pre-generate static Dockerfiles without ARG instructions, avoiding the overhead of intermediate container creation.

Best Practice Recommendations

Based on practical project experience, we recommend the following best practices:

Conclusion

Correct usage of Docker's --build-arg parameter is fundamental to building flexible and configurable images. By employing separate --build-arg flags for each parameter and properly declaring them in Dockerfiles, developers can fully leverage the power of build-time arguments. Additionally, understanding the appropriate contexts for ARG versus ENV, along with optimization strategies for large-scale parameter situations, will contribute to creating more efficient and maintainable Docker image build processes.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.