CodexBloom - Programming Q&A Platform

ASP.NET Core 6 Background Service Not Starting as Expected in Docker Container

๐Ÿ‘€ Views: 96 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-24
asp.net-core docker background-service C#

Hey everyone, I'm running into an issue that's driving me crazy. I'm working with an scenario with a background service in my ASP.NET Core 6 application that's running inside a Docker container. I've implemented a hosted service that should run every minute, but it seems like the service is not starting when I build and run the Docker image. Iโ€™ve simplified my hosted service to just log a message to the console to debug the scenario: ```csharp public class TimedHostedService : IHostedService, IDisposable { private Timer _timer; private readonly ILogger<TimedHostedService> _logger; public TimedHostedService(ILogger<TimedHostedService> logger) { _logger = logger; } public Task StartAsync(CancellationToken cancellationToken) { _logger.LogInformation("Timed Background Service is starting."); _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(1)); return Task.CompletedTask; } private void DoWork(object state) { _logger.LogInformation("Timed Background Service is working."); } public Task StopAsync(CancellationToken cancellationToken) { _logger.LogInformation("Timed Background Service is stopping."); _timer?.Change(Timeout.Infinite, 0); return Task.CompletedTask; } public void Dispose() { _timer?.Dispose(); } } ``` In my `Startup.cs`, I have registered the service like this: ```csharp public void ConfigureServices(IServiceCollection services) { services.AddHostedService<TimedHostedService>(); } ``` When I run the application locally, everything works fine, and the logs show the expected messages. However, when I run this in a Docker container, I see the following output: ``` Timed Background Service is starting. ``` But I never see the "Timed Background Service is working." message in the logs. Iโ€™ve checked the Docker logs, and there are no exceptions being thrown, but it's clear that the `DoWork` method isn't being called. I've tried increasing the container's CPU and memory limits, thinking it might be a resource scenario, but that hasn't helped. Additionally, Iโ€™ve verified that the application is running in a non-interactive mode by using `--interactive` in my Docker run command, yet still no background activity. Hereโ€™s the relevant part of my Dockerfile: ```dockerfile FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base WORKDIR /app EXPOSE 80 FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build WORKDIR /src COPY [YourSolution].sln . COPY [YourProjectName]/[YourProjectName].csproj [YourProjectName]/ RUN dotnet restore COPY . . WORKDIR /src/[YourProjectName] RUN dotnet build -c Release -o /app/build FROM build AS publish RUN dotnet publish -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "[YourProjectName].dll"] ``` I suspect there might be some configuration or environment variable related to the Docker container affecting the timer's execution. Has anyone experienced a similar scenario or can suggest what might be going wrong? Any insights on getting the background service to work correctly inside the Docker environment would be greatly appreciated! For context: I'm using C# on Windows. Has anyone else encountered this?