34

I need to append to the PATH within a Windows Docker container, and I've tried many permutations.

ENV PATH=%PATH%;C:\\Foo\\bin
ENV PATH=$PATH;C:\\Foo\\bin
ENV PATH="%PATH%;C:\Foo\bin"
ENV PATH="$PATH;C:\Foo\bin"
RUN "set PATH=%PATH%;C:\Foo\bin"

None of these work: they don't evaluate the preexisting PATH variable.

What is the right syntax to append to the PATH? Can I even append to the PATH inside Docker? (I can on similar Linux containers)

macetw
  • 1,399
  • 1
  • 15
  • 22
  • Environment variables which you pass to Docker container are only visible for processes executed by docker executable. They will not be visible machine wide. But if you are ok with that then why not just read this variable during start up and append it PATH variable once inside container instead. Issue with environment variables not visible system wide is here https://github.com/docker/docker/issues/30192 – Gregory Suvalian Feb 07 '17 at 14:58
  • 1
    @GSA, if there is no solution, I'd consider that an answer, too. But I'm surprised that the "Append to the PATH for my container environment" isn't a solved user-story for Windows containers. – macetw Feb 07 '17 at 15:10

5 Answers5

52

Unfortunately ENV won't work, because windows environment variable work a little differently than linux. more info

As of now the only way to do this is through RUN

But you don't need to create a separate file to do this. This can be done by the following much simpler one line command:

RUN setx path "%path%;C:\Foo\bin"

mirsik
  • 881
  • 8
  • 9
  • 21
    PS equiv: RUN setx /M PATH $($Env:PATH + ';C:\Foo\bin) – JasonCoder Aug 21 '18 at 15:44
  • 7
    @JasonCoder - thanks! Just missing a final single quote before the last paren. – Glenn Sep 12 '18 at 20:30
  • We tried your solution today, and ran into the trouble that setx did not work but only ENV, as suggested here: https://github.com/PowerShell/PowerShell-Docker/issues/164 I'm quite puzzled; but we will soon switch to linux containers anyway... – Hermann.Gruber Dec 18 '19 at 15:57
11

You can set environment variables permanently in the container using a powershell script.

Create a powershell script in yout docker context (e.g. setpath.ps1 ) containing this:

[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\Foo\bin", [EnvironmentVariableTarget]::Machine)

Add this script to your container dockerfile and RUN the script. Add something like this to your dockerfile:

ADD ./setpath.ps1 c:/MyScripts/setpath.ps1
RUN powershell -Command c:\MyScripts\setpath.ps1
cer49152
  • 111
  • 1
  • 4
  • This does not work for me. The PATH variable is the same as before, regardless of whether I have this line in my dockerfile. Any ideas? – cheesus Aug 21 '18 at 12:05
9

[Environment]::SetEnvironmentVariable is a good way, but will not work in nanoserver. The best choice is:

RUN setx path '%path%;C:\Foo\bin'
mpromonet
  • 10,379
  • 42
  • 54
  • 85
cherishty
  • 333
  • 1
  • 4
  • 11
6

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

Mark Nguyen
  • 6,828
  • 9
  • 30
  • 39
2

Despite all previous answers, I've faced an issue in some environments. Basically on a custom local test environment the setx using the %PATH%;C:\foo\bar way works even when the folder has spaces like C:\Program Files. That though didn't work when trying it on our production environment. Checking what Microsoft do when they install the base packages on their own images it turns out a better and more reliable way is to use the command this way:

RUN setx /M PATH $(${Env:PATH} + \";${Env:ProgramFiles(x86)}\foo\bar\")

This way docker will be able to get the proper paths and properly update the PATH data.

Edit: I've fixed the missing trailing \ in the command, thanks to Robin Ding :)

  • Missing \ at the end: ```RUN setx /M PATH $(${Env:PATH} + \";${Env:ProgramFiles(x86)}\foo\bar\")``` Also works with space: ```RUN setx /M PATH $(${Env:PATH} + \";${Env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\")``` – Robin Ding Aug 03 '21 at 07:10