How to make a build arg mandatory during Docker build?

后端 未结 6 1932
后悔当初
后悔当初 2020-12-23 16:15

Is there any way to make a build argument mandatory during docker build? The expected behaviour would be for the build to fail if the argument is missing.

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-23 16:54

    You could do something like this...

     FROM ubuntu:14.04
     ONBUILD ARG MY_VARIABLE
     ONBUILD RUN if [ -z "$MY_VARIABLE" ]; then echo "NOT SET - ERROR"; exit 1; else : ; fi
    

    Then docker build -t my_variable_base .

    Then build your images based on this...

    FROM my_variable_base
    ...
    

    It's not super clean, but at least it abstracts the 'bleh' stuff away to the base image.

提交回复
热议问题