Dockerfile复制保持子目录结构

时间:2015-05-13 13:09:43

标签: copy docker dockerfile

我正在尝试将一些文件和文件夹复制到我的localhost中的docker镜像构建中。

文件是这样的:

folder1
    file1
    file2
folder2
    file1
    file2

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

COPY files/* /files/

但是,所有文件都放在/ files /中,Docker中是否有办法保留子目录结构以及将文件复制到目录中?

4 个答案:

答案 0 :(得分:277)

使用此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

答案 1 :(得分:8)

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

FROM ubuntu
COPY . /
RUN ls -la /

答案 2 :(得分:1)

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

COPY files/. /files/

答案 3 :(得分:-1)

如果您要完全复制具有相同目录结构的源目录, 然后不要使用星号(*)。如下所示在Dockerfile中编写COPY命令。

COPY . destinatio-directory/ 
相关问题