从VS2019运行Docker容器的问题

时间:2019-10-30 16:58:56

标签: visual-studio docker .net-core dockerfile server-core

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

我正在尝试在Docker容器中运行此应用程序,但在使用完整的Windows server core图像时,努力通过Visual Studio 2019使它运行。据我了解,这是唯一需要GDI +库的图像。

当使用servercore:1803时,通过Visual Studio运行时收到此错误:“无法启动程序'C:\ Program Files \ dotnet \ dotnet.exe。系统找不到指定的路径。

Error message received from Visual Studio 2019 基于Container Tools和Build的日志输出,看来一切都按预期进行。

这是我完整的dockerfile。当您选择“添加-> Docker支持...”时,这正是Visual Studio添加的内容,所使用的图像除外。请注意,当我使用 3.0-nanoserver-1803 时,Visual Studio会按预期运行容器,但是在执行需要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新手,所以希望我只是想念一些简单的东西。

1 个答案:

答案 0 :(得分:2)

这是因为您已将.NET Core运行时和SDK基本映像更改为Windows Server Core映像:

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

服务器核心映像中未安装.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