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

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

问题描述

我想以交互方式在容器中运行一个简单的 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 Support

  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 应用程序.我在上面描述的类似行中创建了一个 dotnet 核心 Web 应用程序,修改了 docker-compose 文件以包含端口映射(如下所示),当我执行 docker-compose up 时,应用程序启动并运行.但是使用控制台应用程序时,容器会直接退出.

推荐答案

默认情况下,当容器使用 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天全站免登陆