首页 > 解决方案 > Dockerfile - 通过在单个语句中复制来减少层数

问题描述

目前,在我的 dockerfile 中,我使用多个 COPY 命令从我的存储库中复制目录。

如何使用单个 COPY 命令获得与上述相同的结果。有办法吗?

标签: dockerdockerfilecopy

解决方案


如果您只想将内容复制到同一个文件夹/opt,也许只需使用下一个:

文件夹结构:

.
├── conftest.py
├── Dockerfile
├── .dockerignore
├── goss
├── newman
├── requirements.txt
├── templates
└── validation

Dockerfile:

FROM alpine
COPY . /opt
#RUN mv /opt/conftest.py /opt/validation
RUN ls /opt

.dockerignore:

Dockerfile

执行:

$ docker build -t abc:1 . --no-cache
Sending build context to Docker daemon  6.144kB
Step 1/3 : FROM alpine
 ---> 28f6e2705743
Step 2/3 : COPY . /opt
 ---> 8beb53be958c
Step 3/3 : RUN ls /opt
 ---> Running in cfc9228124fb
conftest.py
goss
newman
requirements.txt
templates
validation
Removing intermediate container cfc9228124fb
 ---> 4cdb9275d6f4
Successfully built 4cdb9275d6f4
Successfully tagged abc:1

在这里,我们使用COPY . /opt将当前文件夹中/opt/的所有内容复制到容器中。我们.dockerignore习惯于忽略不想复制到容器的文件/文件夹。

另外,不确定规则是否COPY conftest.py /opt/validation/conftest.py正确,如果正确,您可能必须使用RUN mv将其移动到指定文件夹。


推荐阅读