1

I have this docker container of a console app that is supposed to get a value from user with Console.Readline command then continues it's job. However whenever I use a Console.Readline command inside myapplication and try to create image from it, I get the following exception:

System.IO.IOException: 'The handle is invalid.'

this is my docker file:

FROM mcr.microsoft.com/dotnet/core/runtime:3.1-nanoserver-1903 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["DockerSample/DockerSample.csproj", "DockerSample/"]
RUN dotnet restore "DockerSample/DockerSample.csproj"
COPY . .
WORKDIR "/src/DockerSample"
RUN dotnet build "DockerSample.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "DockerSample.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerSample.dll"]

this is my code in console application:

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var input=Console.ReadLine();
           //do something with my input
        }

    }
Sara Nikta Yousefi
  • 1,467
  • 5
  • 21
  • 35
  • Try this: https://stackoverflow.com/questions/38549006/docker-container-exits-immediately-even-with-console-readline-in-a-net-core-c/38550238#38550238 – Sándor Jankovics Jan 18 '20 at 07:59
  • thanks for the comment. However this doesn't answer my question, I cannot create image from my application when using a console.readline but they already created that and just trying to run the container in an interactive mode – Sara Nikta Yousefi Jan 18 '20 at 08:07

1 Answers1

0

I tried to reproduce your exception, but I had another problem with

mcr.microsoft.com/dotnet/core/runtime:3.1-nanoserver-1903

I replaced it with that

mcr.microsoft.com/dotnet/core/runtime:3.0

My Dockerfile now looks like this

FROM mcr.microsoft.com/dotnet/core/runtime:3.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
WORKDIR /src
COPY ["ConsoleApp2/ConsoleApp2.csproj", "ConsoleApp2/"]
RUN dotnet restore "ConsoleApp2/ConsoleApp2.csproj"
COPY . .
WORKDIR "/src/ConsoleApp2"
RUN dotnet build "ConsoleApp2.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "ConsoleApp2.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ConsoleApp2.dll"]

My code in the ConsoleApp2 looks like this

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("Input:");     
            string input = Console.ReadLine();
            Console.WriteLine("Output:");
            Console.WriteLine(input);
        }
    }

I used the following commands for the Docker, but I didn't get any exception. And the app works fine.

docker build -t consoleapp .
docker run -ti  consoleapp
  • thanks for reply. I checked the code, you probably using .net core 3.0 that's why you had some issue with runtime:3.1-nanoserver-1903.Nevertheless I downgrade my project to .net core 3.0 and used your dockerfile configs but still getting the same exception:(( – Sara Nikta Yousefi Jan 18 '20 at 11:39