如何向docker-compose传递参数?v3版本docker-compose.yml配置咨询
Great questions—let’s break down how to handle arguments with Docker Compose, especially for version 3 files.
1. How to Pass Arguments to Docker Compose
There are a few common ways to pass arguments depending on whether you need them for building images or running containers:
Command-line Build Arguments
If you need to pass parameters during the image build process, use the --build-arg flag with docker-compose build or docker-compose up --build. This overrides any args defined in your docker-compose.yml.
Example:
docker-compose up --build --build-arg APP_VERSION=1.2.3 --build-arg BUILD_ENV=production
Shell Environment Variables
You can use shell environment variables directly in your Compose commands, and they’ll be interpolated into your docker-compose.yml file automatically.
First, set the variable in your shell:
export APP_PORT=8080
Then run Compose:
docker-compose up
Your Compose file can reference this variable like ${APP_PORT}.
.env File
For persistent, project-specific variables, create a .env file in your project root. Docker Compose automatically loads variables from this file by default, so you don’t need to export them manually.
Example .env file:
APP_VERSION=2.0.0 APP_PORT=8000 BUILD_DATE=2024-05-20
2. Configuring Arguments in Docker Compose v3 Files
In Compose v3, you define arguments in two key places: build-time args (for image building) and runtime environment variables (for container execution). Here’s how to set them up:
Build-time Arguments (build.args)
These are passed to the Docker build process (you’ll reference them in your Dockerfile with ARG instructions). You can set default values or pull from environment variables.
Example docker-compose.yml:
version: '3.8' services: my-service: build: context: . args: # Default value if no external variable is provided APP_VERSION: 1.0.0 # Reference an environment variable (from shell or .env) BUILD_ENV: ${BUILD_ENV:-development} ports: - "${APP_PORT:-80}:80"
In your Dockerfile, you’d use these args like:
ARG APP_VERSION ARG BUILD_ENV LABEL app_version=$APP_VERSION ENV NODE_ENV=$BUILD_ENV
Runtime Environment Variables
For variables that need to be available inside the running container, use environment or env_file:
Using environment
Define variables directly in the Compose file, with optional defaults:
services: my-service: # ... build config ... environment: - DB_HOST=db - DB_PORT=${DB_PORT:-5432}
Using env_file
Load variables from a specific file (instead of the default .env):
services: my-service: # ... build config ... env_file: - ./config/my-service.env
The my-service.env file would have key-value pairs like DB_USER=admin.
Quick Note on the Official Docs' [ARGS...]
When the docs mention [ARGS...], they’re referring to the command-line arguments you can pass to Compose commands (like --build-arg for builds) or variables that get interpolated into the Compose file. The examples above cover how to use these in practice.
Hope this clears up how to work with arguments in Docker Compose—feel free to ask follow-up questions if you need more specifics!
内容的提问来源于stack exchange,提问作者basickarl

