Why does a container running a console app simply exits after starting

浪尽此生 提交于 2020-06-09 04:41:19

问题


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.

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?

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

The steps to reproduce as described below.

  1. Launch Vs2019 and create a new .net core console project.

  1. Add a couple of statements to make it interactive.

  1. Add Docker support to the created project. Right click the project, Add -> select Container Orchestrator Support

  1. Now visual studio creates a set of files and changes the csproj file as well.

  1. In a powershell navigate to folder having the solution file. Run the command "docker-compose up"

  1. Once the images are built, and containers are up and running, we can see start to see the problem.

  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. I had run web apps in containers. With proper port mapping, a web app running inside of a container can be accessed from outside. I had created a dotnet core web app in similar lines described above, modified the docker-compose file to include port mapping(shown below) and when I do docker-compose up, the app is up and running. But with a console app, the container simply exits.


回答1:


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



回答2:


I found, Docker compose run is an alternative to up command.

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

Specifically, for my application, the docker compose file looks as follows.

version: '3.4'

services:
  dokconsoleapp:
    image: ${DOCKER_REGISTRY-}dokconsoleapp
    build:
      context: .
      dockerfile: DokConsoleApp/Dockerfile
#    stdin_open: true
#    tty: true

Note the last two lines are commented out.

Now if I run

docker-compose run dokconsoleapp

The container runs till the end of the program, interactively waiting for me to type an input after Hello-World!.

So statements

    stdin_open: true
    tty: true

are not needed when you use run with docker-compose instead of up



来源:https://stackoverflow.com/questions/62065836/why-does-a-container-running-a-console-app-simply-exits-after-starting

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