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
 => Gives us a file system to start from with all files needed to run Ubuntu
COPY /pipeline/ /pipeline/
 => Creates the /pipeline/ folder
 => Copies multiple files in the /pipeline/ folder
RUN apt-get install -y python3
 => Add python3 to /var/lib/
Docker の紹介

Docker レイヤー

  • Docker レイヤー: 単一のDockerfile命令によって引き起こされるすべての変更。
  • Dockerイメージ: ビルドの実行中に作成されたすべてのレイヤー

Docker イメージ: すべての Dockerfile 命令によるファイルシステムへのすべての変更。

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のキャッシュ

連続ビルドは、変更されていないレイヤーを 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 の新しいバージョンがリリースされたことを検知できません。
  • Docker は、命令が以前のビルドと同一であるため、キャッシュされたレイヤーを使用します。
 => [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 のビルドを高速化するのに役立ちます。

pipeline.py ファイルが変更された場合、次の Dockerfile のすべての命令を再構築する必要があります:

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 の紹介

練習しましょう!

Docker の紹介

Preparing Video For Download...