Host shuts down immediately
Moved from the docs repo to here. Originally filed by @srininz77 at https://github.com/aspnet/Docs/issues/6862
"I'm trying to run a long running background task in a console application inside docker container.
My code is as follows:
public static async Task Main(string[] args)
{
var hostBuilder = new HostBuilder()
.ConfigureAppConfiguration
(
(hostBuilderContext, configurationBuilder) =>
{
configurationBuilder.SetBasePath(Directory.GetCurrentDirectory());
configurationBuilder.AddEnvironmentVariables();
configurationBuilder.AddJsonFile("appsettings.json", optional: true);
}
)
.ConfigureServices
(
(hostBuilderContext, services) =>
{
services.AddHostedService<HostedService>();
services.AddSingleton<ISocketImplementation, SocketImplementation>();
services.AddSingleton<IMessageHandler, MessageHandler>();
}
);
await hostBuilder.RunConsoleAsync();
}
**When I use Docker run -d to create the container, the container stops immediately. It doesn't look like RunConsoleAsync() is waiting for ctrl+c or SIGTERM.
I tried GenericHostSample and it behaves the same as well**"
It should wait for one of these two: https://github.com/aspnet/Hosting/blob/ec51afd9d0a973c2a00b37a4c4e529b48ce816bf/src/Microsoft.Extensions.Hosting/Internal/ConsoleLifetime.cs#L43-L52 Maybe it's getting some unexpected input through docker? Or maybe it's running into another error and crashing?
There are no errors. It runs fine when I launch the container from Visual studio because visual studio adds the "tail" process to the container which is keeping the container alive. When I launch it using Docker run the container stops immediately.
Yea, it sounds like something is crashing or failing to start. Try running the container interactively and running the process manually inside of it.
The issue only exists for images built by VisualStudio but not for images built on our build server. Not sure why? Anyway, generic host works as expected. Thanks.
Closing because it seems the main issues have been resolved. Thanks!