Cannot update PATH variable in a windows docker container

Actual Behavior

Created a small test docker image from microsoft/windowsservercore, installed cmake using chocolatey (through Win10 PackageManagement) and then added cmake to the system PATH variable. I can build the image but when I run a container from it, the PATH is not getting updated correctly. The application cmake is installed correctly in the container and I have confirmed that.

See below for my dockerfile.

Expected Behavior

The PATH system variable should be updated correctly with the path to the cmake executable directory appeneded to it.

Related Information

I have ‘Docker for windows’ installed on windows 10 Pro 64 bit. I am currently running this in windows containers mode.

Here is my dockerfile:

FROM microsoft/windowsservercore
LABEL version="v2"
SHELL ["powershell", "-Command"]
RUN Get-PackageProvider -Force -Name Chocolatey
RUN Get-PackageProvider -Force -Name NuGet
RUN Register-PackageSource -Name NuGetGallery -Location https://www.nuget.org/api/v2/ -Provider Nuget -Trusted
RUN Install-Package -Force -Name cmake -RequiredVersion '3.7.2' -ProviderName Chocolatey
ENV PATH "C:\Program Files\CMake\bin:%PATH%"

Here is the output of docker version:

Client:
 Version:      17.03.1-ce
 API version:  1.27
 Go version:   go1.7.5
 Git commit:   c6d412e
 Built:        Tue Mar 28 00:40:02 2017
 OS/Arch:      windows/amd64

Server:
 Version:      17.03.1-ce
 API version:  1.27 (minimum version 1.24)
 Go version:   go1.7.5
 Git commit:   c6d412e
 Built:        Tue Mar 28 00:40:02 2017
 OS/Arch:      windows/amd64
 Experimental: true

What I have already tried

I tried following combinations to update the PATH but none of these work:

ENV PATH="C:\Program Files\CMake\bin:%{PATH}%"
ENV PATH "C:\Program Files\CMake\bin":PATH
ENV PATH "C:\Program Files\CMake\bin:%PATH%"
ENV PATH C:\Program Files\CMake\bin
RUN PATH %PATH%;C:\Program Files\CMake\bin

Steps to Reproduce

Install Docker for windows.
Switch to windows containers
Create a small test dockerfile with the following:

FROM microsoft/windowsservercore
ENV PATH "C:\Program Files:%PATH%"

(Add any directory to the system variable PATH in the Dockerfile, I just mentioned C:\Program Files as an example to reproduce the issue)

Any help/feedback is appreciated…

Thanks.

https://github.com/docker-library/golang/blob/master/1.8/windows/windowsservercore/Dockerfile#L31

1 Like

Hi Michael,

One correction…

I missed one line in my dockerfile (second last line) in the previous post which switches the SHELL to ‘cmd /S /C’ just before I update the PATH. I need to update the PATH in cmd and not in powershell. Here is the corrected dockerfile:

FROM microsoft/windowsservercore
LABEL version="v2"
SHELL ["powershell", "-Command"]
RUN Get-PackageProvider -Force -Name Chocolatey
RUN Get-PackageProvider -Force -Name NuGet
RUN Register-PackageSource -Name NuGetGallery -Location https://www.nuget.org/api/v2/ -Provider Nuget -Trusted
RUN Install-Package -Force -Name cmake -RequiredVersion '3.7.2' -ProviderName Chocolatey
SHELL ["cmd", "/S", "/C"]
ENV PATH "C:\Program Files\CMake\bin:%PATH%"

Any ideas?

-Ankit

ENV is not going to work on Windows. You have to do this (see the linked-to Dockerfile):

	$env:PATH = 'C:\git\cmd;C:\git\mingw64\bin;C:\git\usr\bin;' + $env:PATH; \
[Environment]::SetEnvironmentVariable('PATH', $env:PATH, [EnvironmentVariableTarget]::Machine); \

when I enter above two lines in my dockerfile I get this:

Unable to find type [EnvironmentVariableTarget].
At line:1 char:180

  • … onmentVariable(‘PATH’, $env:PATH, [EnvironmentVariableTarget]::Machin …
  •                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (EnvironmentVariableTarget:Typ
      eName) , ParentContainsErrorRecordException
    • FullyQualifiedErrorId : TypeNotFound

Sorry for formatting. As much as Windows is awesome for regular user, it is probably the worst OS developer could choose to work on…

So what do I do wrong? How can I change path variable?

1 Like

I am hitting the exact same issue. Wasting hours because I am not able to update a freaking PATH variable in a Windows container! Like what in the actual f*ck guys. How can something so easy become such a pain in the ass in Docker. Imho extremly counter-productive.

1 Like

This worked for me:

USER ContainerAdministrator
RUN setx /M PATH "%PATH%;C:/your/path"
USER ContainerUser

As seen in the .net sdk Dockerfile: https://github.com/dotnet/dotnet-docker/blob/20ea9f045a8eacef3fc33d41d58151d793f0cf36/2.1/sdk/nanoserver-1909/amd64/Dockerfile#L28-L29

This is a really old post but nothing worked for me and what finally did the trick was this:

# Env variable with the extra bits for the path
ENV ADD_PATH=";C:\Users\ContainerAdministrator\poetry\bin;C:\Users\ContainerAdministrator\app\.venv\bin"

# Change the path value directly in the registry key by appending the contents to the env variable to the contents of the path
RUN Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value ((Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path + "$Env:ADD_PATH")

# Verify the path is correct
RUN (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path

It is awful but it did what I needed and I’m hoping that it helps someone else!

3 Likes

Thanks for this; I tried every variation I could find, even lifted from the Microsoft Dockerfiles themselves, but nothing else worked; either the build would fail or the container would not run. This worked perfectly on the first try.

OMG! Thank you, @paulamariafdez !!!
I spent 3 days trying a multitude of solutions from forums, reddit, Microsoft, GitHub, stackexchange, etc. Nothing worked.
Even MSFT’s own dockerfiles in their GitHub repos error out on build.
This is the only solution that works for updating PATH via Dockerfile in Windows Containers.