Docker 入門
Tim Sangster
Software Engineer @ DataCamp
COPY 指令會把本機的檔案複製到正在建立的映像中:
COPY <src-path-on-host> <dest-path-on-image>
COPY /projects/pipeline_v3/pipeline.py /app/pipeline.py
docker build -t pipeline:v3 .
...
[4/4] COPY ./projects/pipeline_v3/pipeline.py /app/pipeline.py
如果目的路徑沒有檔名,會沿用原本的檔名:
COPY /projects/pipeline_v3/pipeline.py /app/
在來源路徑未指定檔名時,會複製整個資料夾內容。
COPY <src-folder> <dest-folder>
COPY /projects/pipeline_v3/ /app/
COPY /projects/pipeline_v3/ /app/ 會把 pipeline_v3/ 底下全部複製:
/projects/
pipeline_v3/
pipeline.py
requirements.txt
tests/
test_pipeline.py
/init.py
/projects/
Dockerfile
pipeline_v3/
pipeline.py
若目前工作目錄在 projects/ 資料夾中。
你無法把 init.py 複製到映像裡。
docker build -t pipeline:v3 .
=> ERROR [4/4] COPY ../init.py / 0.0s
failed to compute cache key: "../init.py" not found: not found
除了從本機目錄複製外,常見做法是在建置映像時下載檔案:
RUN curl <file-url> -o <destination>
RUN unzip <dest-folder>/<filename>.zip
RUN rm <copy_directory>/<filename>.zip
RUN curl <file_download_url> -o <destination_directory>/<filename>.zip \
&& unzip <destination_directory>/<filename>.zip -d <unzipped-directory> \
&& rm <destination_directory>/<filename>.zip
| 用途 | Dockerfile 指令 |
|---|---|
| 從主機複製檔案到映像 | COPY <src-path-on-host> <dest-path-on-image> |
| 從主機複製資料夾到映像 | COPY <src-folder> <dest-folder> |
| 不能從建置 Dockerfile 的父目錄複製 |
透過在單一 RUN 指令中下載、解壓與清理,讓映像保持精簡:
RUN curl <file_download_url> -o <destination_directory> \
&& unzip <destination_directory>/<filename>.zip -d <unzipped-directory> \
&& rm <destination_directory>/<filename>.zip
Docker 入門