It has been more than a year that I am having raspberry pi 4. I initially purchased for robotics, but I didn’t get chance to work on it. I started on my project recently, which can help me to improve my productivity. So I thought, I can use Raspberry Pi 4 as my testing server. Where I can use it for my self without turning on the computer before I make it publicly available.
My intention was to containerizing the .NET Core Web API in Raspberry pi 4. In this article I am going to put steps to setup docker and containerizing the .net core application.
In my case, there Rasbian was installed on my Raspberry Pi 4. When I bought I did all myself and created YouTube video for same. If you interested please watch this video here.
Sudo apt-get update
This will update the libraries in raspbian Operating System.
2. Upgrade the OS
Suod apt-get upgrade
Above command will upgrade the operating system with latest libraries. It’ll take atleast 5-10m to complete whole process.
We need to run below command, it will download the shell script on the raspberry pi. We will execute that shell script after that. I’ll will install the docker on the raspberry pi.
pi@raspberrypi: $ curl -fsSL https://get.docker.com -o get-docker.sh pi@raspberrypi: $ sudo get-docker.sh
This process will install the docker, It’ll take couple of minutes. To verify whether docker is installed, we need to run below command. This command will tell, how many docker images are running on the device.
pi@raspberrypi: $ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
As we just installed the docker, obviously there is no image is running.
Another important step is require to add user pi into docker. So “pi” user can start/stop and control the docker.
pi@raspberry: $ sudo usermod -aG docker pi
In this article I am using sample web application created using .net core template. How the Dockerfile looks like.
so here we are using base image of arm32v7 “mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim-arm32v7” (line 1 on above docker file code. And creating build with mcr.microsoft.com/dotnet/sdk:5.0. Using above docker file we create docker build which can be used on raspberry pi 4.
Note: We are not going to build/compile image on docker itself. Rather we will build it dev machine and push to docker hub. We will download image from docker hub on raspberry pi 4 and run the image.
Followings are the steps to build and push image to docker hub.
$ docker login
login to docker is require to build the docker image. Below command is used for build:
$ docker build -t nityap1979/testwebapp .
Next we will push the docker image to docker hub
$ docker push nityap1979/testwebapp Here is the screenshot once it is uploaded to docker hub.
We will login to the raspberry pi from our laptop/desktop. Download the image from docker hub.
$ docker pull nityap1979/testwebapp
Start the docker image once pull completed.
pi@raspberrypi: $ sudo docker -d -p5000:80 nityap1979/testwebapp
docker image we have opened the port 80 and we are mapping raspberry pi’s port 5000 with container’s port 80. In this case.
Now application can be accessed with raspberry pi IP address and port 5000.