[英]Keep a self hosted servicestack service open as a docker swarm service without using console readline or readkey
I have a console application written in C# using servicestack that has the following form: 我有一个使用servicestack用C#编写的控制台应用程序,其形式如下:
static void Main(string[] args)
{
//Some service setup code here
Console.ReadKey();
}
This code works fine when run on windows as a console. 在Windows上作为控制台运行时,此代码可以正常工作。 The implementation is almost exactly https://github.com/ServiceStack/ServiceStack/wiki/Self-hosting as this is a test project 实现几乎完全是https://github.com/ServiceStack/ServiceStack/wiki/Self-hosting,因为这是一个测试项目
I then compile this project using mono on linux and build into a docker file. 然后我在linux上使用mono编译这个项目并构建成一个docker文件。
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. 但是,如果省略-it开关,容器会立即退出 - 我假设因为没有STDIN流,所以Console.ReadKey()不起作用。
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... 我可以在我的main方法中启动一个循环来保持控制台服务的活着,但这对我来说似乎很烦人......
Is there a good way of keeping my service alive in the situation where I want to run my container detatched (docker run -d...) 是否有一种很好的方法可以保持我的服务在我想要运行我的容器detatched(docker run -d ...)的情况下
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
) : 为了保持.NET应用程序的活跃 ,你可以在不使用Console
情况下等待,这意味着你不需要在Docker中保持stdin打开( 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 Docker会在docker stop
上发送一个SIGTERM,一个ctrl-c会发送一个SIGINT
所以这些信号应该被捕获 ,然后让程序结束
Wait.Set();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.