Docker中用pwsh安装vcpkg报错:powershell.exe未找到如何解决?
解决vcpkg bootstrap脚本在仅含pwsh的Windows容器中找不到powershell.exe的问题
问题背景
在基于mcr.microsoft.com/dotnet/sdk:7.0的Windows容器中安装vcpkg时,执行bootstrap-vcpkg.bat报错:'powershell.exe' is not recognized as an internal or external command。容器仅预装PowerShell 7(pwsh),无传统Windows PowerShell的powershell.exe。
复现步骤与错误日志
执行docker build .的输出:
> docker build . Sending build context to Docker daemon 2.048kB Step 1/7 : FROM mcr.microsoft.com/dotnet/sdk:7.0 ---> 43067ec72d68 Step 2/7 : WORKDIR C:/dev/ ---> Running in 44765abf2793 Removing intermediate container 44765abf2793 ---> 863d49ebd1c4 Step 3/7 : ARG VCPKG_VERSION=2023.07.21 ---> Running in 2c95eca3b986 Removing intermediate container 2c95eca3b986 ---> 1b6f0b11bef4 Step 4/7 : ADD https://github.com/microsoft/vcpkg/archive/refs/tags/$VCPKG_VERSION.zip C:\TEMP\vcpkg.zip Downloading 7.571MB ---> c957800f783c Step 5/7 : RUN tar -xf C:\TEMP\vcpkg.zip && ren vcpkg-%VCPKG_VERSION% vcpkg && del /f /q c:\TEMP\*.*; ---> Running in e2d020f011a6 Removing intermediate container e2d020f011a6 ---> d5511b253da3 Step 6/7 : SHELL ["pwsh", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"] ---> Running in 2c4d887da6a1 Removing intermediate container 2c4d887da6a1 ---> 14b149981886 Step 7/7 : RUN .\vcpkg\bootstrap-vcpkg.bat -disableMetrics; ---> Running in 31e9ab80d53d 'powershell.exe' is not recognized as an internal or external command, operable program or batch file.
所用Dockerfile
# escape=` FROM mcr.microsoft.com/dotnet/sdk:7.0 WORKDIR C:/dev/ ARG VCPKG_VERSION=2023.07.21 ADD https://github.com/microsoft/vcpkg/archive/refs/tags/$VCPKG_VERSION.zip C:\TEMP\vcpkg.zip RUN tar -xf C:\TEMP\vcpkg.zip ` && ren vcpkg-%VCPKG_VERSION% vcpkg ` && del /f /q c:\TEMP\*.*; SHELL ["pwsh", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"] RUN .\vcpkg\bootstrap-vcpkg.bat -disableMetrics;
容器环境验证
容器中仅存在pwsh:
> docker run --rm mcr.microsoft.com/dotnet/sdk:7.0 pwsh --version PowerShell 7.3.10
调用powershell.exe会报错:
> docker run --rm mcr.microsoft.com/dotnet/sdk:7.0 powershell --version docker: Error response from daemon: container 615360a3654bbe128045fa96770d163dc6c9079b9e86e6d42357eb9e6ee6d10e encountered an error during hcs::System::CreateProcess: failure in a Windows system call: The system cannot find the file specified. (0x2) [Event Detail: Provider: 00000000-0000-0000-0000-000000000000] [Event Detail: Provider: 00000000-0000-0000-0000-000000000000] [Event Detail: onecore\vm\compute\management\orchestration\vmhostedcontainer\processmanagement.cpp(173)\vmcomputeagent.exe!00007FF606A8A1DB: (caller: 00007FF606A3E19A) Exception(1) tid(390) 80070002 The system cannot find the file specified. CallContext:[\Bridge_ProcessMessage\VmHostedContainer_ExecuteProcess] Provider: 00000000-0000-0000-0000-000000000000].
解决方案
bootstrap-vcpkg.bat硬编码调用powershell.exe,可通过以下方式解决:
方法1:创建powershell.exe到pwsh的符号链接
在执行bootstrap脚本前添加符号链接创建步骤:
RUN New-Item -ItemType SymbolicLink -Path "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Target "C:\Program Files\PowerShell\7\pwsh.exe"
方法2:修改bootstrap-vcpkg.bat脚本
解压后替换脚本中所有powershell.exe为pwsh:
RUN (Get-Content .\vcpkg\bootstrap-vcpkg.bat) -replace 'powershell.exe', 'pwsh' | Set-Content .\vcpkg\bootstrap-vcpkg.bat
方法3:直接使用PowerShell版本的bootstrap脚本
vcpkg提供bootstrap-vcpkg.ps1脚本,直接适配pwsh环境:
RUN .\vcpkg\bootstrap-vcpkg.ps1 -disableMetrics
推荐方案
优先选择方法3,无需修改文件或创建链接,是官方支持的简洁适配方式。
内容的提问来源于stack exchange,提问作者malat
相关产品推荐
相关产品推荐

