Software Development

Why is my CI/CD pipeline so slow and how can I optimize the Docker build stage?

DE Asked by Deborah Lewis · 22-08-2023
0 upvotes 17,596 views 0 comments
The question

Our Jenkins pipeline currently takes over 20 minutes to complete, and most of that time is spent on the "Docker Build" stage. Every time we change a single line of code, it seems like the entire image is being rebuilt from scratch instead of using the cache. This is killing our developer productivity and slowing down our release cycle. Are there specific ways to structure a Dockerfile or configure the CI runner to make the build process more incremental and efficient?

 

3 answers

0
PA
Answered on 24-08-2023

To optimize Docker builds, you must leverage the "Layer Cache" effectively. Order your Dockerfile commands from least frequent to most frequent changes. For example, copy your package.json and run npm install BEFORE copying your actual source code. This way, if only the code changes, Docker reuses the cached "node_modules" layer. Also, ensure your Jenkins agent is persisting the Docker cache between runs. If you are using ephemeral agents (like Kubernetes pods), you might need to use docker build --cache-from to pull the previous image and use its layers as a cache source. 

0
C
Answered on 26-08-2023

Are you using "Multi-stage builds" to keep your final production images small, or are you shipping all the build tools and headers in the final image too? 

RI 27-08-2023

Charles, we just switched to multi-stage builds and it's great. We use a heavy "Maven" image to compile the Java JAR file, and then copy just that JAR into a tiny "Alpine Linux" image. Not only did our build speed improve because the final push to the registry is smaller, but our security posture is better because there are fewer vulnerabilities in a stripped-down production image. Smaller images mean faster pulls during the deployment phase too!

0
SU
Answered on 29-08-2023

You should also check your .dockerignore file. If you are sending the node_modules or .git folder to the Docker daemon, it slows down the build context transfer significantly. 

DE 30-08-2023

Good point, Susan! I added a .dockerignore yesterday and it shaved 3 minutes off our build time just by not uploading unnecessary local files to the daemon.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

World globe icon Country: Canada

Book Free Session