考慮以下Dockerfile. 在最后幾行,首先git安裝,然后將一些內容附加到路徑環境變數中。
FROM mcr.microsoft.com/windows/servercore:ltsc2022
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop';"]
RUN Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
RUN choco install -y git
RUN [Environment]::SetEnvironmentVariable('Path', $Env:Path ';C:\my-path', [EnvironmentVariableTarget]::Machine)
構建完成后,路徑看起來像這樣,所以git被添加到路徑中。
C:\ProgramData\chocolatey\bin;C:\Program Files\Git\cmd;C:\my-path;
這是一個等價的Dockerfile,但我已將最后幾行變成單個RUN命令以進行優化。
FROM mcr.microsoft.com/windows/servercore:ltsc2022
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop';"]
RUN Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
RUN choco install -y git; \
[Environment]::SetEnvironmentVariable('Path', $Env:Path ';C:\my-path', [EnvironmentVariableTarget]::Machine)
構建完成后,git不在路徑上!
C:\ProgramData\chocolatey\bin;C:\my-path;
為什么會這樣,我該如何解決?
uj5u.com熱心網友回復:
一種解決方法是使用cmd而不是powershell.
以下兩種方法都有效:
RUN choco install -y git; \
cmd /c "setx path '%path%;C:\my-path'"
SHELL ["cmd", "/S", "/C"]
RUN choco install -y git && \
setx path "%path%;C:\my-path"
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop';"]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/474215.html
標籤:码头工人 电源外壳 dockerfile 环境变量
