Conditional COPY/ADD in Dockerfile?

后端 未结 6 1081
暖寄归人
暖寄归人 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:41

    As stated by this comment, Santhosh Hirekerur's answer still copies the file, to archive a true conditional copy, you can use this method.

    ARG BUILD_ENV=copy
    
    FROM alpine as build_copy
    ONBUILD COPY file /file
    
    FROM alpine as build_no_copy
    ONBUILD RUN echo "I don't copy"
    
    FROM build_${BUILD_ENV}
    # other stuff
    

    The ONBUILD instructions ensures that the file is only copied if the "branch" is selected by the BUILD_ENV. Set this var using a little script before calling docker build

提交回复
热议问题