Dockerizing your Raspberry Pi 4 - A step-by-step guide to setting up Docker on Raspberry Pi 4

This article will walk you through the process of installing Docker on a Raspberry Pi 4. We’ll cover everything from hardware requirements, OS setup, and installation of Docker itself, as well as some …


Updated August 16, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


This article will walk you through the process of installing Docker on a Raspberry Pi 4. We’ll cover everything from hardware requirements, OS setup, and installation of Docker itself, as well as some tips for running Docker containers on an ARM-based device like the Raspberry Pi.

Prerequisites

Before we get started, you will need to make sure that your Raspberry Pi 4 meets the following requirements:

  1. A Raspberry Pi 4 Model B (8GB or higher recommended)
  2. MicroSD card with Raspbian Buster Lite or Ubuntu Server 20.04 installed
  3. Power supply and access to a monitor, keyboard, and internet connection

Setting up the OS on your Raspberry Pi 4

To begin with, we need to set up the operating system on our Raspberry Pi. We’ll be using the latest version of Raspbian Buster Lite as it is lightweight and suitable for headless installations (without a monitor or keyboard). To download and flash Raspbian Buster Lite onto your MicroSD card, follow these steps:

  1. Download the latest version of Raspbian Buster Lite from here.
  2. Use a tool like Etcher to flash the downloaded image onto your MicroSD card.
  3. Once the image is flashed, insert the MicroSD card into your Raspberry Pi and power it on.
  4. Connect your Raspberry Pi to a network using an Ethernet cable or by configuring WiFi in the wpa_supplicant.conf file located on the boot partition of the SD card.

Enable hardware acceleration for Docker (optional)

If you want to enable hardware acceleration for Docker containers, you’ll need to install additional packages and configure your kernel. This is not necessary but can improve performance for certain workloads. To do this, run the following commands:

sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install -y --no-install-recommends \
    dkms \
    linux-modules-extra-raspi \
    linux-firmware \
    linux-headers-raspi

Install Docker on Raspberry Pi 4

Now that our OS is set up, we can proceed to install Docker. First, update the package lists and install prerequisite packages:

sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg2 \
    software-properties-common

Next, add the Docker GPG key and repository:

curl -fsSL https://download.docker.com/linux/raspbian/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=armhf] https://download.docker.com/linux/raspbian buster stable"

Update the package lists again and install Docker:

sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install -y docker-ce

Verify Docker installation

To verify that Docker is installed correctly, run the following command:

sudo docker run hello-world

If everything has gone well, you should see output similar to this:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:7f7450afccb4a1e1d769063844dc60031f7c752e5cd82ca2038bee5d2df5dbc00760
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

Running Docker containers on Raspberry Pi 4

Now that you have Docker installed, you can start running containers! To pull an image and run a container, use the following command:

sudo docker run -d --name my-container nginx:latest

This will download the latest version of Nginx and run it in a container named “my-container” in detached mode. You can check if your container is running by using:

sudo docker ps

To stop or start the container, use:

sudo docker stop my-container
sudo docker start my-container

Conclusion

Congratulations! You have now successfully installed Docker on your Raspberry Pi 4. With this powerful tool at your disposal, you can run applications and workloads in containers with ease. Keep in mind that some images may not be available for the ARM architecture, but you can always build your own or use a pre-built image from a similar architecture (e.g., arm32v7 instead of amd64). Happy containerizing!