I love me some Raspberry Pi. They are great little learning machines and are super fun for kids to play with. Even if those kids are adults and they build a 6 node Kubernetes Raspberry Pi Cluster .
Open source .NET Core runs basically everywhere - Windows, Mac, and a dozen Linuxes. However, there is an SDK (that compiles and builds) and a Runtime (that does the actual running of your app). In the past, the .NET Core SDK (to be clear, the ability to "dotnet build") wasn't supported on ARMv7/ARMv8 chips like the Raspberry Pi. Now it is.
Note:.NET Core 2.1 is supported on Raspberry Pi 2+. It isn’t supported on the Pi Zero or other devices that use an ARMv6 chip. .NET Core requires ARMv7 or ARMv8 chips, like the ARM Cortex-A53 . Folks on the Azure IoT Edge team use the .NET Core Bionic ARM32 Docker images to support developers writing C# with Edge devices .
There's two ways to run .NET Core on a Raspberry Pi.
One, use Docker . This is literally the fastest and easiest way to get .NET Core up and running on a Pi. It sounds crazy but Raspberry Pis are brilliant little Docker container capable systems. You can do it in minutes, truly. You can install Docker quickly on a Raspberry Pi with just:
curl -sSL https://get.docker.com | sh sudo usermod -aG docker pi
After installing Docker you'll want to log in and out. You might want to try a quick sample to make sure .NET Core runs! You can explore the available Docker tags at https://hub.docker.com/r/microsoft/dotnet/tags/ and you can read about the .NET Core Docker samples here https://github.com/dotnet/dotnet-docker/tree/master/samples/dotnetapp
Now I can just docker run and then pass in "dotnet --info" to find out about dotnet on my Pi.
pi@raspberrypi:~ $ docker run --rm -it microsoft/dotnet:2.1-sdk dotnet --info .NET Core SDK (reflecting any global.json): Version: 2.1.300-rc1-008673 Commit: f5e3ddbe73 Runtime Environment: OS Name: debian OS Version: 9 OS Platform: Linux RID: debian.9-x86 Base Path: /usr/share/dotnet/sdk/2.1.300-rc1-008673/ Host (useful for support): Version: 2.1.0-rc1 Commit: eb9bc92051 .NET Core SDKs installed: 2.1.300-rc1-008673 [/usr/share/dotnet/sdk] .NET Core runtimes installed: Microsoft.NETCore.App 2.1.0-rc1 [/usr/share/dotnet/shared/Microsoft.NETCore.App] To install additional .NET Core runtimes or SDKs: https://aka.ms/dotnet-download
This is super cool. There I'm on the Raspberry Pi (RPi) and I just ask for the dotnet:2.1-sdk and because they are using "multiarch" docker files, Docker does the right thing and it just works. If you want to use .NET Core on ARM32 with Docker , you can use any of the following tags.
Try one in minutes like this:
docker run --rm microsoft/dotnet-samples:dotnetapp
Here it is downloading the images...
In previous versions of .NET Core's Dockerfiles it would fail if you were running an x64 image on ARM:
standard_init_linux.go:190: exec user process caused "exec format error"
Different processors! But with multiarch per https://github.com/dotnet/announcements/issues/14 Kendra from Microsoft it just works with 2.1.
Docker has a multi-arch feature that microsoft/dotnet-nightly recently started utilizing. The plan is to port this to the official microsoft/dotnet repo shortly. The multi-arch feature allows a single tag to be used across multiple machine configurations. Without this feature each architecture/OS/platform requires a unique tag. For example, the microsoft/dotnet:1.0-runtime tag is based on Debian and microsoft/dotnet:1.0-runtime-nanoserver if based on Nano Server. With multi-arch there will be one common microsoft/dotnet:1.0-runtime tag. If you pull that tag from a Linux container environment you will get the Debian based image whereas if you pull that tag from a Windows container environment you will get the Nano Server based image. This helps provide tag uniformity across Docker environments thus eliminating confusion.
In these examples above I can:
As a quick example, here I'll jump into a container and new up a quick console app and run it, just to prove I can. This work will be thrown away when I exit the container.
pi@raspberrypi:~ $ docker run --rm -it microsoft/dotnet:2.1-sdk root@063f3c50c88a:/# ls bin boot dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var root@063f3c50c88a:/# cd ~ root@063f3c50c88a:~# mkdir mytest root@063f3c50c88a:~# cd mytest/ root@063f3c50c88a:~/mytest# dotnet new console The template "Console Application" was created successfully. Processing post-creation actions... Running 'dotnet restore' on /root/mytest/mytest.csproj... Restoring packages for /root/mytest/mytest.csproj... Installing Microsoft.NETCore.DotNetAppHost 2.1.0-rc1. Installing Microsoft.NETCore.DotNetHostResolver 2.1.0-rc1. Installing NETStandard.Library 2.0.3. Installing Microsoft.NETCore.DotNetHostPolicy 2.1.0-rc1. Installing Microsoft.NETCore.App 2.1.0-rc1. Installing Microsoft.NETCore.Platforms 2.1.0-rc1. Installing Microsoft.NETCore.Targets 2.1.0-rc1. Generating MSBuild file /root/mytest/obj/mytest.csproj.nuget.g.props. Generating MSBuild file /root/mytest/obj/mytest.csproj.nuget.g.targets. Restore completed in 15.8 sec for /root/mytest/mytest.csproj. Restore succeeded. root@063f3c50c88a:~/mytest# dotnet run Hello World! root@063f3c50c88a:~/mytest# dotnet exec bin/Debug/netcoreapp2.1/mytest.dll Hello World!
If you try it yourself, you'll note that "dotnet run" isn't very fast. That's because it does a restore, build, and run. Compilation isn't super quick on these tiny devices. You'll want to do as little work as possible. Rather than a "dotnet run" all the time, I'll do a "dotnet build" then a "dotnet exec" which is very fast.
If you're doing to do Docker and .NET Core, I can't stress enough how useful the resources are over at https://github.com/dotnet/dotnet-docker .
I found the samples to be super useful...be sure to dig into the Dockerfiles themselves as it'll give you a ton of insight into how to structure your own files. Being able to do Multistage Dockerfiles is crucial when building on a small device like a RPi. You want to do as little work as possible and let Docker cache as many layers with its internal "smarts." If you're not thoughtful about this, you'll end up wasting 10x the time building image layers every build.
Can I take my podcast site and Dockerize it and build/test/run it on a Raspberry Pi? YES.
FROM microsoft/dotnet:2.1-sdk AS build WORKDIR /app # copy csproj and restore as distinct layers COPY *.sln . COPY hanselminutes.core/*.csproj ./hanselminutes.core/ COPY hanselminutes.core.tests/*.csproj ./hanselminutes.core.tests/ RUN dotnet restore # copy everything else and build app COPY . . WORKDIR /app/hanselminutes.core RUN dotnet build FROM build AS testrunner WORKDIR /app/hanselminutes.core.tests ENTRYPOINT ["dotnet", "test", "--logger:trx"] FROM build AS test WORKDIR /app/hanselminutes.core.tests RUN dotnet test FROM build AS publish WORKDIR /app/hanselminutes.core RUN dotnet publish -c Release -o out FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime WORKDIR /app COPY --from=publish /app/hanselminutes.core/out ./ ENTRYPOINT ["dotnet", "hanselminutes.core.dll"]
Love it. Now I can "docker build ." on my Raspberry Pi. It will restore, test, and build. If the tests fail, the Docker build will fail.
See how there's an extra section up there called "testrunner" and then after it is "test?" That testrunner section is a no-op. It sets an ENTRYPOINT but it is never used...yet. The ENTRYPOINT is an implicit run if it is the last line in the Dockerfile. That's there so I can "Run up to it" if I want to.
I can just build and run like this:
docker build -t podcast . docker run --rm -it -p 8000:80 podcast
NOTE/GOTCHA:Note that the "runtime" image is microsoft/dotnet:2.1-aspnetcore-runtime, not microsoft/dotnet:2.1-runtime. That aspnetcore one pre-includes the binaries I need for running an ASP.NET app, that way I can just include a single reference to "<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0-rc1-final" />" in my csproj. If didn't use the aspnetcore-runtime base image, I'd need to manually pull in all the ASP.NET Core packages that I want. Using the base image might make the resulting image files larger, but it's a balance between convenience and size. It's up to you. You can manually include just the packages you need, or pull in the "Microsoft.AspNetCore.App" meta-package for convenience. My resulting "podcast" image ended up 205megs, so not to bad, but of course if I wanted I could trim in a number of ways.
Or, if I JUST want test results from Docker , I can do this! That means I can run the tests in the Docker container, mount a volume between the Linux container and (theoretical) Window host, and then open the .trx resulting file in Visual Studio!
docker build --pull --target testrunner -t podcast:test . docker run --rm -v D:\github\hanselminutes-core\TestResults:/app/hanselminutes.core.tests/TestResults podcast:test
Check it out! These are the test results from the tests that ran within the Linux Container:
Here's the result. I've now got my Podcast website running in Docker on an ARM32 Raspberry Pi 3 with just an hours' work (writing the Dockerfile)!
Cross-platform for the win!
Sponsor: Check out JetBrains Rider: a cross-platform .NET IDE . Edit, refactor, test and debug ASP.NET, .NET Framework, .NET Core, Xamarin or Unity applications. Learn more and download a 30-day trial !