docker compose build Command
The docker compose build command builds 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 names of the services to build. If omitted, images will be built for all services.
OPTIONS:
--no-cache: Do not use cache layers during the build, forcing images to be built from scratch.--pull: Always try to pull the latest base image from the registry.--build-arg: Pass build-time variables (similar to Docker's--build-argoption is used).--progress: Specify the build progress style (auto、plain、tty), which affects how the output is displayed.--parallel: Build multiple service images in parallel for speed.--no-rm: Keep intermediate containers when a build fails (by default, intermediate containers are removed after both success and 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 specific services**
docker compose build web
Build the image only for the web service.
** 3. Build images without cache**
docker compose build --no-cache
Force Docker to build all images from scratch without using previously built cache layers.
** 4. Build from the latest base image**
docker compose build --pull
Make sure Docker pulls the latest base image instead of using the local one.
** 5. Pass build variables**
docker compose build --build-arg NODE_ENV=production
Pass the variables needed at build time through --build-arg, for example the NODE_ENV environment variable.
Docker Command Reference