3

How can i define variables in the Dockerfile for a windows image so they are accessible in subsequent RUN statements?

My test Dockerfile looks like this

FROM microsoft/windowsservercore

SHELL ["powershell", "-Command"]

ARG viaArg=value
ENV viaEnv="""value"""
RUN $viaShell=\"value\"
RUN echo """viaArg: $env:viaArg"""
RUN echo """viaEnv: $env:viaEnv"""
RUN echo """viaShell: $myusername"""
RUN echo """viaShell+env: $env:viaShell"""

when building the image via

PS > docker build -t myImage .

only viaArg and viaEnv show (a) value.

The first example here puts multiple commands in one line like

RUN $someVar=\"2.60.3\" ; echo $someVar

but surely there must be a different way.

Is using the $env namespace the only possible way?

How does setx take part in all of this?

Note: im not trying to pass variables from commandline, so ARG may already be used in a wrong manner. And i dont want to set the variables permanently, they should only exist during the build run, so i dont need them in the registry.

Used versions are

PS > docker -v
Docker version 17.12.0-ce, build c97c6d6
PS > [System.Environment]::OSVersion.Version    
Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      14393  0
PS > (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId
1607
BNT
  • 936
  • 10
  • 27
  • the only way is with the `ENV or ARG` if you want to use in your environemnt and not save the in the registry. As last commands you can delete them in your dockerfile if this is what you are after. – Edwin Jan 16 '18 at 13:54
  • You are trying to pass environment variable from your host into build? – Gregory Suvalian Jan 16 '18 at 17:22
  • @GregorySuvalian nope, just define some variables that are used in later `RUN` statements, it seemed off to use the `$end:` namespace for that but @Edwin states that is the only possibility. Kind of related is my other linked question. – BNT Jan 17 '18 at 12:07

0 Answers0