Dockerfile COPY仅复制它应复制的目录的一个子目录

时间:2020-09-29 16:32:13

标签: docker dockerfile

我有这样的目录结构:

opt
  - dcmtk-3.6.5-linux-x86_64-static
    | - bin    
    | - etc
    | - share
     

在每个子目录中也有一些文件。

Dockerfile中,我有一行

COPY opt/* /opt/

但是,当我从该图像创建容器并运行/bin/bash,并转到/opt并运行ls -al时,我看到只有bin目录,它是内容。没有dcmtk-3.6.5-linux-x86_64-static目录,没有其他子目录,例如etcshare

我还保存了opt中的一些其他文件,这些文件已按预期复制。

为什么不完整复制dcmtk-3.6.5-linux-x86_64-static

2 个答案:

答案 0 :(得分:1)

documentation说:

如果是目录,则将复制目录的整个内容,包括文件系统元数据。

因此,您应该使用以下内容:

COPY opt /opt

只需省略/*并仅指定目录。

编辑01.10.2020

只需使用以下设置再次尝试:

ls -lR
total 8
-rw-r--r--  1 peter  staff   34  1 Okt 13:06 Dockerfile
drwxr-xr-x  4 peter  staff  128  1 Okt 13:01 opt

./opt:
total 0
drwxr-xr-x  2 peter  staff   64  1 Okt 13:01 emptydir
drwxr-xr-x  4 peter  staff  128  1 Okt 13:01 foo

./opt/emptydir:

./opt/foo:
total 0
drwxr-xr-x  3 peter  staff  96  1 Okt 13:01 bar
-rw-r--r--  1 peter  staff   0  1 Okt 13:01 foo.txt

./opt/foo/bar:
total 0
-rw-r--r--  1 peter  staff  0 29 Sep 19:15 bar.txt

Dockerfile:

FROM busybox:latest
COPY opt /opt

构建图像:

docker build -t so-test .
Sending build context to Docker daemon   5.12kB
Step 1/2 : FROM busybox:latest
 ---> 6858809bf669
Step 2/2 : COPY opt /opt
 ---> Using cache
 ---> f6f2692b571a
Successfully built f6f2692b571a
Successfully tagged so-test:latest

运行容器:

docker run -it so-test /bin/sh
/ #

并检查其中的内容:

# ls -lR /opt
/opt:
total 8
drwxr-xr-x    2 root     root          4096 Oct  1 11:01 emptydir
drwxr-xr-x    3 root     root          4096 Oct  1 11:01 foo

/opt/emptydir:
total 0

/opt/foo:
total 4
drwxr-xr-x    2 root     root          4096 Oct  1 11:01 bar
-rw-r--r--    1 root     root             0 Oct  1 11:01 foo.txt

/opt/foo/bar:
total 0
-rw-r--r--    1 root     root             0 Sep 29 17:15 bar.txt

因此复制了整个目录结构,包括空目录。 设置中一定有干扰

答案 1 :(得分:0)

documentation中提取(按照我的回答的相关顺序)

如果直接或由于使用通配符而指定了多个资源 ,则必须是目录,并且必须以斜杠/

[...]

如果是目录,则将复制目录的整个内容,包括文件系统元数据。

注意:目录本身不会被复制,只是其内容被复制。

解决方案:

COPY opt /opt/
相关问题