简体   繁体   English

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

[英].NET core console app will not start in docker image based on Alpine

A couple of months ago, I created my first Dockerfile for a console application.几个月前,我为控制台应用程序创建了我的第一个 Dockerfile。 This console application depends on several other c# projects, so it is a bit more complex than the standard samples that you'd typically find on the internet.此控制台应用程序依赖于其他几个 c# 项目,因此它比您通常在 Internet 上找到的标准示例要复杂一些。 The Dockerfile "works", ie I can create an image and run a container. Dockerfile“工作”,即我可以创建一个图像并运行一个容器。 However, I know that it contains several flaws, and now is the time to improve this Dockerfile.但是,我知道它包含几个缺陷,现在是时候改进这个 Dockerfile。 The current version is当前版本是

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"]

Clearly, the multiple copying of files needs to be addressed, but first I want to reduce the image size by using the Alpine image.显然,需要解决文件的多次复制问题,但首先我想通过使用 Alpine 图像来减小图像大小。 Based on the sample in Microsoft samples , I've come up with基于Microsoft samples中的示例,我想出了

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. #请参阅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"]

Using the modified Dockerfile, I can build a container, but it will not start.使用修改后的Dockerfile,可以建容器,但是启动不了。 There are also no logs.也没有日志。 Following the approach of exporting and extracting the container, I see that the folder "app" the same amount of files as in the original version, except the original version has an additional folder "runtimes".按照导出和提取容器的方法,我看到文件夹“app”与原始版本中的文件数量相同,除了原始版本有一个额外的文件夹“runtimes”。 So what is wrong with the modified version?那么修改后的版本有什么问题呢? Are there any suggestions to bring down the size of the image?有什么建议可以减小图像的大小吗?

(PS, I've updated the question to include suggested fix.) (PS,我已经更新了问题以包括建议的修复。)

I'm pretty sure the issue is with this line:我很确定问题出在这一行:

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

You didn't include the linux-musl-x64 RID here like you did in the dotnet restore command.您没有像在dotnet restore命令中那样在此处包含linux-musl-x64 RID。

You should change it to this:你应该把它改成这样:

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

This is illustrated in the .NET samples at https://github.com/dotnet/dotnet-docker/blob/90ada36795a870fb0113de7406a683ed05a2057f/samples/dotnetapp/Dockerfile.alpine-x64#L11 :这在https://github.com/dotnet/dotnet-docker/blob/90ada36795a870fb0113de7406a683ed05a2057f/samples/dotnet11 Dockerfile .

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM