![](/img/trans.png)
[英]How to pull windows nano server docker image successfully in windows server 2016?
[英]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 都适用于我的笔记本电脑。 完成#2是我真正感到困惑的地方。
谢谢,布伦特
更新
下面是 Dockerfile 和我用来构建映像、在容器中运行并检查它的命令。 请注意,使用此 Dockerfile,我现在在交互式命令提示符中看到:
我刚刚采用了 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.