TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Tips to Reduce Docker Image Sizes

1 点作者 idiotb将近 5 年前
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 条评论

mtmail将近 5 年前
<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>
qohen将近 5 年前
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>