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
=> 在 /var/lib/ 中添加 python3
--> 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 会重用未变化的层。
重新运行构建:
=> [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 入门