将几个目录复制到另一个目录 [英] Copy several directories to another directory
问题描述
如何将多个目录复制到Docker中的目标目录?我不想复制目录内容,而是复制整个目录结构。
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.
COPY
和 ADD
命令复制目录内容,展平结构,这是我不希望的。也就是说,如果这些是我的来源:
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
我希望将其部署到docker映像:
I want this to be deployed to the docker image:
code/
├── a
│ ├── aaa.txt
│ └── uuu.txt
├── b
│ ├── ooo.txt
│ └── ppp.txt
└── c
└── jjj.txt
我知道我可以这样做:
ADD a /code/a
ADD b /code/b
ADD c /code/c
但是,与Linux cp
命令,太冗长了。
But this is, compared to the linux cp
command, too verbose. It also creates unnecessary layers.
还有更好的方法吗?
推荐答案
您可以这样做:
COPY ./ /code/
它将所有内容从当前文件夹复制到图像的 / code 文件夹中。
It will copy everything from the current folder into the /code folder of your image.
因此,您可以创建 .dockerignore 文件,以防止添加其他文件/目录,然后添加 a , b 和 c 。例如, d , e 和 f 是当前文件夹中的其他目录,这些目录不应位于结果图像中,否则的内容.dockerignore 文件如下所示:
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
此方法的缺点是它还会将 .dockerignore 复制到 / code 文件夹。
Disadvantage of this approach is that it will copy also .dockerignore into the /code folder.
这篇关于将几个目录复制到另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!