docker compose build command
The docker compose build command is used to build service images based on the definitions in the docker-compose.yml file.
docker compose build builds images from the specified Dockerfile or build context and prepares containers for all services.
Syntax
docker compose build [OPTIONS] [SERVICE...]
- SERVICE(Optional): Specify the service name to build. If not specified, images will be built for all services.
OPTIONS:
--no-cache: Do not use cached layers during the build process, forcing the image to be built from scratch.--pullAlways try to pull the latest base image from the registry.--build-arg: Pass build-time variables (similar to Docker's--build-argOptions).--progressSpecify the progress style of the build (auto、plain、tty), which affects the displayed output.--parallel: Build images for multiple services in parallel to improve speed.--no-rm: Keep intermediate containers when the build fails (by default, intermediate containers are removed after success or failure).
Example
1. Build images for all services
docker compose build
Build images for all services according to the configuration in docker-compose.yml.
2. Build images for a specific service
docker compose build web
Build images only for the web service.
3. Build images without using cache
docker compose build --no-cache
Force Docker to build all images from scratch, without using previously built cached layers.
4. Build from the latest base image
docker compose build --pull
Ensure Docker pulls the latest base image instead of using the local image.
5. Pass build variables
docker compose build --build-arg NODE_ENV=production
Use --build-arg to pass variables required at build time, for example, passing the NODE_ENV environment variable.
other extensions