How to Build Images With Compose in a CI Pipeline
docker compose build builds every service that has a build section, using the same file your app runs from so images match production.
Run docker compose build to build all services, or name specific ones. Combine with docker compose push to publish tags once the build succeeds.
Steps
- Add a
build:context (and optionalimage:tag) to each service. - Run
docker compose buildin CI. - Push with
docker compose pushafter a successful build.
Compose file and CI
docker-compose.yml
services:
api:
build:
context: .
dockerfile: Dockerfile
image: registry.example.com/api:${GIT_SHA}CI step
.github/workflows/ci.yml
steps:
- run: GIT_SHA=${{ github.sha }} docker compose build
- run: GIT_SHA=${{ github.sha }} docker compose pushGotchas
- A service needs an
image:name fordocker compose pushto know where to publish it. - Set
--pullon build to refresh base images so CI does not use a stale cached base.
Related guides
How to Cache Compose Image Builds in CISpeed up docker compose build in CI with BuildKit inline cache or a registry cache backend so unchanged layer…
How to Build Compose Services With docker compose bake in CIUse docker compose bake to build all services through Buildx Bake for parallel, cache-aware image builds driv…
How to Choose docker compose run vs up in CIUse docker compose up to start long-running dependencies and docker compose run for a one-off test command, u…