TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: Tips to Reduce Docker Image Sizes

1 pointsby idiotbalmost 5 years ago
What are some of the most common and advanced tips to reduce docker image sizes? For Rails app, for python app. For docker images in general?

2 comments

mtmailalmost 5 years ago
<a href="https:&#x2F;&#x2F;hackernoon.com&#x2F;tips-to-reduce-docker-image-sizes-876095da3b34" rel="nofollow">https:&#x2F;&#x2F;hackernoon.com&#x2F;tips-to-reduce-docker-image-sizes-876...</a><p><a href="https:&#x2F;&#x2F;rollout.io&#x2F;blog&#x2F;reduce-docker-image-size&#x2F;" rel="nofollow">https:&#x2F;&#x2F;rollout.io&#x2F;blog&#x2F;reduce-docker-image-size&#x2F;</a><p><a href="https:&#x2F;&#x2F;phoenixnap.com&#x2F;kb&#x2F;docker-image-size" rel="nofollow">https:&#x2F;&#x2F;phoenixnap.com&#x2F;kb&#x2F;docker-image-size</a><p><a href="https:&#x2F;&#x2F;www.codeproject.com&#x2F;Articles&#x2F;1133826&#x2F;tips-to-reduce-Docker-image-size" rel="nofollow">https:&#x2F;&#x2F;www.codeproject.com&#x2F;Articles&#x2F;1133826&#x2F;tips-to-reduce-...</a>
qohenalmost 5 years ago
For docker images in general: the best way is to do multi-stage builds [0][1]. You will need to (re)write your Docker files to use this.<p>A quick-and-dirty way that doesn&#x27;t require rewriting is just to use the experimental --squash flag[1] (i.e. add: --experimental --squash) when running docker (you can also turn on --experimental permanently by putting { &quot;experimental&quot;: true } into docker&#x27;s &#x2F;etc&#x2F;docker&#x2F;daemon.json file. And, on a Mac, you can open the Docker app and there&#x27;s a checkbox to turn on experimental features and then you can just use --squash in the terminal).<p>Anecdotally, I had a 4.67GB docker file that --squash reduced to a little over 1GB. However, a rewrite of the docker file to do a multi-stage build took that 4.67GB file down to just under 0.5GB.<p>P.S. Pro-tip: if you want to speed up your docker builds, if you&#x27;re using 18.06 or higher, you can use use buildkit [2]. Again, anecdotally, on a 6-core Macbook from 2019 I think I got a 30% speedup or so -- your mileage may vary (a colleague with an 8-core machine I think got a 50% speedup -- it&#x27;s been a while, so these percentages may be a bit off, but you get the idea).<p>It was also an experimental feature early on, so you needed to have experimental features turned on (as described above with --squash). With later versions you don&#x27;t need to do that -- check your version&#x27;s documentation.<p>Anyway, you can either set an environment variable to use it i.e. export DOCKER_BUILDKIT=1 or you can set this permanently by adding: {&quot;features&quot;:{&quot;buildkit&quot;: true}} to &#x2F;etc&#x2F;docker&#x2F;daemon.json<p>[0] <a href="https:&#x2F;&#x2F;blog.logrocket.com&#x2F;reduce-docker-image-sizes-using-multi-stage-builds&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.logrocket.com&#x2F;reduce-docker-image-sizes-using-m...</a><p>[1] <a href="https:&#x2F;&#x2F;docs.docker.com&#x2F;develop&#x2F;develop-images&#x2F;multistage-build&#x2F;" rel="nofollow">https:&#x2F;&#x2F;docs.docker.com&#x2F;develop&#x2F;develop-images&#x2F;multistage-bu...</a><p>[2] <a href="https:&#x2F;&#x2F;www.giantswarm.io&#x2F;blog&#x2F;container-image-building-with-buildkit" rel="nofollow">https:&#x2F;&#x2F;www.giantswarm.io&#x2F;blog&#x2F;container-image-building-with...</a>