HOW-TO · DEV
How to use AI to optimize Docker build times by suggesting layer caching improvements in your CI pipeline
Target environment
Ubuntu 24.04 · Ollama 0.4.x
PREREQUISITES
A Dockerfile in the repository, a CI pipeline that builds Docker images (GitHub Actions, Jenkins, or GitLab CI), and an AI assistant accessible via CLI or API.
What this does
Docker layer caching is one of the most effective ways to reduce CI build times. This guide explains how to feed the current Dockerfile and CI build logs to an AI assistant, receive specific recommendations for reordering instructions and splitting layers, and then apply those changes to achieve measurably faster builds.
Steps
- Capture the current Dockerfile content and recent CI build log showing the build time breakdown.
- Provide both artifacts to the AI assistant with a prompt asking for layer-by-layer analysis and optimization suggestions.
- Review the AI's recommendations: identify layers that change infrequently and should be moved earlier, and layers that change on every build and should be moved later.
- Apply the recommended Dockerfile changes, typically by splitting the
COPYcommands so that dependency files are copied before application source files. - Add multi-stage build patterns if the AI suggests them: use a lean
runtimebase image that only includes the final artifact. - Configure the CI pipeline to use Docker layer caching (e.g.,
docker/build-push-actionwithcache-fromandcache-tooptions in GitHub Actions). - Trigger a build and compare the new build time against the baseline from step 1.
- If build time does not improve, feed the new build log back to the AI and ask for additional recommendations.
Verification
gh run view --log | grep "Building Docker image" -A 5
Expected output:
Building Docker image myapp:latest
Cached 7 of 12 layers (58% cache hit)
Build completed in 2m 14s
Compare the build time with the previous run's log. A measurable reduction (typically 20–40%) indicates the caching strategy is effective.
Common failures
- Docker build ignores the cache entirely — The CI runner may not support layer caching. Verify the CI step includes the
--cache-fromflag and that the cache image tag is correctly pushed and pulled between runs. - Cache hit ratio remains low after changes — The instruction order may still be causing too many invalidations. Use
docker historyto inspect which layers are being recreated and ask the AI to reorder based on the actual change frequency of each file. - Multi-stage build fails because final image is missing files — The
COPY --from=builderinstruction may reference the wrong stage name or path. Verify all required artifacts are copied in the builder stage before theFROMline is changed. - CI job fails with out-of-space error on cache push — The cache image size exceeds CI runner disk limits. Reduce the number of cached layers by combining related instructions into single
RUNcommands.
Related guides
- Configure CI/CD with GitHub Actions for AI tests (guide 27)
- Integrate AI test generation into Jenkins pipelines (guide 28)