Conditional COPY/ADD in Dockerfile?

后端 未结 6 1093
暖寄归人
暖寄归人 2020-12-01 04:12

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

6条回答
  •  心在旅途
    2020-12-01 04:36

    Here is a simple workaround:

    COPY foo file-which-may-exist* /target
    

    Make sure foo exists, since COPY needs at least one valid source.

    If file-which-may-exist is present, it will also be copied.

    NOTE: You should take care to ensure that your wildcard doesn't pick up other files which you don't intend to copy. To be more careful, you could use file-which-may-exist? instead (? matches just a single character).

    Or even better, use a character class like this to ensure that only one file can be matched:

    COPY foo file-which-may-exis[t] /target
    

提交回复
热议问题