Create a Dotnet Core app and deployed it via Docker to Raspberry Pi

This tutorial describes the steps to create a simple dotnet core app and package it in a container to be deployed to a Raspberry Pi with Raspian & Docker installed.

Prerequisites

Please install the following tools on your dev machine. It doesn't matter which of the following plattforms you're using: Mac, Linux, Windows.

  1. Visual Studio Code

    You can download Visual Studio Code for Linux, Mac, and Windows here: https://code.visualstudio.com/download

  2. Dotnet Core SDK

    Free Download for: https://www.microsoft.com/net/download/

  3. Docker

    Free Download here: https://www.docker.com/get-docker

Create a dotnet core app

  1. Open your favorite command line tool like bash or cmd

  2. Create a new folder for your app, e.q. c:\dev\dotnet-core-sample

  3. Start Visual Studio Code in this new folder with the following command:

    code .

  4. Open a terminal window in Visual Studio Code

    Menu --> View --> Integrated Terminal The terminal feature is really cool, and enables you to use bash, powershell, within Visual Studio Code.

  5. Create new dotnet core console project with the following command in the terminal window

    dotnet new console

  6. Visual Studio will ask you, to restore needed assets, just agree. It will internal call the method dotnet restore in your current folder. And this feature restores referenced libraries, if they are not installed directly.

  7. The code of "program.cs" is only displaying the current time, every second.

    using System;
    using System.Threading;

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

                Timer timer = new Timer(timerCallback, null, 1000, 1000);

                Console.WriteLine("Press enter to exit");
                Console.ReadLine();
            }

            private static void timerCallback(object state)
            {
                Console.WriteLine($"Timer ticked at {DateTime.Now.ToLongTimeString()}");
            }
        }
    }
  1. Start the code
    You can press F5 in Visual Studio Code. It will compile/build the code and start the integrated debugger.

  2. Your done? Then publish your code. Create the final build.

    Open a terminal window in code and enter the following command dotnet publish all required files will be copied to a folder called something like 'C:\dev\dotnet-core-sample\bin\Debug\netcoreapp2.0\publish\'

Publish Folder

Now you're done with the app.

Build and Deploy the app with docker to a Raspberry Pi

  1. Create Azure Container Registry (if not done yet)
    Go to the Azure portal via https://portal.azure.com and create an Azure Container Registry in your subscription

  2. Login to your ACR

    powershell docker login -u <username> -password <password> <acrlink>

Steps for ARM Version to be deployed to a Raspberry Pi

  1. Add a file called 'dockerfile.arm' to the root of your application
    This file creates an image for Raspberry Pi which is based on ARM.

    ``` docker FROM microsoft/dotnet:2.0-sdk as builder ENV DOTNETCLITELEMETRY_OPTOUT 1 RUN mkdir -p /root/src/app WORKDIR /root/src/app COPY . dotnet-core-sample WORKDIR /root/src/app/dotnet-core-sample RUN dotnet restore ./dotnet-core-sample.csproj RUN dotnet publish -c release -o published -r linux-arm

    FROM microsoft/dotnet:2.0.0-runtime-stretch-arm32v7 WORKDIR /root/ COPY --from=builder /root/src/app/dotnet-core-sample/published . CMD ["dotnet", "./dotnet-core-sample.dll"] ```

    This is a multistep dockerfile with a container for the build process, and one container for the execution.

  2. Build the image with the following command

    powershell docker build --rm -f dockerfile.arm -t oscheeracr.azurecr.io/dotnet-core-sample-arm:latest .

    My tag for the image is called 'oscheeracr.azurecr.io/dotnet-core-sample-arm:latest'

  3. Push it to your registry if you like with the following command

    powershell docker push oscheeracr.azurecr.io/dotnet-core-sample-arm:latest

Now you're down with the "upload" of your image to your registry.

Using the image on a Raspberry Pi

Now switch to your Raspberry Pi with Putty or VNCViewer.

Open the command shell and to the following 3 steps to get your image running ... hopefully :-)

docker login -u <username> -p <password> <acrlink>  
docker pull <imagename>  
docker run -it <imagename>  

You should see something like this, when you're successful.

Dotnet core app on RPI