簡體   English   中英

在 Windows 10 Pro 上創建 .NET Core 3.1 應用程序的 Docker 映像並將其拉到 ZAEA23489CE3AA9B6406EBB201

[英]Create Docker Image of .NET Core 3.1 app on Windows 10 Pro and pull it to Windows Server 2016

我已經成功創建了包含 .NET Core 3.1 項目的 Docker 映像,並在我的本地 Windows 容器中運行它們。 我使用的是 Windows 容器,而不是 Linux。

我希望能夠將它們拉到 Windows 2016 服務器機器(不是 VM)。 我在 Windows 2016 服務器上安裝了 Docker EE,它正在運行。 docker 版本報告版本 20.10.0。

根據以下內容,沒有任何受支持的 .NET Core 映像可用於 Windows Server 2016。 沒有適用於 windows/amd64 Server 2016 的匹配清單

如果這是真的,我是否正在嘗試做一些不可能的事情? 還是根本不支持?

我是新手,但據我了解 .NET 核心應用程序需要在主機中安裝 nanoserver 1809 或更高版本。 如果這不正確,請告訴我...

注意:我已成功將其拉到 Windows 2016 服務器:

docker pull mcr.microsoft.com/windows/nanoserver:sac2016

但從我在這里讀到的內容來看,該基礎不再支持https://github.com/dotnet/dotnet-docker/issues/1041、.NET Core 3.x。

問題 1

當我在 Windows 2016 服務器上運行以下命令時,我收到錯誤“清單列表條目中沒有 windows/amd64 10.0.14393 的匹配清單”。

docker pull mcr.microsoft.com/windows/nanoserver:1809

根據此頁面,我應該能夠得出: https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/deploy-containers-on-server

問題 2

從我讀到的內容看來,我需要創建我的圖像,或者至少在容器中以“hyperv”隔離模式運行它們。 正確的? 還是 docker 主機本身可以運行不同版本的操作系統(如 nanoserver 1809 或其他)? 我正在使用 Visual Studio 在本地構建所有這些,並且還使用 docker-compose 可以在 yml 文件中指定隔離模式。 但是當我通過 VS.NET 將圖像發布到我的 Docker 集線器時,我不知道該怎么做。 我的圖像發布成功,但是當我嘗試使用 docker pull 將它們降低到 Windows 2016 服務器時,我收到此錯誤:

a Windows version 10.0.19042-based image is incompatible with a 10.0.14393 host

問題 3

與上述錯誤相關,我想我需要在我的圖像中包含一個較舊的 Windows 基本版本? 我在 dockerfile 中添加了以下內容:

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

但它似乎沒有做任何事情。 當我在 VS.NET 中運行項目時,我在檢查容器時看到了這一點:

OsVersion: 10.3.19042.867

我的概念證明目標很簡單:

  1. 在我的 ZAEA23489CE3AAA9B6406EBB28E0 筆記本電腦上創建 .NET Core 3.1 web api 項目的映像
  2. 將其拉到 Windows 2016 服務器並在 Docker 容器中運行。

#1 都適用於我的筆記本電腦。 完成#2是我真正感到困惑的地方。

謝謝,布倫特

更新

下面是 Dockerfile 和我用來構建映像、在容器中運行並檢查它的命令。 請注意,使用此 Dockerfile,我現在在交互式命令提示符中看到:

  • 正確的操作系統版本:10.0.14393
  • 文件系統確實包含 my.Net 解決方案、項目和我的源代碼。

我剛剛采用了 MattT 提供的內容並將其添加到 VS.NET 在添加 DockerSupport 時生成的 Dockerfile 的開頭。 我知道我搞砸了我的 Dockerfile,因為它沒有構建/發布或運行我的 webapi 項目(就像在添加新命令之前所做的那樣)。 我認為這與我的 FROM 語句有關。 我讀了很多,但還不明白分層是如何工作的,等等。

# escape=`
FROM mcr.microsoft.com/windows/servercore:ltsc2016 AS baseOS
RUN powershell -Command [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12; $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest -UseBasicParsing -Uri https://dot.net/v1/dotnet-install.ps1 -OutFile dotnet-install.ps1; ./dotnet-install.ps1 -InstallDir '/Program Files/dotnet' -Channel 3.1 -Runtime dotnet; Remove-Item -Force dotnet-install.ps1 && setx /M PATH "%PATH%;C:\Program Files\dotnet"
COPY . .

FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base

WORKDIR /app
EXPOSE 80
EXPOSE 8001

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["WebApi.csproj", "."]

RUN dotnet restore "WebApi.csproj"
COPY . .

WORKDIR "/src/"
RUN dotnet build "WebApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApi.csproj" -c Release -o /app/publish

FROM base AS final
#FROM baseOS AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApi.dll"]

我運行的 docker 命令:

docker build -f "C:\Cabinet\Learning\EssApiProjects\src\Services\WebApi\Dockerfile" --force-rm -t webapi:ltsc2016 --target baseOS "C:\Cabinet\Learning\EssApiProjects\src\Services\WebApi"

docker run --env ASPNETCORE_ENVIRONMENT=Development -p 8001:80 --name webapi_ltsc2016 -it webapi:ltsc2016

docker exec -it webapi_ltsc2016 cmd.exe 

支持的內容和可用的內容之間存在差異。 支持在 Windows Server 2016 上的容器中運行 .NET Core,但沒有可用的映像。 你可以定義你自己的。

您對於 Windows 容器版本可以在 Windows Server 2016 主機上運行的選項僅限於 Windows Server 2016 容器。 例如,這意味着您不能在 Server 2016 主機上運行nanoserver:1809容器。 這在Windows 容器版本兼容性矩陣中進行了說明:

在此處輸入圖像描述

請注意, nanoserver:sac2016早已不受支持,因此它不是推薦的基礎映像。 Nano Server 2016 目前沒有任何支持的標簽。相反,建議使用 Windows Server Core。 servercore:ltsc2016是受支持標簽的最佳選擇。

以下是如何在服務器核心映像上安裝 .NET 核心的示例:

# escape=`

FROM mcr.microsoft.com/windows/servercore:ltsc2016
RUN powershell -Command `
        $ErrorActionPreference = 'Stop'; `
        $ProgressPreference = 'SilentlyContinue'; `
        [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
        Invoke-WebRequest `
            -UseBasicParsing `
            -Uri https://dot.net/v1/dotnet-install.ps1 `
            -OutFile dotnet-install.ps1; `
        ./dotnet-install.ps1 `
            -InstallDir '/Program Files/dotnet' `
            -Channel 3.1 `
            -Runtime aspnetcore; `
        Remove-Item -Force dotnet-install.ps1 `
    && setx /M PATH "%PATH%;C:\Program Files\dotnet"

更多資源:

更新

下面是如何使用 Visual Studio 生成的 Dockerfile 定義的示例:

# escape=`
FROM mcr.microsoft.com/windows/servercore:ltsc2016 AS base

# Install ASP.NET Core 3.1
RUN powershell -Command `
        $ErrorActionPreference = 'Stop'; `
        $ProgressPreference = 'SilentlyContinue'; `
        [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
        Invoke-WebRequest `
            -UseBasicParsing `
            -Uri https://dot.net/v1/dotnet-install.ps1 `
            -OutFile dotnet-install.ps1; `
        ./dotnet-install.ps1 `
            -InstallDir '/Program Files/dotnet' `
            -Channel 3.1 `
            -Runtime aspnetcore; `
        Remove-Item -Force dotnet-install.ps1 `
    && setx /M PATH "%PATH%;C:\Program Files\dotnet"

WORKDIR /app
EXPOSE 80
EXPOSE 8001

FROM mcr.microsoft.com/windows/servercore:ltsc2016 AS build

# Install .NET Core 3.1 SDK
RUN powershell -Command `
        $ErrorActionPreference = 'Stop'; `
        $ProgressPreference = 'SilentlyContinue'; `
        [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
        Invoke-WebRequest `
            -UseBasicParsing `
            -Uri https://dot.net/v1/dotnet-install.ps1 `
            -OutFile dotnet-install.ps1; `
        ./dotnet-install.ps1 `
            -InstallDir '/Program Files/dotnet' `
            -Channel 3.1; `
        Remove-Item -Force dotnet-install.ps1 `
    && setx /M PATH "%PATH%;C:\Program Files\dotnet"

WORKDIR /src
COPY ["WebApi.csproj", "."]

RUN dotnet restore "WebApi.csproj"
COPY . .

WORKDIR "/src/"
RUN dotnet build "WebApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApi.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApi.dll"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM