docker-compose does not attach stdin to .net core app Console.ReadLine

Viewed 940

I've noticed strange behavior when using docker-compose. Console.ReadLine() does not wait for user-input.

Observe the following simplified dot net core c# console app:

private static void Main(string[] args)
{
    Console.WriteLine("Input something:");
    while (true)
    {
        var line = Console.ReadLine();
        Console.WriteLine($"{DateTime.Now} Input was: {line}");
        System.Threading.Thread.Sleep(1000);
    }
}

If you run this via docker run -i readline it works fine:

enter image description here

But if you use docker-compose up it just loops without waiting for input on STDIN:

enter image description here

Why is that?

You can try it yourself using:

git clone https://github.com/mh-qUjB/docker-dotnetcore-readline.git
cd ./docker-dotnetcore-readline/ReadLine
docker-compose up

I'm currently testing this on:

  • Ubuntu 16.04
  • Docker version 18.06.1-ce, build e68fc7a
  • docker-compose version 1.22.0, build f46880fe
  • <TargetFramework>netcoreapp2.1</TargetFramework>
  • Dockerfile see here

Thank you.

PS: if you could improve the title of this question I'd be happy.

1 Answers

You can find individual container and execute the command in that container. Docker compose is streaming your logs, because of this it does not provide you an interactive shell.

As example lets say you have this compose file,

version: '3'

services:
  nats:
    image: nats-streaming
  mongo:
    image: mongo
   

You can execute command in each container like bellow

docker-compose exec mongo sh
docker-compose exec nats sh
Related