在Windows 10 Pro上创建.NET Core 3.1应用程序的Docker映像,并将其拉至Windows Server 2016

在Windows 10 Pro上创建.NET Core 3.1应用程序的Docker映像,并将其拉至Windows Server 2016,docker,.net-core,Docker,.net Core,我已经成功创建了包含.NET Core 3.1项目的Docker映像,并且正在本地Windows 10 Pro计算机上的Windows容器中运行它们。我使用的是Windows容器,不是Linux 我希望能够将它们拉到Windows 2016服务器机器(而不是VM)。我在Windows 2016服务器上安装了Docker EE,它正在运行。docker版本报告版本20.10.0 根据以下内容,Windows Server 2016没有任何受支持的.NET核心映像 如果这是真的,我是否在尝试做一些不

我已经成功创建了包含.NET Core 3.1项目的Docker映像,并且正在本地Windows 10 Pro计算机上的Windows容器中运行它们。我使用的是Windows容器,不是Linux

我希望能够将它们拉到Windows 2016服务器机器(而不是VM)。我在Windows 2016服务器上安装了Docker EE,它正在运行。docker版本报告版本20.10.0

根据以下内容,Windows Server 2016没有任何受支持的.NET核心映像

如果这是真的,我是否在尝试做一些不可能的事情?还是干脆不支持

我是新手,但据我所知.NET核心应用程序需要在主机上安装nanoserver 1809或更高版本。如果这是不正确的,让我知道

注意:我已成功将其拉入Windows 2016服务器:

docker pull mcr.microsoft.com/windows/nanoserver:sac2016
但是从我在这里读到的内容来看,.NETCore3.x在这个基础上不再受支持

第1期

在Windows 2016服务器上运行以下命令时,出现错误“清单列表项中没有与Windows/amd64 10.0.14393匹配的清单”

根据这一页,我应该能够得出:

第二期

从我读到的内容来看,我需要创建我的图像,或者至少在“hyperv”隔离模式下在容器中运行它们。对的或者docker主机本身可以运行不同版本的操作系统(比如nanoserver 1809或其他什么)?我正在使用VisualStudio在本地构建所有这些,同时也在使用docker compose,在这里我可以在yml文件中指定隔离模式。但当我通过VS.NET将我的图像发布到Docker Hub时,我不知道如何做到这一点。我的图像已成功发布,但当我尝试使用docker pull将其下载到Windows 2016服务器时,出现以下错误:

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

与上面的错误相关,我想我需要在我的映像中包含一个旧的Windows base版本?我已将以下内容添加到我的dockerfile中:

FROM mcr.microsoft.com/windows/nanoserver:1809
但它似乎什么也没做。在VS.NET中运行项目时,我在检查容器时看到:

OsVersion: 10.3.19042.867
我的概念验证目标只是:

  • 在我的Windows 10 Pro笔记本电脑上创建.NET Core 3.1 web api项目的映像
  • 将其拉到Windows 2016服务器并在Docker容器中运行
  • #我的笔记本电脑都能用。完成任务是我真正困惑的地方

    谢谢,布伦特

    更新

    下面是Dockerfile和我用来构建映像、在容器中运行映像和检查映像的命令。请注意,使用此Dockerfile,我现在可以在交互式命令提示符中看到:

    • 正确的操作系统版本:10.0.14393
    • 文件系统包含我的.Net解决方案、项目和源代码
    我刚刚将MattT提供的内容添加到添加DockerSupport时VS.NET生成的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 Server 2016主机上可以运行的Windows容器版本,您的选项仅限于Windows Server 2016容器。例如,这意味着您不能在Server2016主机上运行
    nanoserver:1809
    容器。如图所示:

    请注意,
    nanoserver:sac2016
    长期不受支持,因此它不是推荐的基本映像。2016年没有任何当前支持的Nano服务器标签。相反,建议使用WindowsServerCore。
    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"]
    

    谢谢@Matt。我将您的命令添加到我的docker文件中(同时添加此命令是为了避免TLS错误:[System.Net.ServicePointManager]::SecurityProtocol=[System.Net.SecurityProtocolType]::Tls12;),当我的docker构建成功时,检查映像时仍然显示操作系统版本为10.0.19042.867。我以为我的镜像会有一个服务器核心操作系统。。。我不是要你回答,因为我显然还有很多东西要学。你是说你正在构建的Dockerfile基于servercore:ltsc2016,但在检查容器时,你会看到它的版本是10.0.19042.867?您能否提供构建Dockerfile、运行容器和检索操作系统版本所执行的确切命令?非常感谢。我想我在上面发布的更新部分提供了您要求的内容。回到阅读“Windows上的Docker-第二版”。。。我在一周内学到了很多,但还不足以连接所有的点。Matt,通过使用您给我的dockerfile创建一个图像,并将其包含在我的.NET项目的dockerfile中(以FROM myimagename:tag为基础,注释FROM…aspnet:3.1为基础)我能够创建一个具有正确操作系统的映像,它还可以构建我的DotNet应用程序。但当我尝试运行它时,我发现:找不到Microsoft.AspNetCore.App版本“3.1.0”的框架。未找到任何框架。在交互式模式下运行容器时,dotnet--信息报告:.NET Core运行时已安装:Microsoft.NETCore.App 3.1.0[C:\Program Files\dotnet\shared\Microsoft.NE
    # 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"]