从 VS2019 发出 运行 Docker 容器

Issue running Docker container from VS2019

我构建了一个 .Net Core 3.0 控制台应用程序,它使用 Telerik 生成 pdf 报告。 Telerik 利用 GDI+ 库来执行此操作。该应用程序使用内部开发的 dll,该 dll 依赖于 Windows 事件日志,因此不幸的是,目前无法在 Linux 上托管。

我正尝试在 docker 容器中 运行 这个应用程序,但在使用完整的 Windows server core 图像时很难通过 Visual Studio 2019 让它工作。据我了解,这是唯一具有我需要的 GDI+ 库的图像。

当使用 servercore:1803 时,我在 运行ning 通过 Visual Studio 时收到此错误:"Unable to start program 'C:\Program Files\dotnet\dotnet.exe'. The system cannot find the path specified."

根据 Container Tools 和 Build 的日志输出,似乎一切都按预期进行。

这是我的完整 docker 文件。这正是 Visual Studio 在您 select "Add --> Docker Support..." 时添加的内容,但使用的图像除外。请注意,当我按预期使用 3.0-nanoserver-1803 Visual Studio 运行s 容器但在执行需要 GDI 的报告生成代码时失败。

FROM mcr.microsoft.com/windows/servercore:1803 AS base
#FROM mcr.microsoft.com/dotnet/core/runtime:3.0-nanoserver-1803 AS base
WORKDIR /app   

FROM mcr.microsoft.com/windows/servercore:1803 AS build
#FROM mcr.microsoft.com/dotnet/core/sdk:3.0-nanoserver-1803 AS build
WORKDIR /src
COPY ["DM.Web.Reporting.Background/DM.Web.Reporting.Background.csproj", "DM.Web.Reporting.Background/"]
RUN dotnet restore "DM.Web.Reporting.Background/DM.Web.Reporting.Background.csproj"
COPY . .
WORKDIR "/src/DM.Web.Reporting.Background"
RUN dotnet build "DM.Web.Reporting.Background.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "DM.Web.Reporting.Background.csproj" -c Release -o /app/publish

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

我是不是做错了什么?我是一个 Docker 菜鸟所以希望我只是错过了一些简单的东西。

这是因为您已将 .NET Core 运行时和 SDK 基础映像更改为 Windows 服务器核心映像:

FROM mcr.microsoft.com/windows/servercore:1803 AS build
#FROM mcr.microsoft.com/dotnet/core/sdk:3.0-nanoserver-1803 AS build

Server Core 映像中没有安装 .NET Core。您需要将其作为 Dockerfile 的一部分进行安装。

我修改了你的 Dockerfile 以在运行时阶段安装 .NET Core 运行时,在 SDK 阶段安装 SDK:

FROM mcr.microsoft.com/windows/servercore:1803 AS base
#FROM mcr.microsoft.com/dotnet/core/runtime:3.0-nanoserver-1803 AS base

# Install .NET Core Runtime
RUN powershell -Command \
        $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.0 \
            -Runtime dotnet; \
        Remove-Item -Force dotnet-install.ps1 \
    && setx /M PATH "%PATH%;C:\Program Files\dotnet"

WORKDIR /app   

FROM mcr.microsoft.com/windows/servercore:1803 AS build
#FROM mcr.microsoft.com/dotnet/core/sdk:3.0-nanoserver-1803 AS build

# Install .NET Core SDK
RUN powershell -Command \
        $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.0; \
        Remove-Item -Force dotnet-install.ps1 \
    && setx /M PATH "%PATH%;C:\Program Files\dotnet"

WORKDIR /src
COPY ["DM.Web.Reporting.Background/DM.Web.Reporting.Background.csproj", "DM.Web.Reporting.Background/"]
RUN dotnet restore "DM.Web.Reporting.Background/DM.Web.Reporting.Background.csproj"
COPY . .
WORKDIR "/src/DM.Web.Reporting.Background"
RUN dotnet build "DM.Web.Reporting.Background.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "DM.Web.Reporting.Background.csproj" -c Release -o /app/publish

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

有关如何使用 dotnet-install 脚本的更多详细信息,请参阅 https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-install-script

更新:

有关如何在 Dockerfile(Linux 或 Windows)中安装 .NET Core 的更多通用说明,请参阅 https://github.com/dotnet/dotnet-docker/blob/master/samples/snippets/installing-dotnet.md