IT

Docker 컨테이너 이미지가 왜 그렇게 큰가요?

lottoking 2020. 6. 2. 21:14
반응형

Docker 컨테이너 이미지가 왜 그렇게 큰가요?


Fedora의 Dockerfile (초기 320MB)을 통해 간단한 이미지를 만들었습니다.

Nano (1MB 크기의 작은 편집기)가 추가되었으며 이미지 크기가 530MB로 증가했습니다. 그 위에 Git을 추가하고 (30-MB) 이미지 크기가 830MB로 급상승합니다.

미쳤어?

기록 / 중간 이미지를 제거하기 위해 컨테이너를 내보내고 가져 오려고했습니다. 이 노력은 최대 25MB를 절약했으며 이제 이미지 크기는 804MB입니다. 또한 하나 RUN에서 많은 명령을 실행하려고 했지만 여전히 동일한 초기 830MB를 얻습니다.

Docker를 전혀 사용할 가치가 있는지 의심 스럽습니다. 나는 거의 아무것도 설치하지 않았고 1GB를 초과했습니다. 데이터베이스와 같은 심각한 것들을 추가해야 할 경우 디스크 공간이 부족할 수 있습니다.

어리석은 크기의 이미지로 고통받는 사람이 있습니까? 어떻게 처리합니까?

내 Dockerfile이 끔찍하게 잘못되지 않는 한?

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

그러나 여기서 무엇이 잘못 될 수 있는지 상상하기는 어렵습니다.


@rexposadas가 말했듯이 이미지에는 모든 레이어가 포함되고 각 레이어에는 설치 한 항목에 대한 모든 종속성이 포함됩니다. 또한 기본 이미지 (예 : fedora:latest매우 베어 본인 경향이 있음)에 유의해야합니다 . 설치된 소프트웨어의 종속성 수에 놀라실 수 있습니다.

yum -y clean all각 줄 에 추가하여 설치를 크게 줄일 수있었습니다 .

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

계층이 커밋되기 전에 각 RUN에 대해 수행해야합니다. 그렇지 않으면 삭제시 실제로 데이터가 제거되지 않습니다. 즉, Union / Copy-On-Write 파일 시스템에서 실제 데이터가 이미 하위 계층에 커밋되어 있기 때문에 결국 정리해도 파일 시스템 사용이 줄어드는 것은 아닙니다. 이 문제를 해결하려면 각 층을 청소해야합니다.

$ 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

도커 이미지는 크지 않고 단지 큰 이미지를 작성하고 있습니다.

scratch이미지가 0B가 정적 바이너리로 코드를 컴파일 할 수 있다면 당신은 당신의 코드를 패키지로 그것을 사용할 수 있습니다 ANDD입니다. 예를 들어 Go 프로그램을 컴파일 하고 맨 위에 패키지하여scratch 5MB 미만의 완전히 사용 가능한 이미지를 만들 수 있습니다.

열쇠는 공식 Docker 이미지를 사용하지 않는 것입니다. 이미지가 너무 큽니다. 스크래치는 그다지 실용적이지 않으므로 Alpine Linux를 기본 이미지로 사용하는 것이 좋습니다. ~ 5MB이며 앱에 필요한 것만 추가하십시오. 마이크로 컨테이너 에 대한이 게시물은 알파인에 기반을 둔 매우 작은 이미지를 만드는 방법을 보여줍니다.

업데이트 : 공식 Docker 이미지는 알파인을 기반으로하므로 지금 사용하는 것이 좋습니다.


할 수있는 일이 더 있습니다 :

  • 가능한 여러 RUN명령을 피하십시오 . 하나의 RUN명령 에 가능한 한 많이 넣습니다 (을 사용하여 &&)
  • wget 또는 git과 같은 불필요한 도구 정리 (다운로드 또는 빌드에만 필요하지만 프로세스는 실행하지 않아야 함)

@Andy 및 @michau의 AND와 권장 사항을 모두 사용하여 nodejs 이미지의 크기를 1.062GB에서 542MB로 조정할 수있었습니다.

편집 : 한 가지 더 중요한 점 : "각 Dockerfile 명령이 델타를 사용하여 새 컨테이너를 작성한다는 사실을 이해하는 데 시간이 조금 걸렸습니다. [...] 이후 명령에서 파일을 rm rf로 작성해도 문제가되지 않습니다. "일부 중간 레이어 컨테이너에 계속 존재합니다." 그래서 지금은 넣어 관리 apt-get install, wget, npm install(자식 종속성)와 apt-get remove단일로 RUN이제 내 이미지는 438메가바이트을 가지고, 명령.

29/06/17 수정

Docker v17.06에는 Dockerfile에 대한 새로운 기능이 있습니다. FROM하나의 Dockerfile에 여러 명령문을 사용할 수 있으며 마지막 내용 만 FROM최종 Docker 이미지에 있습니다. 예를 들어 이미지 크기를 줄이는 데 유용합니다.

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

이미지가 가진 발생합니다 것은 단지는 기본 이미지와 첫 단계에서의 / var / 내 프로젝트에서 콘텐츠를 nodejs -하지만 하지 않고 루비, 파이썬, 자식, OpenSSH의 및 GCC!


그렇습니다. 그 크기는 말도 안되며, 왜 그렇게 적은 사람들이 그 사실을 알지 못합니다.

다른 "최소"이미지와 달리 실제로는 최소 인 Ubuntu 이미지를 만들었습니다. 호출 textlab/ubuntu-essential되며 60MB가 있습니다.

FROM textlab/ubuntu-essential
RUN apt-get update && apt-get -y install nano

위 이미지는 nano를 설치 한 후 82MB입니다.

FROM textlab/ubuntu-essential
RUN apt-get update && apt-get -y install nano git

Git has many more prerequisites, so the image gets larger, about 192 MB. That's still less that the initial size of most images.

You can also take a look at the script I wrote to make the minimal Ubuntu image for Docker. You can perhaps adapt it to Fedora, but I'm not sure how much you will be able to uninstall.


The following helped me a lot:

After removing unused packages (e.g. redis 1200 mb freed) inside my container, I have done the following:

  1. docker export [containerID] -o containername.tar
  2. docker import -m "commit message here" containername.tar imagename:tag

The layers get flatten. The size of the new image will be smaller because I've removed packages from the container as stated above.

This took a lot of time to understand this and that's why I've added my comment.


For best practise, you should execute a single RUN command, because every RUN instruction in the Dockerfile writes a new layer in the image and every layer requires extra space on disk. In order to keep the number layers to a minimum, any file manipulation like install, moving, extracting, removing, etc, should ideally be made under a single RUN instruction

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

Docker Squash is a really nice solution to this. you can $packagemanager clean in the last step instead of in every line and then just run a docker squash to get rid of all of the layers.

https://github.com/jwilder/docker-squash


Yes the layer system is quite surprising. If you have a base image and you increment it by doing the following:

# Test
#
# VERSION       1

# use the centos base image provided by dotCloud
FROM centos7/wildfly
MAINTAINER JohnDo 

# Build it with: docker build -t "centos7/test" test/

# Change user into root
USER root

# Extract weblogic
RUN rm -rf /tmp/* \
    && rm -rf /wildfly/* 

The image has exactly the same size. That essentially means, you have to manage to put into your RUN steps a lot of extract, install and cleanup magic to make the images as small as the software installed.

This makes life much harder...

The dockerBuild is missing RUN steps without commit.

참고URL : https://stackoverflow.com/questions/24394243/why-are-docker-container-images-so-large

반응형