0

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

JosephS
  • 744
  • 5
  • 22
  • You're right: it should work both ways: with `=` or `space`. Perhaps it that's something specific for Windows. Sorry, Windows it's not my strength and I don't have a Windows machine nearby to reproduce. I'm deleting my initial answer. Hopefully someone else will answer. My guess is that it's something wrong with that echo command. Perhaps the variable is set but that echo does not interpret the $CONT_IMG_VER – Neo Anderson Jul 21 '20 at 19:02

2 Answers2

2

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
James Rhoat
  • 170
  • 8
0

To add the environment variables in the Windows container, follow the below steps

  1. Add environment variable in the dockerfile ENV MYENVVAR

  2. RUN $env:MYENVVAR

docker run -e MYENVVAR=<ACTUVAL_VALUE>