Dockerfile复制保留子目录结构

Dockerfile copy keep subdirectory structure

我正在尝试从我的本地主机将一些文件和文件夹复制到 docker 图像构建。

文件是这样的:

folder1/
    file1
    file2
folder2/
    file1
    file2

我正在尝试制作这样的副本:

COPY files/* /files/

但是,folder1/folder2/ 中的所有文件都直接放在 /files/ 中,没有它们的文件夹:

files/
    file1
    file2

在 Docker 中有没有办法保持子目录结构并将文件复制到它们的目录中?

使用此 Dockerfile 从 COPY 中删除星标:

FROM ubuntu
COPY files/ /files/
RUN ls -la /files/*

结构在那里:

$ docker build .
Sending build context to Docker daemon 5.632 kB
Sending build context to Docker daemon 
Step 0 : FROM ubuntu
 ---> d0955f21bf24
Step 1 : COPY files/ /files/
 ---> 5cc4ae8708a6
Removing intermediate container c6f7f7ec8ccf
Step 2 : RUN ls -la /files/*
 ---> Running in 08ab9a1e042f
/files/folder1:
total 8
drwxr-xr-x 2 root root 4096 May 13 16:04 .
drwxr-xr-x 4 root root 4096 May 13 16:05 ..
-rw-r--r-- 1 root root    0 May 13 16:04 file1
-rw-r--r-- 1 root root    0 May 13 16:04 file2

/files/folder2:
total 8
drwxr-xr-x 2 root root 4096 May 13 16:04 .
drwxr-xr-x 4 root root 4096 May 13 16:05 ..
-rw-r--r-- 1 root root    0 May 13 16:04 file1
-rw-r--r-- 1 root root    0 May 13 16:04 file2
 ---> 03ff0a5d0e4b
Removing intermediate container 08ab9a1e042f
Successfully built 03ff0a5d0e4b

或者您可以使用“.”而不是 *,因为这将获取工作目录中的所有文件,包括文件夹和子文件夹:

FROM ubuntu
COPY . /
RUN ls -la /

将本地目录合并到图像中的目录中,请执行此操作。 它不会删除图像中已经存在的文件。它只会添加本地存在的文件,如果已存在同名文件,则会覆盖图像中的文件。

COPY ./local-path/. /image-path/

如果你想复制一个完全相同目录结构的源目录, 那么不要使用星号 (*)。如下在 Dockerfile 中写入 COPY 命令。

COPY . destinatio-directory/