Docker เบื้องต้น
Tim Sangster
Software Engineer @ DataCamp
การดาวน์โหลดและแตกไฟล์โดยใช้คำสั่ง Docker
RUN curl http://example.com/example_folder.zip
RUN unzip example_folder.zip
จะเปลี่ยนแปลงระบบไฟล์และเพิ่ม:
/example_folder.zip
/example_folder/
example_file1
example_file2
การเปลี่ยนแปลงเหล่านี้คือสิ่งที่ถูกจัดเก็บไว้ใน image
แต่ละคำสั่งใน 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 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
การ 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
การเข้าใจว่า layer ไหนถูกแคชช่วยให้เราทราบว่าทำไม image บางครั้งจึงไม่เปลี่ยนแปลงหลังจาก rebuild
=> [1/3] FROM docker.io/library/ubuntu
=> CACHED [2/3] RUN apt-get update
=> CACHED [3/3] RUN apt-get install -y python3
ช่วยให้เขียน 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
ช่วยให้เขียน 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 เบื้องต้น