问题
How would you copy several directories to a destination directory in docker? I do not want to copy the directory contents, but the whole directory structure.
The COPY
and ADD
commands copy the directory contents, flattening the structure, which I do not want. That is, if these are my sources:
.
├── a
│ ├── aaa.txt
│ └── uuu.txt
├── b
│ ├── ooo.txt
│ └── ppp.txt
└── c
└── jjj.txt
I want this to be deployed to the docker image:
code/
├── a
│ ├── aaa.txt
│ └── uuu.txt
├── b
│ ├── ooo.txt
│ └── ppp.txt
└── c
└── jjj.txt
I know I can do this:
ADD a /code/a
ADD b /code/b
ADD c /code/c
But this is, compared to the linux cp
command, too verbose. It also creates unnecessary layers.
Is there a better way?
回答1:
You can do:
COPY ./ /code/
It will copy everything from the current folder into the /code folder of your image.
So then you can create .dockerignore file to prevent of adding other files/directories then a, b and c. For example d, e and f are other directories in the current folder which should not be in the result image then content of the .dockerignore file will look like:
Dockerfile*
d
e
f
Disadvantage of this approach is that it will copy also .dockerignore into the /code folder.
来源:https://stackoverflow.com/questions/52605123/copy-several-directories-to-another-directory