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.
Visual Studio Code
You can download Visual Studio Code for Linux, Mac, and Windows here: https://code.visualstudio.com/download
Dotnet Core SDK
Free Download for: https://www.microsoft.com/net/download/
Docker
Free Download here: https://www.docker.com/get-docker
Create a dotnet core app
Open your favorite command line tool like bash or cmd
Create a new folder for your app, e.q. c:\dev\dotnet-core-sample
Start Visual Studio Code in this new folder with the following command:
code .
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.
Create new dotnet core console project with the following command in the terminal window
dotnet new console
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.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()}");
}
}
}
Start the code
You can press F5 in Visual Studio Code. It will compile/build the code and start the integrated debugger.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\'
Now you're done with the app.
Build and Deploy the app with docker to a Raspberry Pi
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 subscriptionLogin to your ACR
powershell docker login -u <username> -password <password> <acrlink>
Steps for ARM Version to be deployed to a Raspberry Pi
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.
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'
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.