为什么 Docker 容器镜像如此之大? [英] Why are Docker container images so large?

查看:96
本文介绍了为什么 Docker 容器镜像如此之大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 Fedora 的 Dockerfile 制作了一个简单的镜像(最初 320 MB).

I made a simple image through Dockerfile from Fedora (initially 320 MB).

添加了 Nano(这个 1MB 大小的微型编辑器),图像大小已经上升到 530 MB.我在此之上添加了 Git(30-ish MB),然后我的图像大小猛增至 830 MB.

Added Nano (this tiny editor of 1MB size), and the size of the image has risen to 530 MB. I've added Git on top of that (30-ish MB), and then my image size sky-rockets to 830 MB.

这不是疯了吗?

我尝试导出和导入容器以删除历史/中间图像.这项工作最多节省了 25 MB,现在我的图像大小为 804 MB.我还尝试在一个 RUN 上运行许多命令,但仍然得到相同的初始 830MB.

I've tried to export and import container to remove history/intermediate images. This effort saved up to 25 MB, now my image size is 804 MB. I've also tried to run many commands on one RUN, but still I'm getting the same initial 830MB.

我怀疑是否值得使用 Docker.我的意思是,我几乎没有安装任何东西,而且超过了 1GB.如果我必须添加一些重要的东西,比如数据库等等,我可能会用完磁盘空间.

I'm having my doubts if it is worth to use Docker at all. I mean, I barely installed anything and I'm hitting 1GB over. If I will have to add some serious stuff like a database and so on I might run out of disk space.

任何人都因图像尺寸过大而苦恼吗?你是怎么处理的?

Anyone suffers from ridiculous size of images? How do you deal with it?

除非我的 Dockerfile 非常不正确?

Unless my Dockerfile is horribly incorrect?

FROM fedora:latest
MAINTAINER Me NotYou <email@dot.com>
RUN yum -y install nano
RUN yum -y install git

但是很难想象这里会出现什么问题.

but it's hard to imagine what could go wrong in here.

推荐答案

正如@rexposadas 所说,图像包括所有层,每个层都包括您安装的所有依赖项.同样重要的是要注意基本映像(如 fedora:latest 往往非常简单.您可能会对已安装软件的依赖项数量感到惊讶.

As @rexposadas said, images include all the layers and each layer includes all the dependencies for what you installed. It is also important to note that the base images (like fedora:latest tend to be very bare-bones. You may be surprised by the number of dependencies your installed software has.

通过将 yum -y clean all 添加到每一行,我能够使您的安装显着缩小:

I was able to make your installation significantly smaller by adding yum -y clean all to each line:

FROM fedora:latest
RUN yum -y install nano && yum -y clean all
RUN yum -y install git && yum -y clean all

在层被提交之前,为每个 RUN 执行此操作很重要,否则删除实际上不会删除数据.也就是说,在联合/写时复制文件系统中,最后的清理并没有真正减少文件系统的使用,因为真实数据已经提交到较低层.要解决这个问题,您必须在每一层都进行清洁.

It is important to do that for each RUN, before the layer gets committed, or else deletes don't actually remove data. That is, in a union/copy-on-write file system, cleaning at the end doesn't really reduce file system usage because the real data is already committed to lower layers. To get around this you must clean at each layer.

$ docker history bf5260c6651d
IMAGE               CREATED             CREATED BY                                      SIZE
bf5260c6651d        4 days ago          /bin/sh -c yum -y install git; yum -y clean a   260.7 MB
172743bd5d60        4 days ago          /bin/sh -c yum -y install nano; yum -y clean    12.39 MB
3f2fed40e4b0        2 weeks ago         /bin/sh -c #(nop) ADD file:cee1a4fcfcd00d18da   372.7 MB
fd241224e9cf        2 weeks ago         /bin/sh -c #(nop) MAINTAINER Lokesh Mandvekar   0 B
511136ea3c5a        12 months ago                                                       0 B

这篇关于为什么 Docker 容器镜像如此之大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆