gracefully stop azure container instance

Viewed 271

I have a .net core 5.0 application wrapped in a docker image, I can run the image as a container under Docker Desktop with Linux Container mode, when I am stopping the container app is able to catch following events, which is what I want.

System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += ctx =>
{
   Console.WriteLine("Unloading");
};

AppDomain.CurrentDomain.ProcessExit += (s, e) =>
{
   Console.WriteLine("Exitting");
};

However once I push the docker image to the azure registry and start as Azure container instance, neither stopping or restart via button in portal.azure trigger these events. Same issue for azure cloud shell commands.

az container restart --name gracefullApp --resource-group gracefullAppGroup
az container stop --name gracefullApp --resource-group gracefullAppGroup

Any idea how can this be achieved?

The dockerfile is generated by VS2019

FROM mcr.microsoft.com/dotnet/runtime:5.0-buster-slim AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["gracefullApp/gracefullApp.csproj", "gracefullApp/"]
RUN dotnet restore "gracefullApp/gracefullApp.csproj"
COPY . .
WORKDIR "/src/gracefullApp"
RUN dotnet build "gracefullApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "gracefullApp.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "gracefullApp.dll"]
1 Answers

Unfortunately this is happening because when you run "docker stop", docker sends a Sigterm to the containers pid 1.

What the sigterm signal does is alert the process to shutdown but the process might might finish up whatever it waa doing during the signaling and then terminate itself once complete. Think of it as a graceful shutdown.

Azure container instances send the SIGKILL signal which is more destructive in a sense as it doesnt care what the process was doing or will do after, it will just terminate. This is why you having this issue but works fine in docker, I do know its currently an issue on their github board as well since 2021

Related