0

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.

NetCore Console App running inside of a docker 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 Answer 1

3

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:

Sign up to request clarification or add additional context in comments.

2 Comments

Man.., not able to believe myself. Its working. If you can explain a little more as to why 'up' does not work and this works.
Couple of observations. 1. The container exits(stops) after the program exits. 2. When I run the command(docker-compose run dokconsoleapp) again, a new container is created. I can see them on the docker desktop dashboard. But with up command, the same container is started again(which does not anyway interact with me). Thanks any way!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.