Partage via


Exemple avancé pour les conteneurs

L’échantillon de fichier Dockerfile décrit dans Installer Build Tools dans un conteneur utilise toujours l’image microsoft/dotnet-framework:4.8 basée sur la dernière image microsoft/windowsservercore et sur la dernière installation de Visual Studio Build Tools. Si vous publiez cette image dans un registre Docker pour que d’autres personnes puissent extraire, cette image peut convenir à de nombreux scénarios. Toutefois, dans la pratique, il est plus courant d’être spécifique sur l’image de base que vous utilisez, les fichiers binaires que vous téléchargez et les versions d’outils que vous installez.

L’exemple dockerfile suivant utilise une balise de version spécifique de l’image microsoft/dotnet-framework. L’utilisation d’une balise spécifique pour une image de base est courante et permet de se rappeler facilement que la création ou la reconstruction d’images a toujours la même base.

Remarque

Vous ne pouvez pas installer Visual Studio dans microsoft/windowsservercore :10.0.14393.1593 ou toute image basée sur celle-ci, qui présente des problèmes connus lors du lancement du programme d’installation dans un conteneur. Pour plus d’informations, consultez Problèmes connus pour les conteneurs.

L’exemple suivant télécharge la dernière version de Build Tools. Si vous souhaitez utiliser une version antérieure de Build Tools que vous pouvez installer dans un conteneur ultérieurement, vous devez d’abord créer et gérer une configuration.

Installer le script

Pour collecter des journaux d'activité quand une erreur d’installation se produit, créez un script de commandes par lots nommé « Install.cmd » dans le répertoire de travail et ayant le contenu suivant :

@if not defined _echo echo off
setlocal enabledelayedexpansion

call %*
if "%ERRORLEVEL%"=="3010" (
    exit /b 0
) else (
    if not "%ERRORLEVEL%"=="0" (
        set ERR=%ERRORLEVEL%
        call C:\TEMP\collect.exe -zip:C:\vslogs.zip

        exit /b !ERR!
    )
)

exit /b 0

Dockerfile

Dans le répertoire de travail, créez le fichier « Dockerfile » avec le contenu suivant :

# escape=`

# Use a specific tagged image. Tags can be changed, though that is unlikely for most images.
# You could also use the immutable tag @sha256:324e9ab7262331ebb16a4100d0fb1cfb804395a766e3bb1806c62989d1fc1326
ARG FROM_IMAGE=mcr.microsoft.com/windows/servercore:ltsc2019
FROM ${FROM_IMAGE}

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

# Copy our Install script.
COPY Install.cmd C:\TEMP\

# Download collect.exe in case of an install failure.
ADD https://aka.ms/vscollect.exe C:\TEMP\collect.exe

# Use the latest release channel. For more control, specify the location of an internal layout.
ARG CHANNEL_URL=https://aka.ms/vs/16/release/channel
ADD ${CHANNEL_URL} C:\TEMP\VisualStudio.chman

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.
    && (call C:\TEMP\Install.cmd vs_buildtools.exe --quiet --wait --norestart --nocache install `
        --installPath "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\BuildTools" `
        --channelUri C:\TEMP\VisualStudio.chman `
        --installChannelUri C:\TEMP\VisualStudio.chman `
        --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) `
    `
    # 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"]
# escape=`

# Use the latest Windows Server Core 2019 image.
ARG FROM_IMAGE=mcr.microsoft.com/windows/servercore:ltsc2019
FROM ${FROM_IMAGE}

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

# Copy our Install script.
COPY Install.cmd C:\TEMP\

# Download collect.exe in case of an install failure.
ADD https://aka.ms/vscollect.exe C:\TEMP\collect.exe

# Use the latest release channel. For more control, specify the location of an internal layout.
ARG CHANNEL_URL=https://aka.ms/vs/17/release/channel
ADD ${CHANNEL_URL} C:\TEMP\VisualStudio.chman

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.
    && (call C:\TEMP\Install.cmd vs_buildtools.exe --quiet --wait --norestart --nocache install `
        --installPath "%ProgramFiles(x86)%\Microsoft Visual Studio\2022\BuildTools" `
        --channelUri C:\TEMP\VisualStudio.chman `
        --installChannelUri C:\TEMP\VisualStudio.chman `
        --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) `
    `
    # 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"]

Exécutez la commande suivante pour générer l’image dans le répertoire de travail actuel :

docker build -t buildtools2019:16.0.28714.193 -t buildtools2019:latest -m 2GB .
docker build -t buildtools2022:17.0 -t buildtools2022:latest -m 2GB .

Si vous le souhaitez, passez les arguments FROM_IMAGE ou CHANNEL_URL, ou les deux, à l'aide du paramètre de ligne de commande --build-arg pour spécifier une image de base différente ou l'emplacement d’une disposition interne afin de maintenir une image fixe.

Conseil

Pour obtenir la liste des charges de travail et des composants, consultez le répertoire de composants Visual Studio Build Tools.

Diagnostic des échecs d’installation

Cet exemple télécharge des outils spécifiques et valide que les hachages correspondent. Il télécharge également l'utilitaire de collecte de journaux Visual Studio et .NET le plus récent afin que, si une défaillance d'installation se produit, vous puissiez ensuite copier les journaux sur votre ordinateur hôte pour analyser la défaillance.

> docker build -t buildtools2019:16.0.28714.193 -t buildtools2019:latest -m 2GB .
Sending build context to Docker daemon

...
Step 8/10 : RUN C:\TEMP\Install.cmd C:\TEMP\vs_buildtools.exe --quiet --wait --norestart --nocache ...
 ---> Running in 4b62b4ce3a3c
The command 'cmd /S /C C:\TEMP\Install.cmd C:\TEMP\vs_buildtools.exe ...' returned a non-zero code: 1603

> docker cp 4b62b4ce3a3c:C:\vslogs.zip "%TEMP%\vslogs.zip"
> docker build -t buildtools2022:17.0 -t buildtools2022:latest -m 2GB .
Sending build context to Docker daemon

...
Step 8/10 : RUN C:\TEMP\Install.cmd C:\TEMP\vs_buildtools.exe --quiet --wait --norestart --nocache ...
 ---> Running in 4b62b4ce3a3c
The command 'cmd /S /C C:\TEMP\Install.cmd C:\TEMP\vs_buildtools.exe ...' returned a non-zero code: 1603

> docker cp 4b62b4ce3a3c:C:\vslogs.zip "%TEMP%\vslogs.zip"

Une fois la dernière ligne terminée, ouvrez «%TEMP%\vslogs.zip» sur votre ordinateur ou soumettez un problème sur le site web de la communauté des développeurs .

Support ou résolution des problèmes

Parfois, les choses peuvent mal tourner. Si votre installation de Visual Studio échoue, consultez résoudre les problèmes d’installation et de mise à niveau de Visual Studio pour obtenir des instructions pas à pas.

Voici quelques options de support supplémentaires :