Changing the PATH variable in Dockerfile for a Windows container

152 Views Asked by At

I am building a Windows Docker container and I would like to add a path to the PATH environment variable.

I did not have success using the ENV command in the Dockerfile. Is there a way to use this command to achieve my goal with Windows containers?

As I workaround I tried

RUN setx /m PATH %PATH%;Hello

and this actually set the PATH variable successfully. Is there some downside to doing it like this?

Another thing I noticed is that executing the equivalent in PowerShell (with SHELL set to PowerShell) did cause errors afterwards:

Step 1/6 : FROM mcr.microsoft.com/windows/servercore:ltsc2019
 ---> ce4ffba65ace
Step 2/6 : SHELL ["PowerShell", "-Command"]
 ---> Using cache
 ---> 69c6518616c7
Step 3/6 : RUN echo $env:PATH
 ---> Using cache
 ---> dbd17db2deb3
Step 4/6 : RUN setx /m PATH '$env:PATH;Hello'
 ---> Running in d697e2132ddf

SUCCESS: Specified value was saved.
Removing intermediate container d697e2132ddf
 ---> 150c85a8a063
Step 5/6 : RUN echo $env:PATH
 ---> Running in c4638d8ffa24
container c4638d8ffa243ebd85f684c8891dd8110831ef9fe083526b996211f6f28bc188 encountered an error during hcs::System::CreateProcess: failure in a Windows system call: The system cannot find the file specified. (0x2)

Is there something wrong with this?

1

There are 1 best solutions below

2
Antonio Petricca On

You have to supply the Windows path at build time like follow:

Docker file

ARG WINDOWS_PATH
RUN setx /m PATH ${WINDOWS_PATH};Hello

Docker building script

docker build --build-arg WINDOWS_PATH=%PATH%