1

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.

New DotNet Core Console Project Creation

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

Simple DotNet Core Console App

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

Adding Docker Support To Console App

Adding Container Orchestrator Support With Docker Compose.jpg

Docker Linux Containers

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

Solution Folder In Windows Explorer

Project Folder In Windows Explorer

Solution Explorer After Adding Docker Support

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

Docker-Compose Command InPowerShell

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

Docker Start Container

  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.

Docker Containers In Docker Desktop

  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.

Docker file With Port Mapping For WebApp

VivekDev
  • 20,868
  • 27
  • 132
  • 202
  • 1
    does this answer your question? https://stackoverflow.com/questions/36249744/interactive-shell-using-docker-compose – paltaa May 28 '20 at 13:30

2 Answers2

5

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
jmaitrehenry
  • 2,190
  • 21
  • 31
0

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

VivekDev
  • 20,868
  • 27
  • 132
  • 202