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

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

问题描述

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



添加了Nano(1MB大小的这个小编辑器)和图像的大小已升至530 MB。我已经添加了Git(30-ish MB),然后我的图像大小天空火箭到830 MB。



这不是疯了吗?



我试图导出和导入容器来删除历史/中间图片。这个工作节省了25 MB,现在我的图像大小是804 MB。我也尝试在一个 RUN 上运行许多命令,但是我仍然拥有相同的初始830MB。



我有疑问,如果值得使用Docker。我的意思是,我几乎没有安装任何东西,我打了1GB。如果我必须添加一些严重的东西,如数据库等,我可能会用尽磁盘空间。



任何人都有可笑的大小的图像?如何处理它?<​​/ p>

除非我的Dockerfile是非常不正确的?

  FROM fedora:latest 
MAINTAINER Me NotYou< email@dot.com>
运行yum -y安装nano
运行yum -y安装git

解决方案

正如@rexposadas所说,图像包括所有图层,每层包括你所安装的所有依赖项。同样重要的是要注意,基本图像(如$ code> fedora:最新的往往是非常裸的,您可能会惊讶于您安装的软件所依赖的数量。



通过向每一行添加 yum -y clean all 可以使您的安装显着更小: p>

  FROM fedora:latest 
RUN yum -y install nano; yum -y clean all
运行yum -y安装git; yum -y clean all

在层之前,对每个RUN执行此操作很重要得到承诺,否则删除实际上不会删除数据,也就是说,在联合/写时复制文件系统中,最终的清理并不能真正减少文件系统的使用,因为实际的数据已经被提交到较低层为了解决这个问题,你必须在每个层次上清理。

  $ docker history bf5260c6651d 
由SIZE创建的IMAGE CREATED b $ b bf5260c 6651d 4天前/ bin / sh -c yum -y install git; yum -y clean a 260.7 MB
172743bd5d60 4天前/ bin / sh -c yum -y install nano; yum -y clean 12.39 MB
3f2fed40e4b0 2周前/ bin / sh -c#(nop)ADD文件:cee1a4fcfcd00d18da 372.7 MB
fd241224e9cf 2周前/ bin / sh -c#(nop)MAINTAINER Lokesh Mandvekar 0 B
511136ea3c5a 12个月前0 B


I made a simple image through Dockerfile from Fedora (initially 320 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.

Isn't that insane?

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.

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?

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.

解决方案

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.

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

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天全站免登陆