How to Use Docker Layer Caching in CI for Faster Builds
To use
docker layer caching in CI, configure your CI pipeline to reuse the Docker build cache by saving and restoring the cache between runs. This often involves using cache volumes or registry-based cache with docker build --cache-from to speed up builds by reusing unchanged layers.Syntax
The key Docker command to use layer caching in CI is docker build --cache-from. This option tells Docker to use an existing image as a cache source for layers.
Parts explained:
docker build: Builds a Docker image from a Dockerfile.--cache-from <image>: Uses the specified image as a cache source.-t <tag>: Tags the new image..: The build context (current directory).
bash
docker build --cache-from myrepo/myimage:latest -t myrepo/myimage:latest .Example
This example shows a GitHub Actions workflow that caches Docker layers by pushing and pulling the image from a registry. It speeds up builds by reusing unchanged layers.
yaml
name: Docker Build with Cache on: [push] jobs: build: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Log in to Docker Hub uses: docker/login-action@v2 with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - name: Pull cache image run: | docker pull myrepo/myimage:latest || true - name: Build Docker image with cache run: | docker build --cache-from myrepo/myimage:latest -t myrepo/myimage:latest . - name: Push Docker image run: | docker push myrepo/myimage:latest
Output
Step outputs show Docker pulling existing image layers, building only changed layers, then pushing updated image.
Common Pitfalls
Common mistakes when using Docker layer caching in CI include:
- Not pulling the cache image before build, so cache is not used.
- Not pushing the updated image after build, so cache is not saved for next run.
- Changing files early in the Dockerfile invalidates many layers, reducing cache benefits.
- Using
latesttag without proper versioning can cause cache confusion.
Example of wrong and right usage:
bash
# Wrong: No cache pull docker build -t myrepo/myimage:latest . # Right: Pull cache first docker pull myrepo/myimage:latest || true docker build --cache-from myrepo/myimage:latest -t myrepo/myimage:latest .
Quick Reference
| Step | Command/Action | Purpose |
|---|---|---|
| 1 | docker pull myrepo/myimage:latest || true | Get existing image cache if available |
| 2 | docker build --cache-from myrepo/myimage:latest -t myrepo/myimage:latest . | Build image using cache layers |
| 3 | docker push myrepo/myimage:latest | Save updated image for next cache use |
Key Takeaways
Always pull the cache image before building to reuse layers.
Use --cache-from option with docker build to enable layer caching.
Push the updated image after build to save cache for future runs.
Order Dockerfile commands to maximize cache efficiency by minimizing early changes.
Use consistent image tags to avoid cache confusion in CI.