Docker环境添加XamarinBuildTools workload致VS Build Tools 2022安装失败
在Docker中搭建.NET MAUI Jenkins构建环境时VS Build Tools安装失败的问题
我正在为.NET MAUI的Jenkins持续构建环境搭建Docker镜像,遇到了VS Build Tools安装的核心问题:直接安装MAUI相关workload会导致安装彻底失败。按照微软文档示例添加--add Microsoft.VisualStudio.Workload.XamarinBuildTools参数后,C:\Program Files (x86)\Microsoft Visual Studio目录下根本不会生成2022文件夹,安装失败;但如果不加这个参数,VS Build Tools就能正常安装。
已尝试的操作
- 在已安装完整Visual Studio(非Build Tools)的开发机上执行相同安装步骤,能正常运行(未在干净环境测试)
- 更换过不同的Windows Server Core基础镜像
- 切换到ContainerAdministrator权限执行安装
- 使用过不同来源的vs_buildtools.exe(其中一个在装有VS 2017/2019的镜像上能成功安装,但无法构建.NET MAUI项目,提示找不到MAUI框架)
初步猜测
我怀疑可能是本地已安装Visual Studio导致干净Docker环境中的全新安装出现冲突,但不确定具体原因。
当前可行的解决方法
经过多场景测试,发现不直接安装workload,而是逐个安装workload包含的独立组件可以解决问题。以下是前后两个Dockerfile对比:
原失败Dockerfile
# escape=` # Use the latest Windows Server Core 2019 image. FROM mcr.microsoft.com/windows/servercore:ltsc2019 # Restore the default Windows shell for correct batch processing. SHELL ["cmd", "/S", "/C"] USER ContainerAdministrator RUN ` # Download the Build Tools bootstrapper. curl -SL --output vs_buildtools.exe https://aka.ms/vs/17/release/vs_buildtools.exe ` ` # Install Build Tools with the Microsoft.VisualStudio.Workload.AzureBuildTools workload, excluding workloads and components with known issues. && (start /w vs_buildtools.exe --quiet --wait --norestart --nocache ` --installPath "%ProgramFiles(x86)%\Microsoft Visual Studio\2022\BuildTools" ` --add Microsoft.VisualStudio.Workload.AzureBuildTools ` --add Microsoft.VisualStudio.Workload.XamarinBuildTools ` --remove Microsoft.VisualStudio.Component.Windows10SDK.10240 ` --remove Microsoft.VisualStudio.Component.Windows10SDK.10586 ` --remove Microsoft.VisualStudio.Component.Windows10SDK.14393 ` --remove Microsoft.VisualStudio.Component.Windows81SDK ` || IF "%ERRORLEVEL%"=="3010" EXIT 0) ` ` # Cleanup && del /q vs_buildtools.exe # Define the entry point for the docker container. # This entry point starts the developer command prompt and launches the PowerShell shell. # ENTRYPOINT ["C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\Common7\\Tools\\VsDevCmd.bat", "&&", "powershell.exe", "-NoLogo", "-ExecutionPolicy", "Bypass"] RUN mkdir c:\working WORKDIR C:\working
修改后可行的Dockerfile
# escape=` # Use the latest Windows Server Core 2019 image. FROM mcr.microsoft.com/windows/servercore:ltsc2019 # Restore the default Windows shell for correct batch processing. SHELL ["cmd", "/S", "/C"] USER ContainerAdministrator RUN ` # Download the Build Tools bootstrapper. curl -SL --output vs_buildtools.exe https://download.visualstudio.microsoft.com/download/pr/5c9aef4f-a79b-4b72-b379-14273860b285/bd2dd3a59d2553382f89712d19e4d5c3d930d9a41c9426cf8194dd5a3a75875f/vs_BuildTools.exe ` ` # Install Build Tools with the Microsoft.VisualStudio.Workload.AzureBuildTools workload, excluding workloads and components with known issues. && start /w vs_buildtools.exe --quiet --wait --norestart --nocache ` --installPath "%ProgramFiles(x86)%\Microsoft Visual Studio\2022\BuildTools" ` --add android ` --add Component.Android.SDK.MAUI ` --add Component.OpenJDK ` --add ios ` --add maccatalyst ` --add maui.android ` --add maui.blazor ` --add maui.core ` --add maui.ios ` --add maui.maccatalyst ` --add maui.windows ` --add Microsoft.Component.MSBuild ` --add Microsoft.Component.NetFX.Native ` --add Microsoft.Net.Component.4.6.1.TargetingPack ` --add Microsoft.Net.Component.4.8.SDK ` --add microsoft.net.runtime.android ` --add microsoft.net.runtime.android.aot ` --add microsoft.net.runtime.android.aot.net6 ` --add microsoft.net.runtime.android.net6 ` --add microsoft.net.runtime.ios ` --add microsoft.net.runtime.ios.net6 ` --add microsoft.net.runtime.maccatalyst ` --add microsoft.net.runtime.maccatalyst.net6 ` --add microsoft.net.runtime.mono.tooling ` --add microsoft.net.runtime.mono.tooling.net6 ` --add Microsoft.NetCore.Component.Runtime.6.0 ` --add Microsoft.NetCore.Component.Runtime.7.0 ` --add Microsoft.NetCore.Component.SDK ` --add Microsoft.VisualStudio.Component.NuGet.BuildTools ` --add Microsoft.VisualStudio.Component.Roslyn.Compiler ` --add Microsoft.VisualStudio.ComponentGroup.UWP.BuildTools ` --add runtimes.ios ` --add runtimes.ios.net6 ` --add runtimes.maccatalyst ` --add runtimes.maccatalyst.net6 ` # Cleanup && del /q vs_buildtools.exe # Define the entry point for the docker container. # This entry point starts the developer command prompt and launches the PowerShell shell. # ENTRYPOINT ["C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\Common7\\Tools\\VsDevCmd.bat", "&&", "powershell.exe", "-NoLogo", "-ExecutionPolicy", "Bypass"] RUN mkdir c:\working WORKDIR C:\working
希望能得到更多关于这个问题的排查思路或替代解决方案,比如是否有更简洁的安装方式,或者能直接修复workload安装失败的方法。
内容的提问来源于stack exchange,提问作者Simas Paškauskas
相关产品推荐
相关产品推荐

