Dockerfile - set ENV to result of command

后端 未结 6 1258
傲寒
傲寒 2020-12-02 15:17

Is it possible to set a docker ENV variable to the result of a command? Like:

ENV MY_VAR whoami

i want MY_VAR to get the value \"root\" or

6条回答
  •  心在旅途
    2020-12-02 15:52

    As an addition to @DarkSideF's answer, if you want to reuse the result of a previous command in your Dockerfile during in the build process, you can use the following workaround:

    1. run a command, store the result in a file
    2. use command substitution to get the previous result from that file into another command

    For example :

    RUN echo "bla" > ./result
    RUN echo $(cat ./result)
    

    For something cleaner, you can use also the following gist which provides a small CLI called envstore.py :

    RUN envstore.py set MY_VAR bla
    RUN echo $(envstore.py get MY_VAR)
    

    Or you can use python-dotenv library which has a similar CLI.

提交回复
热议问题