Skip to content

Host shuts down immediately #3195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rachelappel opened this issue Jun 5, 2018 · 5 comments
Closed

Host shuts down immediately #3195

rachelappel opened this issue Jun 5, 2018 · 5 comments

Comments

@rachelappel
Copy link

rachelappel commented Jun 5, 2018

Moved from the docs repo to here. Originally filed by @srininz77 at dotnet/AspNetCore.Docs#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**"

@Tratcher
Copy link
Member

Tratcher commented Jun 5, 2018

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?

@srininz77
Copy link

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.

@davidfowl
Copy link
Member

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

@srininz77
Copy link

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.

@Eilon
Copy link
Contributor

Eilon commented Jul 13, 2018

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

@Eilon Eilon closed this as completed Jul 13, 2018
@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants