Docker 캐싱 소개

Docker 입문

Tim Sangster

Software Engineer @ DataCamp

Docker 빌드

Docker 명령으로 파일을 다운로드하고 압축 해제하기.

RUN curl http://example.com/example_folder.zip
RUN unzip example_folder.zip

파일 시스템이 변경되며 다음이 추가됩니다:

/example_folder.zip
/example_folder/
    example_file1
    example_file2

이 변경 내용이 이미지에 저장됩니다.

Docker 입문

Docker 명령과 파일 시스템 변경의 연결

Dockerfile의 각 명령은 이미지 파일 시스템에 만든 변경 사항과 연결됩니다.

FROM docker.io/library/ubuntu
 => Ubuntu 실행에 필요한 파일이 있는 시작 파일 시스템을 제공합니다
COPY /pipeline/ /pipeline/
 => /pipeline/ 폴더 생성
 => /pipeline/ 폴더에 여러 파일 복사
RUN apt-get install -y python3
 => /var/lib/ 에 python3 추가
Docker 입문

Docker 레이어

  • Docker 레이어: 단일 Dockerfile 명령으로 인한 모든 변경.
  • Docker 이미지: 빌드 중 생성된 모든 레이어

--> Docker 이미지: 모든 Dockerfile 명령으로 인한 파일 시스템의 전체 변경.

Docker 빌드 중 현재 처리 중인 레이어를 표시합니다:

 => [1/3] FROM docker.io/library/ubuntu 
 => [2/3] RUN apt-get update
 => [3/3] RUN apt-get install -y python3
Docker 입문

Docker 캐싱

연속 빌드는 훨씬 빠릅니다. 변경되지 않은 레이어는 재사용됩니다.

빌드 재실행:

 => [1/3] FROM docker.io/library/ubuntu
 => CACHED [2/3] RUN apt-get update
 => CACHED [3/3] RUN apt-get install -y python3

변경 후 빌드 재실행:

 => [1/3] FROM docker.io/library/ubuntu
 => CACHED [2/3] RUN apt-get update
 => [3/3] RUN apt-get install -y R
Docker 입문

Docker 캐싱 이해

레이어가 캐시되는 시점을 알면, 재빌드했는데 이미지가 바뀌지 않는 이유를 이해할 수 있습니다.

  • Docker는 python3의 새 버전 출시를 알 수 없습니다.
  • 명령이 이전 빌드와 동일하면 캐시된 레이어를 사용합니다.
 => [1/3] FROM docker.io/library/ubuntu
 => CACHED [2/3] RUN apt-get update
 => CACHED [3/3] RUN apt-get install -y python3
Docker 입문

Docker 캐싱 이해

모든 레이어를 다시 빌드하지 않아도 되어 Dockerfile 빌드가 빨라집니다.

다음 Dockerfile에서는 pipeline.py가 바뀌면 모든 명령을 다시 빌드해야 합니다:

FROM ubuntu
COPY /app/pipeline.py /app/pipeline.py
RUN apt-get update
RUN apt-get install -y python3
 => [1/4] FROM docker.io/library/ubuntu
 => [2/4] COPY /app/pipeline.py /app/pipeline.py
 => [3/4] RUN apt-get update
 => [4/4] RUN apt-get install -y python3
Docker 입문

Docker 캐싱 이해

모든 레이어를 다시 빌드하지 않아도 되어 Dockerfile 빌드가 빨라집니다.

다음 Dockerfile에서는 COPY만 다시 실행됩니다.

FROM ubuntu
RUN apt-get update
RUN apt-get install -y python3
COPY /app/pipeline.py /app/pipeline.py
 => [1/4] FROM docker.io/library/ubuntu
 => CACHED [2/4] RUN apt-get update
 => CACHED [3/4] RUN apt-get install -y python3
 => [4/4] COPY /app/pipeline.py /app/pipeline.py
Docker 입문

Ayo berlatih!

Docker 입문

Preparing Video For Download...