Christian Bongiorno
Christian Bongiorno

Reputation: 5648

How to update path in Windows container for Docker?

I am trying to update the path inside my container. I have been everywhere and checked out several threads on this and nothing works. So, that's the trick?


# escape=`
ARG SDK_VERSION=4.8

FROM mcr.microsoft.com/dotnet/framework/sdk:${SDK_VERSION}

ENV NODE_VERSION=8.11.2

HEALTHCHECK NONE

RUN Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object 
System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

USER ContainerAdministrator

RUN setx /M PATH "%PATH%;C:/Foo/bin"

USER ContainerUser

SHELL ["cmd", "/S", "/C"]

CMD ["powershell.exe", "-NoLogo", "-ExecutionPolicy", "Bypass"]

This is the error:

PS C:\users\cbongiorno\source> docker build -t mercury:latest -m 4GB -f Dockerfile i18n-tools
Sending build context to Docker daemon  2.057MB
Step 1/10 : ARG SDK_VERSION=4.8
Step 2/10 : FROM mcr.microsoft.com/dotnet/framework/sdk:${SDK_VERSION}
 ---> 4a9d58026a2d
Step 7/10 : RUN setx /M PATH "%PATH%;C:/Foo/bin"
 ---> Running in 1caf9e758af2

SUCCESS: Specified value was saved.
C:/Foo/bin : The term 'C:/Foo/bin' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At line:1 char:96
+ ... ogressPreference = 'SilentlyContinue'; setx /M PATH %PATH%;C:/Foo/bin
+                                                                ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:/Foo/bin:String) [], ParentCo
   ntainsErrorRecordException
    + FullyQualifiedErrorId : CommandNotFoundException

The command 'powershell -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; setx /M PATH "%PATH%;C:/Foo/bin"' returned a non-zero code: 1

Upvotes: 1

Views: 1482

Answers (2)

mloskot
mloskot

Reputation: 38942

There are at least two ways to update the PATH:

  1. Using setx as the earlier answer suggests and as is displayed in the golang's nanoserver-1809/Dockerfile. Notice the use of the USER instruction. The ContainerAdministrator must be used in order to set the system PATH:

    # PATH isn't actually set in the Docker image, so we have to set it from within the container
    USER ContainerAdministrator
    RUN setx /m PATH "%GOPATH%\bin;C:\go\bin;%PATH%"
    USER ContainerUser
    # doing this first to share cache across versions more aggressively
    
  2. Using Powershell as displayed in the golang's windowsservercore-ltsc2016/Dockerfile

    RUN $newPath = ('{0}\bin;C:\go\bin;{1}' -f $env:GOPATH, $env:PATH); \
        Write-Host ('Updating PATH: {0}' -f $newPath); \
        [Environment]::SetEnvironmentVariable('PATH', $newPath, [EnvironmentVariableTarget]::Machine);
    # doing this first to share cache across versions more aggressively
    

Both ways do work with recent Docker Desktop.

The ENV instruction is not going to work on Windows.

Upvotes: 2

Christian Bongiorno
Christian Bongiorno

Reputation: 5648

This is what I got working, but it doesn't seem ideal:

RUN setx /M PATH $($Env:PATH + ';C:\Foo\bin')

Upvotes: 1

Related Questions