在我的 Dockerfiles 中,如果存在,我想将一个文件复制到我的图像中,pip 的 requirements.txt 文件似乎是一个不错的候选者,但是如何实现呢?
COPY (requirements.txt if test -e requirements.txt; fi) /destination
...
RUN if test -e requirements.txt; then pip install -r requirements.txt; fi
或者
if test -e requirements.txt; then
COPY requiements.txt /destination;
fi
RUN if test -e requirements.txt; then pip install -r requirements.txt; fi
原文由 derrend 发布,翻译遵循 CC BY-SA 4.0 许可协议
目前不支持此功能(因为我怀疑它会导致无法复制的图像,因为相同的 Dockerfile 会复制或不复制该文件,具体取决于它的存在)。
在 issue 13045 中仍然要求使用通配符:“
COPY foo/* bar/" not work if no file in foo
”(2015 年 5 月)。它现在(2015 年 7 月)不会在 Docker 中实现,但是像 bocker 这样的另一个构建工具可以支持这一点。
2021 年:
2022