Why are Docker container images so large?

前端 未结 8 1915
被撕碎了的回忆
被撕碎了的回忆 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 17:50

    Here are some more things you can do:

    • Avoid multiple RUN commands where you can. Put as much as possbile into one RUN command (using &&)
    • clean-up unnecessary tools like wget or git (which you only need for download or building stuff, but not to run your process)

    With these both AND the recommendations from @Andy and @michau I was able to resize my nodejs image from 1.062 GB to 542 MB.

    Edit: One more important thing: "It took me a while to really understand that each Dockerfile command creates a new container with the deltas. [...] It doesn't matter if you rm -rf the files in a later command; they continue exist in some intermediate layer container." So now I managed to put apt-get install, wget, npm install (with git dependencies) and apt-get remove into a single RUN command, so now my image has only 438 MB.

    Edit 29/06/17

    With Docker v17.06 there comes a new features for Dockerfiles: You can have multiple FROM statements inside one Dockerfile and only the stuff from last FROM will be in your final Docker image. This is useful to reduce image size, for example:

    FROM nodejs as builder
    WORKDIR /var/my-project
    RUN apt-get install ruby python git openssh gcc && \
        git clone my-project . && \
        npm install
    
    FROM nodejs
    COPY --from=builder /var/my-project /var/my-project
    

    Will result in an image having only the nodejs base image plus the content from /var/my-project from the first steps - but without the ruby, python, git, openssh and gcc!

提交回复
热议问题