5.3.3.5 CI Stage Flexibility
A focused guide to CI Stage Flexibility, connecting core concepts with practical Docker and container operations.
CI stage flexibility is the benefit a CI pipeline gains from being able to target specific stages of a multi-stage Dockerfile independently, running tests, linting, or producing different image variants as distinct, separately reportable pipeline steps, all from a single shared Dockerfile definition.
Structuring a Pipeline Around Multiple Targets
A CI pipeline can invoke the same Dockerfile multiple times with different --target values, each serving a distinct purpose within the overall pipeline.
docker build --target lint -t myapp:lint . && docker run --rm myapp:lint
docker build --target test -t myapp:test . && docker run --rm myapp:test
docker build --target build -t myapp:latest .
Each step uses the same underlying Dockerfile, but produces and runs a distinct, purpose-specific image corresponding to a different point in the overall build process.
Reporting Each Stage's Result Independently
Running each targeted stage as its own distinct pipeline step allows a CI system to report success or failure for each one independently, making it immediately clear which specific aspect of the build (linting, testing, the actual application build) failed if something goes wrong.
- name: Lint
run: docker build --target lint -t myapp:lint .
- name: Test
run: docker build --target test -t myapp:test .
- name: Build
run: docker build --target build -t myapp:latest .
Caching Benefits Across Multiple Targeted Builds
Because targeted builds sharing common earlier stages can reuse cached layers from those shared stages, running multiple targets in sequence within the same pipeline run doesn't necessarily mean redundantly repeating shared setup work.
docker build --target test -t myapp:test .
docker build --target build -t myapp:latest .
The second build can reuse cached layers from stages shared with the first, rather than rebuilding them from scratch.
Why CI Stage Flexibility Matters
The ability to independently target different stages of the same Dockerfile gives a CI pipeline fine-grained, clearly reported visibility into each distinct aspect of the build process, all while maintaining a single, coherent source of truth for how the application is actually built.