powershelldockerfilewindows-container

Dockerfile - How can I declare a variable in a Dockerfile that I can use in multiple RUN instructions?


I'm writing a Dockerfile. I want to have a variable that I can pass around to different PowerShell scripts.

FROM mcr.microsoft.com/windows/servercore/iis
ENV CONT_IMG_VER test
RUN echo $CONT_IMG_VER

Outputs

Step 4/4 : RUN echo $CONT_IMG_VER
 ---> Running in 70c2085d9735
$CONT_IMG_VER

Instead of

Step 4/4 : RUN echo $CONT_IMG_VER
 ---> Running in 70c2085d9735
test

UPDATE: Per Dockerfile reference looks like RUN does not supports ENV.

How can I declare a variable in a Dockerfile that I can use in multiple RUN instructions?

enter image description here


Solution

  • One way that you can pass an ENV variable to a script is just have the powershell script reference the ENV variables you have configured in your docker container.

    For example. In your dockerfile if you are trying to inject the following ENV variable

    ENV CONT_IMG_VER=test
    

    Have your powershell script pull that variable by either doing a get-childitem

    (get-childitem ENV:CONT_IMG_VER).value
    

    or referencing the powershell env items.

    $env:CONT_IMG_VER