简体   繁体   English

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

[英]Dockerfile COPY copies only one subdirectory of a directory it should copy

I have directory structure like this:我有这样的目录结构:

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

In each of there subdirectories there are also some files.在每个子目录中还有一些文件。

In Dockerfile I have lineDockerfile我有一行

COPY opt/* /opt/

But when I create container from this image, and run /bin/bash , and go to /opt and run ls -al I can see there is only bin directory, with it's content.但是,当我从此映像创建容器并运行/bin/bash ,然后转到/opt并运行ls -al我可以看到只有bin目录及其内容。 No dcmtk-3.6.5-linux-x86_64-static directory, no other subdirectories like etc or share .没有dcmtk-3.6.5-linux-x86_64-static目录,没有其他子目录,如etcshare

There are also some other files I keep in opt , those are copied just as expected.我还保留了一些其他文件opt ,这些文件按预期复制。

Why isn't whole dcmtk-3.6.5-linux-x86_64-static copied?为什么不复制整个dcmtk-3.6.5-linux-x86_64-static

As the documentation says:正如文档所说:

If <src> is a directory, the entire contents of the directory are copied, including filesystem metadata.如果 <src> 是目录,则复制目录的全部内容,包括文件系统元数据。

So you should use the following:所以你应该使用以下内容:

COPY opt /opt

Just leave out the /* and specify only the directory.只需省略/*并仅指定目录。

Edit 01.10.2020 : 2020 年 1 月 10 日编辑

Just tried again with the following setup:刚刚使用以下设置再次尝试:

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: Dockerfile:

FROM busybox:latest
COPY opt /opt

building the image:构建图像:

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

running the container:运行容器:

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

and checking what's in there:并检查里面有什么:

# 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

So the whole directory structure was copied, including the empty directory.所以整个目录结构都被复制了,包括空目录。 There must be something interfering in you setup一定有什么东西干扰了你的设置

Extracts from the documentation (in relevant order for my answer)文档摘录(按照我的回答的相关顺序)

If multiple <src> resources are specified, either directly or due to the use of a wildcard , then <dest> must be a directory, and it must end with a slash /如果直接指定了多个 <src> 资源,或者由于使用了通配符,则 <dest> 必须是目录,并且必须以斜杠 / 结尾

[...] [...]

If <src> is a directory, the entire contents of the directory are copied, including filesystem metadata.如果 <src> 是目录,则复制目录的全部内容,包括文件系统元数据。

Note: The directory itself is not copied, just its contents.注意:不会复制目录本身,只会复制其内容。

Solution:解决方案:

COPY opt /opt/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM