Sample to install build tools into a container dont install

Philippe Villeneuve 1 Reputation point
2022-01-17T22:16:09.787+00:00

Hi,

When building a docker image, installing the 2019 Visual Studio build tools doesnt work for me. When I run the resulting image, the directory C:\Program Files (x86)\Microsoft Visual Studio\2019 doesn't exist. On the other hand, from a running container, I'm able to perform the installation. I'm following Microsoft's instructions available at
https://learn.microsoft.com/en-us/visualstudio/install/build-tools-container?view=vs-2019 . (See below for a copy of the Dockerfile)

When running inside a container, I've noticed that even with the --wait flag, the command vs_buildtools.exe returns before the installation completes. Using tasklist, one can observe that many setup process need to complete for the installation to be done. Also, the command vs_buildtools.exe might need to be ran more than once if the installer needs to be updated before I can observe an installation.

I was able to make progress with the following strategy. When launching vs_buildtools, dont only rely on the --wait flag. Instead, consult the process list until it is no longer present. Due to race condition, again with the process list, wait for that process to appear before waiting for it to complete. Once the process vs_buildtools has completed, wait on all the setup process to complete. Doing this twice has been required for me to clear all required updates. On the third invocation, I'm able to install the desired workload, "Desktop development with C++" (Microsoft.VisualStudio.Workload.NativeDesktop) while indicating to no longer update the installer with --includeRecommended. At this point, the 2019 Visual Studio build tools are installed in the image. This can quickly be validated by checking that 'C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\' exists.

The following Dockerfile has worked for me:

# escape=`  

# Use the latest Windows Server Core image with .NET Framework 4.8.  
FROM mcr.microsoft.com/dotnet/framework/sdk:4.8-windowsservercore-ltsc2019  

SHELL ["powershell", "-command"]  

RUN Invoke-WebRequest "https://aka.ms/vs/16/release/vs_buildtools.exe" -OutFile "$env:TEMP\vs_buildtools.exe" -UseBasicParsing  

# Works  
RUN Write-Host "Update the installer" ; `  
    & "$env:TEMP\vs_buildtools.exe" --quiet --wait --norestart --installPath 'C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools' ; `  
    while((get-process "vs_buildtools" -ea SilentlyContinue) -eq $Null) {Write-Host "no vs_buildtools"; Start-Sleep -Seconds 1;} ; `  
    while((get-process "vs_buildtools" -ea SilentlyContinue) -ne $Null) {Write-Host "waiting for vs_buildtools"; wait-process vs_buildtools 2>$null;} ; `  
    while((get-process "setup" -ea SilentlyContinue) -ne $Null) {Write-Host "waiting for setup"; wait-process setup 2>$null;} ; `  
    `  
    Write-Host "Basic install of VS BuildTools 2019. Adding workload here mostly fails." ; `  
    & "$env:TEMP\vs_buildtools.exe" --quiet --wait --norestart --installPath 'C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools' ; `  
    while((get-process "vs_buildtools" -ea SilentlyContinue) -eq $Null) {Write-Host "no vs_buildtools"; Start-Sleep -Seconds 1;} ; `  
    while((get-process "vs_buildtools" -ea SilentlyContinue) -ne $Null) {Write-Host "waiting for vs_buildtools"; wait-process vs_buildtools 2>$null;} ; `  
    while((get-process "setup" -ea SilentlyContinue) -ne $Null) {Write-Host "waiting for setup"; wait-process setup 2>$null;} ; `  
    `  
    Write-Host "Workload installation." ; `  
    & "$env:TEMP\vs_buildtools.exe" --quiet --wait --norestart --installPath 'C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools'   --includeRecommended --add Microsoft.VisualStudio.Workload.NativeDesktop --add Microsoft.VisualStudio.Component.VC.ATLMFC ; `  
    while((get-process "vs_buildtools" -ea SilentlyContinue) -eq $Null) {Write-Host "no vs_buildtools"; Start-Sleep -Seconds 1} ; `  
    while((get-process "vs_buildtools" -ea SilentlyContinue) -ne $Null) {Write-Host "waiting for vs_buildtools"; wait-process vs_buildtools 2>$null;} ; `  
    while((get-process "vs_setup_bootstrapper" -ea SilentlyContinue) -ne $Null) {Write-Host "waiting for vs_setup_bootstrapper"; wait-process vs_setup_bootstrapper 2>$null;} ; `  
    while((get-process "setup" -ea SilentlyContinue) -ne $Null) {Write-Host "waiting for setup"; wait-process setup 2>$null;} ; `  
    Write-Host "Installation should be complete!" ;  


# Define the entry point for the docker container.  
SHELL ["cmd", "/S", "/C"]  

# This entry point starts the developer command prompt and launches the PowerShell shell.  
ENTRYPOINT ["C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\Common7\\Tools\\VsDevCmd.bat", "&&", "powershell.exe", "-NoLogo", "-ExecutionPolicy", "Bypass"]  

With more time on my hands, I'd use Start-Process to launch vs_buildtools and capture a reference to it. This should remove the need to wait for the process to start and simplify waiting for it to complete. That is left as an exercise to the reader :)

Hopefully, some improvements can be made to the sample Dockerfile you are providing (https://learn.microsoft.com/en-us/visualstudio/install/build-tools-container?view=vs-2019) to build working docker images.

Thanks

The content of the Dockerfile, as provided in the article

# escape=`  

# Use the latest Windows Server Core image with .NET Framework 4.8.  
FROM mcr.microsoft.com/dotnet/framework/sdk:4.8-windowsservercore-ltsc2019  

# Restore the default Windows shell for correct batch processing.  
SHELL ["cmd", "/S", "/C"]  

RUN `  
    # Download the Build Tools bootstrapper.  
    curl -SL --output vs_buildtools.exe https://aka.ms/vs/16/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 modify `  
        --installPath "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\BuildTools" `  
        --add Microsoft.VisualStudio.Workload.AzureBuildTools `  
        --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\\2019\\BuildTools\\Common7\\Tools\\VsDevCmd.bat", "&&", "powershell.exe", "-NoLogo", "-ExecutionPolicy", "Bypass"]  
Visual Studio Setup
Visual Studio Setup
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Setup: The procedures involved in preparing a software program or application to operate within a computer or mobile device.
982 questions
{count} votes

1 answer

Sort by: Most helpful
  1. toast12 1 Reputation point
    2022-01-21T00:01:46.75+00:00

    @Philippe Villeneuve How long should we expect your script to run? I have the same problem as you and cant install VS 2019 buildtools using the dockerfile provided by microsoft. Trying your script but its taking a long time and I don't see it finishing (after about 15 mins now)