Logo
Blog
Speeding Up Azure DevOps CI/CD with Docker Layer Caching

Speeding Up Azure DevOps CI/CD with Docker Layer Caching

Avatar
Hitarth Thaker
September 18, 2025

One of the biggest challenges in CI/CD pipelines is long build times.
In our case, our Azure DevOps pipeline was rebuilding Docker images from scratch on every run.

This meant:

  • Slow builds 🚢
  • Delayed deployments ⏳
  • Frustrated developers 😀

❌ The Problem

By default, when Docker builds run inside CI/CD pipelines, they don’t persist cache layers across builds.
This leads to repeated downloads and rebuilds even if nothing has changed in those layers.

For example:

dockerfile
FROM node:18
WORKDIR /app
# Dependencies
COPY package.json package-lock.json ./
RUN npm install
# Copy source
COPY . .
CMD ["npm", "start"]
  • Even if only one file changes, all layers may rebuild.
  • This adds unnecessary minutes to every pipeline run.

βœ… The Solution: Docker Layer Caching

We implemented Docker layer caching in Azure DevOps. This ensures unchanged layers are reused, making builds much faster.

Example Pipeline (YAML)

Here’s an example of how to implement Docker layer caching using the Cache@2 task in an Azure DevOps pipeline.

yaml
pool:
vmImage: 'ubuntu-latest'
steps:
- task: DockerInstaller@0
displayName: 'Install Docker'
- task: Cache@2
displayName: 'Cache Docker layers'
inputs:
key: '"docker" | "$(Agent.OS)" | package-lock.json'
path: $(Pipeline.Workspace)/.docker
restoreKeys: |
"docker" | "$(Agent.OS)"
- script: |
docker build \
--cache-from=$(Pipeline.Workspace)/.docker/cache \
--tag myapp:$(Build.BuildId) .
docker save myapp:$(Build.BuildId) -o $(Pipeline.Workspace)/.docker/cache
displayName: 'Build Docker image with cache'

πŸš€ Results

After implementing layer caching:

  • Build times reduced by 60–70% ⏱️
  • Faster feedback in CI/CD πŸ”„
  • Developers could ship updates quicker ⚑

πŸ”‘ Best Practices

  1. Order your Dockerfile properly
    • Place frequently changing layers (e.g., source code) at the bottom.
    • Keep stable layers (like npm install or apt-get) at the top.
  2. Use .dockerignore
    • Exclude unnecessary files (like .git/, node_modules/) to avoid cache invalidation.
  3. Cache dependencies smartly
    • If your dependencies don’t change often, keep them as a separate cached layer.

Conclusion

By adding Docker layer caching, we turned our slow Azure DevOps builds into fast and efficient pipelines. This small tweak made a huge difference in developer productivity and deployment speed.

Contact Us

Thank you for reading our comprehensive guide on "Speeding Up Azure DevOps CI/CD with Docker Layer Caching" We hope you found it insightful and valuable. If you have any questions, need further assistance, or are looking for expert support in developing and managing your projects. our team is here to help!

Reach out to us for Your Project Needs:

🌐 Website: https://www.prometheanz.com

πŸ“§ Email: [email protected]

Copyright Β© 2025 PrometheanTech. All Rights Reserved.