.NET核心控制台应用程序不会在基于Alpine的docker映像中启动



几个月前,我为控制台应用程序创建了第一个Dockerfile。这个控制台应用程序依赖于其他几个c#项目,所以它比你通常在互联网上找到的标准示例要复杂一些。Dockerfile;作品";,即,我可以创建图像并运行容器。然而,我知道它包含了几个缺陷,现在是时候改进这个Dockerfile了。当前版本为

FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY Directory.Build.props .
COPY src/ .
RUN dotnet restore "MyProject/MyProject.csproj"
COPY . .
WORKDIR "/src/MyProject"
RUN dotnet build "MyProject.csproj" -c Release -o /app/build --no-restore
FROM build AS publish
RUN dotnet publish "MyProject.csproj" -c Release -o /app/publish --no-restore
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyProject.dll"]

显然,需要解决文件的多次复制问题,但首先我想通过使用Alpine图像来减小图像大小。基于微软样品中的样品,我提出了

#请参阅https://aka.ms/containerfastmode了解Visual Studio如何使用此Dockerfile来构建映像以实现更快的调试。

FROM mcr.microsoft.com/dotnet/runtime:6.0-alpine-amd64 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build
WORKDIR /src
COPY Directory.Build.props .
COPY src/ .
RUN dotnet restore "MyProject/MyProject.csproj" -r linux-musl-x64
COPY . .
WORKDIR "/src/MyProject"
RUN dotnet build "MyProject.csproj" -c Release -o /app/build --no-restore
FROM build AS publish
RUN dotnet publish "MyProject.csproj" -c Release -o /app/publish -r linux-musl-x64 --self-contained false --no-restore
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["./MyProject"]

使用修改后的Dockerfile,我可以构建一个容器,但它不会启动。也没有日志。按照导出和提取容器的方法;应用程序";与原始版本中相同数量的文件,除了原始版本具有额外的文件夹";运行时";。那么,修改后的版本出了什么问题呢?有没有降低图片尺寸的建议?

(附言,我已经更新了问题,包括建议的修复。(

我很确定问题出在这行:

RUN dotnet publish "MyProject.csproj" -c Release -o /app/publish --no-restore

您没有像在dotnet restore命令中那样在此处包含linux-musl-x64RID。

您应该将其更改为:

RUN dotnet publish "MyProject.csproj" -c Release -o /app/publish -r linux-musl-x64 --self-contained false --no-restore

.NET示例中对此进行了说明https://github.com/dotnet/dotnet-docker/blob/90ada36795a870fb0113de7406a683ed05a2057f/samples/dotnetapp/Dockerfile.alpine-x64#L11:

RUN dotnet publish -c Release -o /app -r linux-musl-x64 --self-contained false --no-restore

我在尝试使用dotnet MyProject.dll运行后台服务时遇到了同样的问题。

所以我所做的是将去码头的图像FROM mcr.microsoft.com/dotnet/runtime更改为FROM mcr.microsoft.com/dotnet/aspnet

在发布步骤中,我改为使用UseAppHost=false:

RUN dotnet publish"MyProject.csproj"-c发布-o/app/publish-p:UseAppHost=false

基于您的完整docker文件:

FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine-amd64 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build
WORKDIR /src
COPY Directory.Build.props .
COPY src/ .
RUN dotnet restore "MyProject/MyProject.csproj" 
COPY . .
WORKDIR "/src/MyProject"
RUN dotnet build "MyProject.csproj" -c Release -o /app/build --no-restore
FROM build AS publish
RUN dotnet publish "MyProject.csproj" -c Release -o /app/publish -p:UseAppHost=false --no-restore
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyProject.dll"]

相关内容

  • 没有找到相关文章

最新更新