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

kupeojn6  于 2023-03-01  发布在  Docker
关注(0)|答案(2)|浏览(129)

几个月前,我为一个控制台应用程序创建了我的第一个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如何使用此停靠文件生成映像以加快调试速度。

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,我可以构建一个容器,但它不会启动。也没有日志。按照exporting and extracting容器的方法,我看到文件夹“app”的文件量与原始版本相同,除了原始版本有一个额外的文件夹“runtimes”。那么修改后的版本有什么问题呢?有什么建议可以缩小图像的大小吗?
(PS,我已更新问题,以包括建议的修复方法。)

pjngdqdw

pjngdqdw1#

我很确定问题出在这句台词上:

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

这里没有像dotnet restore命令中那样包含linux-musl-x64 RID。
您应该将其更改为:

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

www.example.com上的.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
jvlzgdj9

jvlzgdj92#

我在尝试使用dotnet MyProject.dll运行后台服务时遇到了同样的问题。
我所做的就是将docker图像FROM mcr.microsoft.com/dotnet/runtime更改为FROM mcr.microsoft.com/dotnet/aspnet
在发布步骤中,我改为使用UseAppHost=false
运行网络发布“我的项目.csproj”-c发布-o /app/publish -p:使用应用程序主机=假
基于您的完整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"]

相关问题