The Docker COPY folder is inexplicably tiled

Problem phenomenon

When making a docker image, there is a need to copy all files and folders under a certain path to the image, and I wrote the following dockerfile:

FROM alpine
WORKDIR /root/test_docker_proj
COPY * ./

The original directory structure is like this:

/projects/test_docker_proj
├── Dockerfile
├── dir1
│   ├── dir11
│   │   └── file11
│   └── file1
└── file2

However, the directory structure copied to the docker image becomes this:

/root/test_docker_proj
├── Dockerfile
├── dir11
│   └── file11
├── file1
└── file2

It can be seen that the folder dir1 has not been copied to the mirror, but the subfolders and files in dir1 have been copied in, and the files at the same level as dir1 have also been copied. In other words, during the execution of COPY, the first-level folder was "unpacked".

COPY/ADD behavior logic

To determine the behavior of COPY and similar ADD commands, the following tests were done:

FROM alpine

WORKDIR /root/test_docker_proj_1
COPY * ./

WORKDIR /root/test_docker_proj_2
ADD * ./

WORKDIR /root/test_docker_proj_3
COPY ./ ./

WORKDIR /root/test_docker_proj_4
ADD ./ ./

WORKDIR /root/test_docker_proj_5
COPY ./dir* ./

WORKDIR /root/test_docker_proj_6
ADD ./dir* ./

Through testing, we can find that the COPY/ADD command has several rules:

  1. The ADD and COPY commands behave the same when copying files
  2. When using * as the source of the COPY/ADD command, it means ./*
  3. If the source of the COPY/ADD command is a folder, the content of the folder is copied instead of itself
  4. The * in COPY ./* target will be translated into the following logic:
COPY ./sub_dir1 target
COPY ./sub_dir2 target
COPY ./file1 target
COPY ./file2 target

The folders and files in the file system are essentially files. The cp command of the familiar operating system will treat the folder as a file and copy it to the target path when executing cp * target. It can be considered that the file itself is copied. And docker's COPY/ADD copies its contents when copying folders.

This "strange" logic of docker has been criticized for a long time, but it does not seem to be changed. The latest progress can refer to the following two issues. Before docker makes changes, you can only pay attention to it when writing dockerfile. .

The Docker COPY folder is inexplicably tiled

Author: simpleapples
link: https: //www.jianshu.com/p/9b7da9aacd8a
Source: Jane books
are copyrighted by the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/yao_zhuang/article/details/114383125