简体   繁体   中英

Running a standard .NET Console Application in docker container

I am trying to run a very simple console application as a windows docker container. I have a docker file shown below using the "dotnet-framework:4.7.2-runtime-windowsservercore-1803" base image.

FROM microsoft/dotnet-framework:4.7.2-runtime-windowsservercore-1803
ARG source
WORKDIR /app
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT "DockerConsoleApp.exe"

The console application just outputs "Hello World" to a log file every 5 seconds"

static void Main(string[] args)
    {
        while (true)
        {
            try
            {
                Thread.Sleep(5000);
                _logger.Info("Hello Wolrd");
            }
            catch (Exception e)
            {
                //handle the exception 
                Console.Error.WriteLine(e);
            }
        }


    }

I am using the following docker compose file

version: '3.4'

services:
  dockerconsoleapp:
    image: dockerconsoleapp:dev
    build:
      context: .\
      args:
        source: obj\Docker\publish
    volumes:
      - C:\Users\user\source\repos\DockerConsoleApp\DockerConsoleApp:C:\app
      - C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\Remote Debugger:C:\remote_debugger:ro
      - C:\Users\user\source\repos\DockerConsoleApp\VolumeTest:C:\app\logs

The problem is that as soon as I manually build or run "docker-compose up -d" The container is created and then immediately dies. I would expect that the container should stay up given that the application is being called in the entrypoint and the application should just keep going unless manually stopped.

Your container died most likely as a result of exception in your ENTRYPOINT or ENTRYPOINT itself not being valid. You can examine docker logs to find out a reason.

In the end, the fix was to change the ENTRYPOINT to a CMD in the dockerfile. See below.

FROM microsoft/dotnet-framework:4.7.2-runtime-windowsservercore-1803
ARG source
WORKDIR /app
COPY ${source:-obj/Docker/publish} .
CMD ["DockerConsoleApp.exe"]

I have let this run overnight and the container is now up 15 hours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM