Technical Implementation and Best Practices for Passing Build Arguments in Docker Compose

Dec 01, 2025 · Programming · 10 views · 7.8

Keywords: Docker Compose | Build Arguments | Dockerfile

Abstract: This article provides an in-depth exploration of the technical implementation for passing build arguments to Dockerfile within Docker Compose. Based on Docker Compose file format 1.6 and later versions, it详细解析了如何在docker-compose.yml文件中使用args配置项来定义构建时参数,并通过具体代码示例展示了实际应用场景。同时,文章还对比了环境变量替代机制与构建参数的区别,分析了参数优先级规则,为开发者在容器化部署中实现灵活的配置管理提供了全面的技术指导。

Build Argument Passing Mechanism in Docker Compose

In the Docker ecosystem, passing build arguments is a crucial technique for achieving flexible configuration of container images. Docker 1.9 introduced the ARG instruction in Dockerfile, allowing variables to be defined and used during the build process. With the continuous development of Docker Compose, starting from Compose file format version 1.6, direct passing of arguments to the build process within the docker-compose.yml file has been officially supported. This feature significantly simplifies configuration management for multi-environment deployments.

Basic Syntax and Configuration of Build Arguments

In the docker-compose.yml file, build argument configuration is placed inside the service's build object. The following is a standard configuration example:

services:
  web:
    build:
      context: .
      args:
        FOO: foo
        BAR: bar

In this configuration, the args section defines two build arguments, FOO and BAR, with values foo and bar respectively. These arguments are passed to the corresponding Dockerfile when executing the docker-compose build command.

Argument Reception and Usage in Dockerfile

To receive build arguments from docker-compose.yml, the Dockerfile must declare them using the ARG instruction. Here is a complete example:

# Dockerfile
FROM alpine:latest

# Declare build arguments
ARG FOO
ARG BAR

# Use build arguments
RUN echo "FOO value: ${FOO}" && echo "BAR value: ${BAR}"

# Build arguments can also be used for environment variable settings
ENV MY_FOO=${FOO}
ENV MY_BAR=${BAR}

When docker-compose build is executed, Docker Compose passes the argument values defined in args to the build process, and ${FOO} and ${BAR} in the Dockerfile are replaced with the corresponding values.

Relationship Between Build Arguments and Environment Variables

It is important to note the interaction between build arguments and environment variables. In Docker Compose, if both a build argument and an environment variable with the same name exist, the environment variable takes precedence. Consider the following configuration:

services:
  web:
    build:
      context: .
      args:
        DATABASE_URL: postgres://default:5432
    environment:
      DATABASE_URL: postgres://production:5432

In this example, although DATABASE_URL is defined in args, because there is an environment variable with the same name in the environment section, the build will actually use the environment variable value postgres://production:5432. This design allows runtime configurations to override build-time configurations, providing flexibility for different deployment environments.

Supplementary Explanation of Variable Substitution Mechanism

In addition to the build argument passing mechanism, Docker Compose also supports variable substitution based on shell environment variables. This mechanism is primarily used for dynamic settings such as image tags, for example:

services:
  db:
    image: "postgres:${POSTGRES_VERSION}"

When executing docker-compose up, Compose reads the value of the POSTGRES_VERSION variable from the current shell environment and substitutes it into the configuration. This mechanism is fundamentally different from build argument passing: variable substitution occurs during the configuration parsing phase, while build argument passing occurs during the image building phase.

Practical Application Scenarios and Best Practices

In actual development, the build argument passing mechanism is particularly suitable for the following scenarios:

  1. Multi-environment Configuration Management: By defining different build argument values, different image variants can be built for development, testing, production, and other environments.
  2. Sensitive Information Handling: Passing sensitive information such as database passwords and API keys as build arguments avoids hardcoding them in the Dockerfile.
  3. Version Control: Passing parameters like software version numbers and dependency versions enables versioned image building.

Best practice recommendations include:

Version Compatibility and Migration Recommendations

The build argument passing feature requires Docker Compose file format 1.6 or later. The version must be explicitly specified at the beginning of the file:

version: '3.8'
services:
  # Service configuration...

For projects migrating from older versions, it is recommended to gradually introduce the build argument mechanism, first testing it on non-critical services to ensure compatibility with existing build processes. Additionally, update relevant documentation and provide training for team members.

Conclusion

The build argument passing mechanism in Docker Compose provides powerful flexibility for configuration management of containerized applications. By properly utilizing the args configuration item, developers can achieve dynamic injection of build-time parameters. Combined with the environment variable override mechanism, it can meet complex and varied deployment requirements. As the Docker ecosystem continues to evolve, this feature will continue to play an important role in DevOps practices.

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.