且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Dockerfile中的条件COPY / ADD?

更新时间:2023-09-26 08:38:58

目前不支持(因为我怀疑它会导致不可重现的图像,因为相同的Docker文件会根据文件的存在而复制或不复制)。



仍然要求,在问题13045 中,使用通配符: COPY foo / * bar /不工作,如果没有文件在foo (2015年5月)。

它不会实现现在(2015年7月)在Docker,但另一个构建工具,如 bocker 可以支持这个。


Inside of my Dockerfiles I would like to COPY a file into my image if it exists, the requirements.txt file for pip seems like a good candidate but how would this be achieved?

COPY (requirements.txt if test -e requirements.txt; fi) /destination
...
RUN  if test -e requirements.txt; then pip install -r requirements.txt; fi

or

if test -e requirements.txt; then
    COPY requiements.txt /destination;
fi
RUN  if test -e requirements.txt; then pip install -r requirements.txt; fi

Thanks :)

This isn't currently supported (as I suspect it would lead to non-reproducible image, since the same Dockerfile would copy or not the file, depending on its existence).

This is still requested, in issue 13045, using wildcards: "COPY foo/* bar/" not work if no file in foo" (May 2015).
It won't be implemented for now (July 2015) in Docker, but another build tool like bocker could support this.