How can I interactively run a dotnet core console application inside of a docker container

只谈情不闲聊 提交于 2020-08-10 19:15:52

问题


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

回答1:


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:



来源:https://stackoverflow.com/questions/62171941/how-can-i-interactively-run-a-dotnet-core-console-application-inside-of-a-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!