Keep a self hosted servicestack service open as a docker swarm service without using console readline or readkey

拜拜、爱过 提交于 2020-01-14 12:55:07

问题


I have a console application written in C# using servicestack that has the following form:

static void Main(string[] args)
        {
            //Some service setup code here

            Console.ReadKey();
        }

This code works fine when run on windows as a console. The implementation is almost exactly https://github.com/ServiceStack/ServiceStack/wiki/Self-hosting as this is a test project

I then compile this project using mono on linux and build into a docker file.

I have no problems running up a container based on this image if it is interactive

docker run -it --name bob -p 1337:1337 <myservice>

The container runs in the foreground

However, if I omit the -it switch, the container exits straight away - I assume because there is no STDIN stream, so the Console.ReadKey() doesn't work.

I am trying to get the service hosted in a swarm, so there is no notion of detached. I can spin up a loop within my main method to keep the console service alive, but this seems hacky to me...

Is there a good way of keeping my service alive in the situation where I want to run my container detatched (docker run -d...)


回答1:


Stealing from this answer for keeping .NET apps alive, you can wait without using Console which means you don't need to keep stdin open in Docker (docke run -i) :

private ManualResetEvent Wait = new ManualResetEvent(false);
Wait.WaitOne();

Docker will send a SIGTERM on docker stop and a ctrl-c will send a SIGINT so those signals should be trapped and then you let the program end with

Wait.Set();


来源:https://stackoverflow.com/questions/39246610/keep-a-self-hosted-servicestack-service-open-as-a-docker-swarm-service-without-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!