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
這些變更會被儲存在映像中。
Dockerfile 中的每個指令都會連結到它在映像檔案系統所做的變更。
FROM docker.io/library/ubuntu
=> 提供可用的起始檔案系統,含執行 Ubuntu 所需檔案
COPY /pipeline/ /pipeline/
=> 建立 /pipeline/ 資料夾
=> 複製多個檔案到 /pipeline/ 資料夾
RUN apt-get install -y python3
=> 將 python3 加到 /var/lib/
--> Docker image:所有 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 會重用未變更的層。
再次執行建置:
=> [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
了解層何時被快取,有助於理解為何有時重建後映像不會改變。
=> [1/3] FROM docker.io/library/ubuntu
=> CACHED [2/3] RUN apt-get update
=> CACHED [3/3] RUN apt-get install -y python3
幫助我們撰寫建置更快的 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
幫助我們撰寫建置更快的 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 入門