บทนำสู่การแคชใน Docker

Docker เบื้องต้น

Tim Sangster

Software Engineer @ DataCamp

Docker build

การดาวน์โหลดและแตกไฟล์โดยใช้คำสั่ง Docker

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

จะเปลี่ยนแปลงระบบไฟล์และเพิ่ม:

/example_folder.zip
/example_folder/
    example_file1
    example_file2

การเปลี่ยนแปลงเหล่านี้คือสิ่งที่ถูกจัดเก็บไว้ใน image

Docker เบื้องต้น

คำสั่ง Docker เชื่อมโยงกับการเปลี่ยนแปลงในระบบไฟล์

แต่ละคำสั่งใน Dockerfile เชื่อมโยงกับการเปลี่ยนแปลงที่เกิดขึ้นในระบบไฟล์ของ image

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 layers

  • Docker layer: การเปลี่ยนแปลงทั้งหมดที่เกิดจากคำสั่ง Dockerfile หนึ่งคำสั่ง
  • Docker image: ทุก layer ที่สร้างขึ้นระหว่างการ build

--> Docker image: การเปลี่ยนแปลงทั้งหมดในระบบไฟล์จากทุกคำสั่ง Dockerfile

ระหว่างการ build Dockerfile Docker จะแสดง layer ที่กำลังดำเนินการ:

 => [1/3] FROM docker.io/library/ubuntu 
 => [2/3] RUN apt-get update
 => [3/3] RUN apt-get install -y python3
Docker เบื้องต้น

Docker caching

การ build ครั้งต่อๆ ไปจะเร็วขึ้นมาก เพราะ Docker นำ layer ที่ไม่มีการเปลี่ยนแปลงกลับมาใช้ใหม่

การ build ซ้ำ:

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

การ build ซ้ำเมื่อมีการเปลี่ยนแปลง:

 => [1/3] FROM docker.io/library/ubuntu
 => CACHED [2/3] RUN apt-get update
 => [3/3] RUN apt-get install -y R
Docker เบื้องต้น

ทำความเข้าใจ Docker caching

การเข้าใจว่า layer ไหนถูกแคชช่วยให้เราทราบว่าทำไม image บางครั้งจึงไม่เปลี่ยนแปลงหลังจาก rebuild

  • Docker ไม่สามารถรู้ได้ว่า python3 มีเวอร์ชันใหม่ออกมาเมื่อไร
  • Docker จะใช้ layer ที่แคชไว้เพราะคำสั่งเหมือนกับการ build ก่อนหน้า
 => [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 caching

ช่วยให้เขียน Dockerfile ที่ build ได้เร็วขึ้น เพราะไม่จำเป็นต้อง rebuild ทุก layer

ใน Dockerfile ต่อไปนี้ ทุกคำสั่งต้อง rebuild ใหม่หากไฟล์ 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 caching

ช่วยให้เขียน Dockerfile ที่ build ได้เร็วขึ้น เพราะไม่จำเป็นต้อง rebuild ทุก layer

ใน 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...