aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Host shuts down immediately

Open rachelappel opened this issue 8 years ago • 5 comments

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**"

rachelappel avatar Jun 05 '18 10:06 rachelappel

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?

Tratcher avatar Jun 05 '18 15:06 Tratcher

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.

srininz77 avatar Jun 05 '18 22:06 srininz77

Yea, it sounds like something is crashing or failing to start. Try running the container interactively and running the process manually inside of it.

davidfowl avatar Jun 06 '18 05:06 davidfowl

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.

srininz77 avatar Jun 07 '18 01:06 srininz77

Closing because it seems the main issues have been resolved. Thanks!

Eilon avatar Jul 13 '18 18:07 Eilon