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

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

问题描述

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

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

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

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

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.

现在,我将Docker支持添加到该控制台项目中.我在另一个问题中添加了分步说明,以添加Docker支持.

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.

添加docker支持后,在命令提示符下,导航到docker-compose文件所在的文件夹并发出命令

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

应用程序运行,打印Hello World!.然后显然它停止了,并等待我输入一些文本.但是,当我键入文本并按Enter时,什么也没有发生.似乎我在控制台上提供的输入没有传达给在容器内部运行的应用程序.

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.

我想念什么?为什么在容器内部运行的应用程序无法接收我的输入?只需按Ctrl + C,容器即会退出.

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.

注意,如果容器立即退出,则必须按照以下 so的说明,将以下内容添加到docker-compose文件中问题的答案.这样可以防止容器立即退出.

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

推荐答案

默认情况下,启用了标准输入和TTY.

By default standard input and TTY are enabled.

代替docker-compose up运行

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

从docker文档粘贴:

Pasting from the docker docs:

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

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

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运行Web bash

docker-compose run web bash

两个区别:

首先,运行传递的命令将覆盖服务配置中定义的命令.例如,如果Web服务配置以bash开始,则docker-compose run web python app.py会被python app.py覆盖.

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.

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

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天全站免登陆