Why are Docker container images so large?

前端 未结 8 1928
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 17:32

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\

8条回答
  •  孤独总比滥情好
    2020-11-30 18:00

    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
    

提交回复
热议问题