问题
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
来源:https://stackoverflow.com/questions/48282208/variable-handling-during-windows-docker-build