DEV Community

Cover image for How to Reduce Docker Image Size: Best Practices and Tips for DevOps Engineers

How to Reduce Docker Image Size: Best Practices and Tips for DevOps Engineers

H A R S H H A A on September 29, 2024

Table of Contents Why Reducing Docker Image Size is Important Start with a Minimal Base Image Multistage Builds Avoid Installing...
Collapse
 
little_twinkle_0ae2b15172 profile image
little twinkle

Good optimisation content.Thanks for sharing. Clear content with immense knowledge.

Is above technique can be used in production deployment as well? Prune command not recommended to use in higher level environment.

Collapse
 
notharshhaa profile image
H A R S H H A A • Edited

Thank you so much! 😊 @little_twinkle_0ae2b15172
I'm glad you found the optimization techniques helpful! The strategies outlined can absolutely be used in production environments. However, you're right—using the prune command should be done with caution in higher-level environments.

In production, it's best to carefully manage image and layer cleanup to avoid unintended deletions. Instead of using docker system prune broadly, you might want to implement specific cleanup practices, such as removing unused images and layers only when certain they are no longer needed.

Thanks again for your thoughtful feedback! 🙌

Collapse
 
david_j_eddy profile image
David J Eddy

Replace Docker with containers or padman or any other cli tool please. Docker Inc is constantly becoming more and more of a hostile organization to the OSS community and the masses of engineers who supported it in the early days.

All thw above still holds true for any containerized workload. Just don't use the Docker cli/desktop.

Collapse
 
notharshhaa profile image
H A R S H H A A

Thank you for your input! 🙏 @david_j_eddy
You make a valid point about Docker's relationship with the OSS community, and I agree that the best practices mentioned in the article apply to any containerized workload, whether you're using Docker, Podman, or another CLI tool.

I'll definitely consider adding a section on alternatives like Podman and how the same strategies can be applied across different containerization platforms. My goal is to help DevOps engineers optimize their containers regardless of the specific tool they choose.

Thanks again for your valuable feedback! 😊

Collapse
 
paulsanjay81 profile image
Sanjay Paul

Great post! Reducing Docker image size is such an essential topic for DevOps teams, and you've laid out some fantastic strategies here. I particularly love the emphasis on multistage builds and using minimal base images—those alone can lead to significant improvements in efficiency and security.

The reminder to regularly audit images is also crucial; it’s easy to let things bloat over time. Tools like Trivy for scanning are game changers in maintaining lean images while keeping an eye on vulnerabilities.

Thanks for sharing these best practices! I’ll definitely be implementing some of these tips in my next project. Keep up the awesome work!

Collapse
 
notharshhaa profile image
H A R S H H A A • Edited

Thank you so much for the kind words! 😊 @paulsanjay81
I'm really glad the post resonated with you and that you found the strategies useful. Multistage builds and minimal base images can indeed make a huge difference in optimizing Docker images, both in terms of efficiency and security. Regular audits with tools like Trivy are definitely game-changers, and it's great to hear that you'll be implementing some of these tips in your next project!

Thanks again for your feedback, and I’ll keep sharing more DevOps insights. Best of luck with your projects! 🙌

Collapse
 
kcq profile image
Kyle Quest • Edited

There's also DockerSlim (aka MinToolkit , aka SlimToolkit), which automatically shrinks container images for you. The most basic way to use is "mint slim nginx:latest" where "nginx:latest" is the container image you want to shrink.

Lots of examples here: github.com/mintoolkit/examples Take a look at the 3rdparty directory there... It has a lot more examples including FastAPI examples and Spring and Micronaut examples.

Collapse
 
notharshhaa profile image
H A R S H H A A

Thank you for sharing this! 🙌 @kcq
DockerSlim (MinToolkit/SlimToolkit) is indeed an excellent tool for automatically shrinking container images, and I appreciate you mentioning it! It's a great addition for those looking to automate and further optimize their images without manual intervention. I'll definitely take a look at the examples in the repository you shared—it's especially useful to see examples for FastAPI, Spring, and Micronaut.

I'll consider adding a section in the article to highlight DockerSlim as another powerful optimization tool. Thanks again for contributing such valuable information! 🚀

Collapse
 
notharshhaa profile image
H A R S H H A A • Edited

Image description

🐳 𝗥𝗲𝗱𝘂𝗰𝗶𝗻𝗴 𝗗𝗼𝗰𝗸𝗲𝗿 𝗜𝗺𝗮𝗴𝗲 𝗦𝗶𝘇𝗲 – 𝗙𝗿𝗼𝗺 𝟭.𝟱𝗚𝗕 𝘁𝗼 𝗝𝘂𝘀𝘁 𝟱𝟱𝗠𝗕 🐳

Building efficient and lightweight Docker images is key to faster deployments and better resource management. In my latest project, I successfully reduced a Docker image from a massive 1.5GB to just 55MB! This optimization can drastically improve your CI/CD pipelines, reduce storage costs, and speed up deployment times.

Collapse
 
mrzaizai2k profile image
Mai Chi Bao

Wow My Docker for AI project is usually so big.

Collapse
 
notharshhaa profile image
H A R S H H A A

Thanks for your comment! 😊 @mrzaizai2k
AI projects, especially with all the libraries and dependencies involved, can indeed lead to some pretty large Docker images! The tips in this article, like using minimal base images, multistage builds, and cleaning up unused dependencies, can help reduce those image sizes. It’s definitely worth a try for AI projects as well!

Feel free to reach out if you need any more specific advice on optimizing your Docker setup for AI workloads. Glad you found the article useful! 🙌

Collapse
 
frankzonarix profile image
Frank Müller

Great article. Saved me some time!

Collapse
 
notharshhaa profile image
H A R S H H A A

Thanks mate @frankzonarix

Collapse
 
anil_mohite_2ac976aaab00f profile image
Anil Mohite

👍 Thanks for sharing

Collapse
 
notharshhaa profile image
H A R S H H A A
Collapse
 
raulpenate profile image
The Eagle 🦅

Thanks for the info dawg!

Collapse
 
notharshhaa profile image
H A R S H H A A

Thanks 👍 mate @raulpenate

Collapse
 
usman_awan profile image
USMAN AWAN

Nice 👍

Collapse
 
notharshhaa profile image
H A R S H H A A

Thanks 👍 @usman_awan