如何在 docker 容器内以交互方式运行 dotnet 核心控制台应用程序 [英] How can I interactively run a dotnet core console application inside of a docker container

查看:53
本文介绍了如何在 docker 容器内以交互方式运行 dotnet 核心控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 Visual Studio 2019 创建了以下简单的 dotnet 核心控制台应用程序.

Console.WriteLine("Hello World!");var readText = Console.ReadLine();Console.WriteLine(readText);

当我按 F5 时,程序会在 Console.ReadLine 等待我,直到我输入一些文本.当我输入一些文本并按回车键时,相同的文本会显示给我.

现在我为这个控制台项目添加了 docker 支持.我已经在另一个

我错过了什么?为什么在容器内运行的应用程序不接受我的输入?只需要 Ctrl+C 之后容器本身就会退出.

注意,如果容器立即退出,那么您必须将以下内容添加到 docker-compose 文件中,如同一 so 中所述问题的答案.这将阻止容器立即退出.

stdin_open: 真tty: 真的

解决方案

默认启用标准输入和 TTY.

代替 docker-compose up 运行

docker-compose run

从 docker 文档中粘贴:

https://docs.docker.com/compose/reference/run/

针对服务运行一次性命令.例如,以下命令启动 Web 服务并运行 bash 作为其命令.

docker-compose 运行 web bash

两个不同点:

首先,run传递的命令覆盖了服务配置中定义的命令.例如,如果 web 服务配置是用 bash 启动的,那么 docker-compose run web python app.py 会用 python app.py 覆盖它.

第二个区别是 docker-compose run 命令不会创建服务配置中指定的任何端口.这可以防止端口与已经打开的端口发生冲突.如果您确实希望创建服务的端口并将其映射到主机,请指定 --service-ports 标志:

I created the following simple dotnet core console application from Visual Studio 2019.

Console.WriteLine("Hello World!");
var readText = Console.ReadLine();
Console.WriteLine(readText);

When I press F5, the program waits for me at Console.ReadLine till I enter some text. When I type some text and press enter, the same text is displayed to me back.

Now I add docker support to this console project. I have written the step by step instructions in another so question to add the docker support.

After I add the docker support, in a command prompt, I navigate to the folder where the docker-compose file is present and issue the command,

docker-compose up

the application runs, prints the Hello World!. Then apparently it stops and waits for me to input some text. But as I type the text and press enter, nothing happens. Seems that the input I am giving at the console is not being communicated to the app running inside of the container.

What am I missing? Why is the app running inside of the container not taking my input? It only takes Ctrl+C after which the container iteself exits.

Note, if the container exits immediately, then you have to add the following to the docker-compose file as explained in the same so question's answer. This will prevent the container from exiting immediately.

stdin_open: true
tty: true

解决方案

By default standard input and TTY are enabled.

Instead of docker-compose up run

docker-compose run <service name from docker-compose.yml file>

Pasting from the docker docs:

https://docs.docker.com/compose/reference/run/

Runs a one-time command against a service. For example, the following command starts the web service and runs bash as its command.

docker-compose run web bash

two differences:

First, the command passed by run overrides the command defined in the service configuration. For example, if the web service configuration is started with bash, then docker-compose run web python app.py overrides it with python app.py.

The second difference is that the docker-compose run command does not create any of the ports specified in the service configuration. This prevents port collisions with already-open ports. If you do want the service’s ports to be created and mapped to the host, specify the --service-ports flag:

这篇关于如何在 docker 容器内以交互方式运行 dotnet 核心控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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