Appending to PATH in a Windows Docker container

前端 未结 4 1246
别跟我提以往
别跟我提以往 2020-12-16 13:22

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\         


        
相关标签:
4条回答
  • 2020-12-16 13:46

    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

    0 讨论(0)
  • 2020-12-16 13:52

    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
    
    0 讨论(0)
  • 2020-12-16 13:59

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

    RUN setx path '%path%;C:\Foo\bin'
    
    0 讨论(0)
  • 2020-12-16 14:05

    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"

    0 讨论(0)
提交回复
热议问题