为什么运行控制台应用程序的容器在启动后仅退出 [英] Why does a container running a console app simply exits after starting

查看:92
本文介绍了为什么运行控制台应用程序的容器在启动后仅退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在容器中交互运行一个简单的dotnet核心控制台应用程序.我无法执行此操作,并且容器只是在没有完全运行程序的情况下启动然后立即退出.

I want to run a simple dotnet core console app in a container interactively. I am not able to do that and the container simply starts and then exits immediately without fully running the program.

控制台应用程序仅具有以下三个语句.

All that the console app has is the followng three statements.

Console.WriteLine("Hello World!");
var readText = Console.ReadLine(); // Wait for me to enter some text
Console.WriteLine(readText);

最后两行使其具有交互性.

The last two lines make it interactive.

运行容器时,它会打印Hello World!但是随后它立即退出,而无需等待我输入一些文本.为什么?我想念什么?

When I run the container, it prints the Hello World! But then it immediately exits, without waiting for me enter some text. Why? what am I missing?

我能够以类似的方式在容器中运行dotnet核心Web应用程序,并且能够映射容器外部和内部的端口以成功浏览Web应用程序.但是当涉及到控制台应用程序时,我很困惑.

I was able to run a dotnet core web app in the container in a similar manner, and I am able to map the ports outside and within the container to successfully browse the web app. But when it comes to console app, I am stumped.

我想我可能会缺少一些非常简单的东西.疯了我

I guess there could be something very simple that I am missing. Driving me nuts

如下所述的复制步骤.

  1. 启动Vs2019并创建一个新的.net核心控制台项目.

  1. 添加一些语句以使其具有交互性.

  1. 将Docker支持添加到创建的项目中.右键单击项目,添加->选择Container Orchestrator支持

  1. 现在visual studio创建了一组文件并更改了csproj文件.

  1. 在Powershell中,导航到包含解决方案文件的文件夹.运行命令"docker-compose up"

  1. 一旦建立了映像,并且容器已启动并运行,我们就可以开始看到问题了.

  1. 我们可以在此处看到Hello-World!. 但它不等我输入一些东西.输入docker ps -a并查看已退出的容器.当我尝试使用docker start -i或docker start -a启动该容器时,该容器会启动,但会立即退出.我应该怎么做才能使容器运行,以便我可以为其中运行的应用程序键入一些内容?您也可以在Docker桌面中看到.即使我启动它们(针对每个容器使用docker桌面UI上提供的开始按钮),它也会再次停止.
  1. We can see here Hello-World!. But it does not wait for me to type something. Type docker ps -a and see a container that is exited. When I try to start that using docker start -i or docker start -a, the container starts but exits immediately. What should I do to make the container run so that i can type something for my app running in it read? You can see in the docker desktop as well. Even if I start them(using the start button available with the docker desktop UI against each container), it simply stops again.

  1. 我已经在容器中运行了Web应用程序.通过正确的端口映射,可以从外部访问在容器内部运行的Web应用程序.我已经以与上述类似的方式创建了一个dotnet核心Web应用程序,修改了docker-compose文件以包含端口映射(如下所示),并且当我进行docker-compose时,该应用程序已启动并正在运行.但是使用控制台应用程序,容器便会退出.

推荐答案

默认情况下,当容器以docker-compose up启动时,您没有交互式TTY.

By default, you don't have an interactive TTY when the container is started with docker-compose up.

您需要将其添加到服务中

You need to add that to your service:

stdin_open: true
tty: true

这篇关于为什么运行控制台应用程序的容器在启动后仅退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆