I am building a background service (.NET 8) that is connected to a RabbitMQ server and Sql database. Both RabbitMQ server and Sql database are hosted on docker. When I run the application in debug mode on Visual Studio 2022 the application works perfectly fine. When I publish the application and register it as a service on my computer that using Windows 11, it also works perfectly well. I wrote a Dockerfile that will containerize the application. After building the image and container, the container does not start. Here are more details.
Solution folder structure: Solution folder structure
Project file:
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-LogRecord_Service-2087123f-9942-4cc7-aa76-8c572656e691</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="NLog" Version="5.2.8" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.3.8" />
<PackageReference Include="Quartz" Version="3.8.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Database_Logic_Layer\Database_Logic_Layer.csproj" />
<ProjectReference Include="..\Geotab_Logic_Layer\Geotab_Logic_Layer.csproj" />
<ProjectReference Include="..\QueueManagement\QueueManagement.csproj" />
</ItemGroup>
</Project>
Docker File:
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["LogRecord_Service/LogRecord_Service.csproj", "LogRecord_Service/"]
COPY ["Database_Logic_Layer/Database_Logic_Layer.csproj", "Database_Logic_Layer/"]
COPY ["DataAccess/DataAccess.csproj", "DataAccess/"]
COPY ["Helpers/Helpers.csproj", "Helpers/"]
COPY ["Geotab_Logic_Layer/Geotab_Logic_Layer.csproj", "Geotab_Logic_Layer/"]
COPY ["QueueManagement/QueueManagement.csproj", "QueueManagement/"]
RUN dotnet restore "./LogRecord_Service/LogRecord_Service.csproj"
COPY . .
WORKDIR "/src/LogRecord_Service"
RUN dotnet build "./LogRecord_Service.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./LogRecord_Service.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "LogRecord_Service.dll"]
Image build logs:
docker build -t log_record_producer_image .
[+] Building 1.2s (22/22) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 1.31kB 0.0s
=> [internal] load metadata for mcr.microsoft.com/dotnet/runtime:8.0 0.0s
=> [internal] load metadata for mcr.microsoft.com/dotnet/sdk:8.0 0.4s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 464B 0.0s
=> [build 1/12] FROM mcr.microsoft.com/dotnet/sdk:8.0@sha256:9bb0d97c4361cc844f225c144ac2adb2b65fabfef21f95caedcf215d844238fe 0.0s
=> CACHED [final 1/3] FROM mcr.microsoft.com/dotnet/runtime:8.0 0.0s
=> [internal] load build context 0.1s
=> => transferring context: 6.22kB 0.0s
=> [final 2/3] WORKDIR /app 0.1s
=> CACHED [build 2/12] WORKDIR /src 0.0s
=> CACHED [build 3/12] COPY [LogRecord_Service/LogRecord_Service.csproj, LogRecord_Service/] 0.0s
=> CACHED [build 4/12] COPY [Database_Logic_Layer/Database_Logic_Layer.csproj, Database_Logic_Layer/] 0.0s
=> CACHED [build 5/12] COPY [DataAccess/DataAccess.csproj, DataAccess/] 0.0s
=> CACHED [build 6/12] COPY [Helpers/Helpers.csproj, Helpers/] 0.0s
=> CACHED [build 7/12] COPY [Geotab_Logic_Layer/Geotab_Logic_Layer.csproj, Geotab_Logic_Layer/] 0.0s
=> CACHED [build 8/12] COPY [QueueManagement/QueueManagement.csproj, QueueManagement/] 0.0s
=> CACHED [build 9/12] RUN dotnet restore "./LogRecord_Service/LogRecord_Service.csproj" 0.0s
=> CACHED [build 10/12] COPY . . 0.0s
=> CACHED [build 11/12] WORKDIR /src/LogRecord_Service 0.0s
=> CACHED [build 12/12] RUN dotnet build "./LogRecord_Service.csproj" -c Release -o /app/build 0.0s
=> CACHED [publish 1/1] RUN dotnet publish "./LogRecord_Service.csproj" -c Release -o /app/publish /p:UseAppHost=false 0.0s
=> [final 3/3] COPY --from=publish /app/publish . 0.5s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:bc73c757dec2ead70c8db7df5a2e08fe9665e8b0396ab2a141bf590fd449af82 0.0s
=> => naming to docker.io/library/log_record_producer_image 0.0s
View build details: docker-desktop://dashboard/build/default/default/r14olesvrbxevmit01mirf4ev
What's Next?
View a summary of image vulnerabilities and recommendations → docker scout quickview
Container logs after creating and attempting to start the container:
I am not sure what the issue is. I used a similar file to create and start a container for a console application (also .Net 8) and it worked perfectly well. Please help in figuring this out.
- I asked ChatGPT about this issue and it kept regargitating the same information. It says my Dockerfile is incorrect and I should change it. The suggested Dockerfile it gave me was identical to the one I have.
- I also checked on Github for similar issues and the proposed solutions do not solve my current problem.
Your app is looking for the ASP.NET framework (as indicated by the log message
Framework: 'Microsoft.AspNetCore.App', version '8.0.0' (x64)). ASP.NET isn't included in theruntimeimage. You need to use theaspnetimage as your base image.Change
to
and it should work.
In the future, please don't link images of text in your questions. Copy and paste the text into your question. That makes searching and copy/pasting a lot easier. I had to type in the log message manually from your image.