dockerpowershellinstallationvisual-studio-2022windows-container

How to install VS BuildTools via Dockerfile with PowerShell?


I have this Windows container Dockerfile:

FROM mcr.microsoft.com/windows/servercore:ltsc2022

USER ContainerAdministrator
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'Continue'; $verbosePreference='Continue';"]

RUN Invoke-WebRequest -OutFile 'c:\install-powershell.ps1' 'https://aka.ms/install-powershell.ps1'; \
    &'c:\install-powershell.ps1' -UseMSI; \
    Remove-Item -LiteralPath 'c:\install-powershell.ps1'

RUN Invoke-WebRequest -OutFile 'c:\vs_buildtools.exe' 'https://download.visualstudio.microsoft.com/download/pr/f73d49f7-22b6-4a11-b980-72f3daae77a6/e258e16d0e663bcd43db4979704b913bec3d19c052d09ea3013803a6913f28ab/vs_BuildTools.exe'; \
    Start-Process -FilePath 'c:\vs_buildtools.exe' -ArgumentList '--wait','--quiet','--norestart','--nocache','--installPath','C:\Program Files\Microsoft\Visual Studio\2022\BuildTools','--add','Microsoft.Component.MSBuild','--add','Microsoft.VisualStudio.Component.VC.Tools.x86.x64','--add','Microsoft.VisualStudio.Component.VC.ATL','--add','Microsoft.VisualStudio.Component.Windows11SDK.22621','--add','Microsoft.Net.ComponentGroup.4.8.DeveloperTools','--add','Microsoft.VisualStudio.Component.VC.CMake.Project' -NoNewWindow -Wait; \
    Remove-Item -LiteralPath 'c:\vs_buildtools.exe'

RUN ls 'C:\Program Files\Microsoft\Visual Studio\2022\BuildTools'

ENTRYPOINT [ \
    "C:\\Program Files\\PowerShell\\7\\pwsh.exe", "-NoLogo", "-ExecutionPolicy", "Bypass", "-NoExit", "-c", \
    "&{Import-Module 'C:\\Program Files\\Microsoft\\Visual Studio\\2022\\BuildTools\\Common7\\Tools\\Microsoft.VisualStudio.DevShell.dll'; Enter-VsDevShell -VsInstallPath 'C:\\Program Files\\Microsoft Visual Studio\\2022\\BuildTools' -SkipAutomaticLocation -DevCmdArguments '-arch=x64 -host_arch=x64'}"]

Save Dockerfile to disk, go to its directory and run docker build ..

Installing pwsh works. Installing Visual Studio BuildTools looks like it works, but when I look at the install location it isn't there.

Step 8/9 : RUN ls 'C:\Program Files\Microsoft\Visual Studio\2022\BuildTools'
 ---> Running in 9514712729e3
ls : Cannot find path 'C:\Program Files\Microsoft\Visual
Studio\2022\BuildTools' because it does not exist.
At line:1 char:99
+ ... 'Continue'; ls 'C:\Program Files\Microsoft\Visual Studio\2022\BuildTo ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Program File...2022\BuildToo
   ls:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCh
   ildItemCommand

Is there something wrong with my Docker or Powershell syntax?


Solution

  • You don't need to use Powershell to install Visual Studio Build Tools.

    Try something like:

    RUN `
        # Install VS Build Tools
        && curl -fSLo vs_BuildTools.exe https://aka.ms/vs/17/release/vs_BuildTools.exe `
        && start /w vs_BuildTools ^ `
            --installPath "%ProgramFiles(x86)%\Microsoft Visual Studio\2022\BuildTools" ^ `
            --add Microsoft.Component.ClickOnce.MSBuild ^ `
            --add Microsoft.Net.Component.4.8.1.SDK ^ `
            --add Microsoft.NetCore.Component.Runtime.6.0 ^ `
            --add Microsoft.NetCore.Component.Runtime.8.0 ^ `
            --add Microsoft.NetCore.Component.SDK ^ `
            --add Microsoft.VisualStudio.Component.NuGet.BuildTools ^ `
            --add Microsoft.VisualStudio.Component.WebDeploy ^ `
            --add Microsoft.VisualStudio.Web.BuildTools.ComponentGroup ^ `
            --add Microsoft.VisualStudio.Workload.MSBuildTools ^ `
            --quiet --norestart --nocache --wait `
        && powershell -Command "if ($err = dir $Env:TEMP -Filter dd_setup_*_errors.log | where Length -gt 0 | Get-Content) { throw $err }" `
        && del vs_BuildTools.exe
    

    The above sample is from the official .NET Framework 4.8 SDK Dockerfile.

    Please refer to the Visual Studio Build Tools component directory for a complete list of the components/workloads that you can use to install Visual Studio from the command line.