docker - Dockerfile - 通过在单个语句中复制来减少层数
问题描述
目前,在我的 dockerfile 中,我使用多个 COPY 命令从我的存储库中复制目录。
- 复制 requirements.txt requirements.txt
- 复制验证 /opt/validation
- 复制模板 /opt/templates
- 复制 goss /opt/goss
- 复制纽曼 /opt/newman
- 复制 conftest.py /opt/validation/conftest.py
如何使用单个 COPY 命令获得与上述相同的结果。有办法吗?
解决方案
如果您只想将内容复制到同一个文件夹/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
将其移动到指定文件夹。
推荐阅读
- windows - 如何让 ReadLineAsSecureString() 接受管道输入?
- java - android没有意识到进度条已初始化
- python - 从数组求和,直到达到给定值。
- javascript - Spring boot:使用@Path变量时JQuery验证不起作用
- java - CompletableFuture allof(..).join() 与 CompletableFuture.join()
- android - AIDL 在调试 APK 中工作,但在签名APK 中不工作
- java - 为什么我看不到运行 JavaFX Web 视图的 URL 网站?
- javascript - 使用 JavaScript 在悬停时添加类
- python - 基于半一致特征拆分字符串
- forms - Symfony - 需要的个人递归函数 = true 返回 false