在构建过程中重新启动 Docker 个容器

Restart Docker container during build process

由于 dotnetfx,我需要在构建过程中重新启动 Docker 容器。 Dockerfile:

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

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

# Install Chocolatey
RUN Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

# Universal Windows Platform build tools workload for Visual Studio 2019 Build Tools (https://chocolatey.org/packages/visualstudio2019-workload-universalbuildtools#dependencies)
RUN choco install visualstudio2019-workload-universalbuildtools --package-parameters "--includeOptional" --confirm

但是我遇到了这个错误:

Packages requiring reboot:
 - dotnetfx (exit code 3010) # <--- it means a reboot is needed!

我尝试在同一个 RUN 中 运行 两个命令并在它们之间添加 Restart-Computer(由 \ 分隔)并执行 RUN 命令在每个安装命令之后,但当我这样做时,它看起来像 Docker 输出丢失。

在构建过程中是否可以重启当前容器而不使Docker迷路并保持安装过程?


更新 1

试图在 运行 最后一个命令之前安装这个 dotnetfx 但我得到同样的错误。

# Microsoft .NET Framework (https://chocolatey.org/packages/dotnetfx)
RUN choco install dotnetfx --confirm

错误:

Packages requiring reboot:
 - dotnetfx (exit code 3010)

更新 2(解决方法)

我已经使用已安装 .NET 的基础映像解决了这个问题:

FROM mcr.microsoft.com/dotnet/framework/sdk:4.8

解决此问题的一种方法是使用多阶段构建。在第一阶段,您可以安装二进制文件,在第二阶段,您复制二进制文件并构建 docker 映像。

以下是您可以执行此操作的方法:https://docs.docker.com/develop/develop-images/multistage-build/

示例

FROM mcr.microsoft.com/windows/servercore:ltsc2019 as baseimage

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

# Install Chocolatey
RUN Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

# Universal Windows Platform build tools workload for Visual Studio 2019 Build Tools (https://chocolatey.org/packages/visualstudio2019-workload-universalbuildtools#dependencies)
FROM baseimage
RUN choco install visualstudio2019-workload-universalbuildtools --package-parameters "--includeOptional" --confirm

好的,看起来你正在尝试安装 VisualStudio 2019。这就是我解决问题的方法。 第一种方法是使用如上所述的多阶段构建:

FROM mcr.microsoft.com/windows/servercore:1809 as baseimage
RUN powershell -NoProfile -ExecutionPolicy Bypass -Command \    
     $Env:chocolateyVersion = '0.10.15' ; \
     $Env:chocolateyUseWindowsCompression = 'false' ; \
     "[Net.ServicePointManager]::SecurityProtocol = \"tls12, tls11, tls\"; iex ((New-Object System.Net.WebClient).DownloadString('http://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

# suppress the "restart required" error code (3010)
RUN choco install -y --ignore-package-exit-codes=3010 dotnetfx

# emulate the required restart after installing dotnetfx
FROM baseimage
RUN choco install -y visualstudio2019buildtools --package-parameters \
    "--norestart --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64"

该方法的问题是 dotnetfx 包似乎已损坏 - 由于缺少 'alink.dll' 库,其他一些包无法安装。另外,我没有检查 --ignore-package-exit-codes=3010 是否只抑制一个错误或所有错误(choco doc 没有说明指定确切代码的可能性)。

第二种方法是从 MS 网站安装 visual studio(完美运行):

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

RUN powershell -NoProfile -ExecutionPolicy Bypass -Command \
    Invoke-WebRequest "https://aka.ms/vs/16/release/vs_community.exe" \
    -OutFile "%TEMP%\vs_community.exe" -UseBasicParsing

RUN "%TEMP%\vs_community.exe"  --quiet --wait --norestart --noUpdateInstaller \
    --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 \
    --add Microsoft.VisualStudio.Component.Windows10SDK.18362

请注意,组件可能与您的情况有所不同。

docker不需要重启。唯一需要做的就是告诉 choco 不要以非零退出代码退出。这是由 --ignore-package-exit-codes=3010.

完成的

以下对我有用:

FROM mcr.microsoft.com/windows/servercore:1809-amd64

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN ["powershell","Set-ExecutionPolicy Bypass -Scope Process -Force;","iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"]
RUN choco install dotnetfx -y --version 4.8.0.20190930 --ignore-package-exit-codes=3010
# see https://docs.microsoft.com/en-us/visualstudio/install/workload-component-id-vs-build-tools?vs-2019&view=vs-2019 for a list of available workloads and components
RUN choco install visualstudio2019buildtools -y --version 16.8.1.0 --params \"--add Microsoft.VisualStudio.Workload.ManagedDesktopBuildTools --add Microsoft.VisualStudio.Workload.NetCoreBuildTools\" 
RUN setx /M PATH $($Env:PATH+';C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/MSBuild/Current/bin/;c:/Program Files (x86)/Microsoft SDKs/Windows/v10.0A/bin/NETFX 4.8.0 Tools/')