简体   繁体   中英

.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. 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. The Dockerfile "works", ie I can create an image and run a container. However, I know that it contains several flaws, and now is the time to improve this 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. Based on the sample in Microsoft samples , I've come up with

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

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. 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". 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.)

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.

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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