Skip to main content

The following code shows you the structure of the console app we created and will help you prepare a dotnet core console app for Docker.

---
title: Prepare a Dotnet Core Console App for Docker
subtitle: The following code shows you the structure of the console app we created and will help you Prepare a .Net Core Console App for Docker
author: Carlos Mendible
date: October 17, 2017
source: https://carlos.mendible.com/2017/10/15/prepare-a-net-core-console-app-for-docker/
---

Last week I had the luck to attend the [Microsoft Azure OpenHack in Amsterdam](https://www.microsoftevents.com/profile/form/index.cfm?PKformID=0x2564678abcd).
We spent two and a half days learning a lot about kubernetes, Azure Container
Services, Azure Container Registry, Azure OMS and Minecraft!

In one of the challenges we decided to implement a [sidecar container](https://docs.microsoft.com/en-us/azure/architecture/patterns/sidecar)
for logging purposes. So using .NET Core we created a console application with
proper handling of the <kbd>Control + C</kbd> and <kbd>Control + Break</kbd>
key shortcuts.

The following code shows you the structure of the console app we created
and will help you **Prepare a .Net Core Console App for Docker**

```cs
using System;
using System.Threading;
using System.Threading.Tasks;

namespace docker.controlc
{
    class Program
    {
        // AutoResetEvent to signal when to exit the application.
        private static readonly AutoResetEvent waitHandle = new AutoResetEvent(false);

        static void Main(string[] args)
        {
            // Fire and forget
            Task.Run(() =>
            {
                var random = new Random(10);
                while (true)
                {
                    // Write here whatever your side car applications needs to do.
                    // In this sample we are just writing a random number to the Console (stdout)
                    Console.WriteLine($"Loop = {random.Next()}");

                    // Sleep as long as you need.
                    Thread.Sleep(1000);
                }
            });

            // Handle Control+C or Control+Break
            Console.CancelKeyPress += (o, e) =>
            {
                Console.WriteLine("Exit");

                // Allow the manin thread to continue and exit...
                waitHandle.Set();
            };

            // Wait
            waitHandle.WaitOne();
        }
    }
}
```

Note that we are handling the `Console.CancelKeyPress` because Docker does not
behave as expected if you use the typical `Console.ReadKey` method to make the
application run until a key is pressed.

Get the [code here](https://github.com/cmendible/dotnetcore.samples/tree/master/docker.controlc).