Dockerfile - set ENV to result of command

后端 未结 6 1248
傲寒
傲寒 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:41

    Not sure if this is what you were looking for, but in order to inject ENV vars or ARGS into your .Dockerfile build this pattern works.

    in your my_build.sh:

    echo getting version of osbase image to build from
    OSBASE=$(grep "osbase_version" .version | sed 's/^.*: //')
    
    echo building docker
    docker build -f \
    --build-arg ARTIFACT_TAG=$OSBASE \
    PATH_TO_MY.Dockerfile \
    -t my_artifact_home_url/bucketname:$TAG .
    

    for getting an ARG in your .Dockerfile the snippet might look like this:

    FROM scratch
    ARG ARTIFACT_TAG
    FROM my_artifact_home_url/bucketname:${ARTIFACT_TAG}
    

    alternatively for getting an ENV in your .Dockerfile the snippet might look like this:

    FROM someimage:latest
    ARG ARTIFACT_TAG
    ENV ARTIFACT_TAG=${ARTIFACT_TAG}
    

    the idea is you run the shell script and that calls the .Dockerfile with the args passed in as options on the build.

提交回复
热议问题