Docker on Raspberry Pi - Installation Guide


Docker has become an indispensable tool for developers, allowing them to easily create, deploy, and run applications in containers. While Docker is commonly used on desktops and servers, it can also be used on embedded devices such as the Raspberry Pi. In this article, we will go through the steps to install Docker on a Raspberry Pi and start using it.

Hardware Requirements

Before we begin, let's take a look at the hardware requirements for running Docker on a Raspberry Pi. Docker can be run on any Raspberry Pi model, but the performance will vary depending on the model. For example, the Raspberry Pi 4 with 4GB or 8GB of RAM will provide better performance than the Raspberry Pi 3B+.

Here are the recommended hardware requirements for running Docker on a Raspberry Pi −

  • Raspberry Pi 4 with 2GB or more RAM

  • MicroSD card with at least 16GB of storage

  • Power supply with at least 3A output

  • Ethernet cable or Wi-Fi dongle for network connectivity

  • Installing Docker on Raspberry Pi

Now that we have our hardware ready, let's move on to installing Docker on our Raspberry Pi.

Step 1: Update and Upgrade Raspberry Pi

Before we install Docker, it's a good idea to update and upgrade the Raspberry Pi to ensure that we have the latest software and security patches. To do this, run the following commands −

sudo apt-get update
sudo apt-get upgrade

Step 2: Install Docker

To install Docker on the Raspberry Pi, we need to run the following command −

curl -sSL https://get.docker.com | sh

This command will download and install the latest version of Docker on the Raspberry Pi.

Step 3: Add User to Docker Group

By default, the Docker daemon will run as the root user, which can be a security risk. To avoid this, we can add our user to the Docker group, which will allow us to run Docker commands without sudo.

To add the current user to the Docker group, run the following command −

sudo usermod -aG docker $USER

Step 4: Verify Docker Installation

To verify that Docker has been installed correctly, we can run the following command −

docker run hello-world

This command will download a small Docker image and run a container based on that image. If everything is working correctly, we should see a message that says "Hello from Docker!".

Using Docker on Raspberry Pi

Now that we have Docker installed on our Raspberry Pi, we can start using it to deploy and run containers.

To run a container, we first need to pull an image from a Docker registry. Docker Hub is the most popular registry and has a large number of pre-built images that we can use.

For example, let's say we want to run a container that runs a simple web server. We can do this by pulling the "nginx" image from Docker Hub and running a container based on that image.

Step 1: Pull the Nginx Image

To pull the Nginx image from Docker Hub, run the following command −

docker pull nginx

This command will download the latest version of the Nginx image from Docker Hub.

Step 2: Run the Nginx Container

To run a container based on the Nginx image, run the following command −

docker run -d -p 80:80 nginx

This command will run a container in detached mode (-d) and map port 80 of the container to port 80 on the Raspberry Pi (-p 80:80).

We can now access the Nginx web server by opening a web browser and navigating to the IP address of the Raspberry Pi. If you're unsure what the IP address is, you can use the following command to find out −

hostname -I

This will output the IP address of the Raspberry Pi, which you can then use to access the Nginx web server.

Step 3: Create a Dockerfile

While running containers from pre-built images is convenient, it's not always possible to find an image that meets your specific needs. In such cases, you can create your own Docker image by creating a Dockerfile.

A Dockerfile is a text file that contains a set of instructions for building a Docker image. You can use a Dockerfile to specify the base image, add files and directories to the image, set environment variables, and more.

Here's an example Dockerfile that builds an image based on the latest version of Alpine Linux and installs Nginx −

FROM alpine:latest

RUN apk update &&
apk add nginx &&
mkdir -p /run/nginx

COPY index.html /usr/share/nginx/html/

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

Let's break down each line of the Dockerfile −

  • FROM alpine:latest specifies the base image to use, in this case the latest version of Alpine Linux.

  • RUN apk update && apk add nginx && mkdir -p /run/nginx runs three commands in sequence −

  • apk update updates the package manager and package list.

  • apk add nginx installs the Nginx web server.

  • mkdir -p /run/nginx creates a directory for Nginx to store runtime data.

  • COPY index.html /usr/share/nginx/html/ copies the index.html file to the /usr/share/nginx/html/ directory in the Docker image.

  • EXPOSE 80 specifies that port 80 should be exposed to the host machine.

  • CMD ["nginx", "-g", "daemon off;"] specifies the command to run when the container starts, in this case starting the Nginx web server and running it in the foreground.

Step 4: Build the Docker Image

To build a Docker image from the Dockerfile, navigate to the directory containing the Dockerfile and run the following command −

docker build -t my-nginx-image .

This command will build an image with the tag my-nginx-image and use the current directory as the build context. The period at the end of the command specifies the build context.

The build process can take some time, depending on the size of the base image and the complexity of the Dockerfile.

Step 5: Run the Custom Docker Image

Once the Docker image has been built, you can run a container based on the image using the following command −

docker run -d -p 80:80 my-nginx-image

This command will run a container in detached mode (-d) and map port 80 of the container to port 80 on the Raspberry Pi (-p 80:80).

You can now access the Nginx web server by opening a web browser and navigating to the IP address of the Raspberry Pi, just as you did in step 2.

Step 6: Clean Up

To stop and remove the container, run the following command −

docker stop CONTAINER_ID && docker rm CONTAINER_ID

Replace CONTAINER_ID with the ID of the container you want to stop and remove. You can use the docker ps command to view a list of running containers and their IDs.

To remove the Docker image, run the following command −

docker rmi my-nginx-image

Replace my-nginx-image with the tag of the Docker image you want to remove. You can use the docker images command to view a list of Docker images and their tags.

Conclusion

Docker provides a convenient and efficient way to package, distribute, and run software applications, and it's especially useful when working with resource-constrained devices like Raspberry Pi. In this guide, we've shown you how to install Docker on a Raspberry Pi and run your first container.

With Docker, you can easily deploy and manage a wide range of applications, from web servers to databases and beyond, all within a containerized environment. This makes it easy to manage dependencies, avoid conflicts, and ensure consistency across different environments.

In addition to the basic installation steps, we've also covered some best practices for working with Docker on a Raspberry Pi, including optimizing container performance and managing container logs.

As you continue to work with Docker on a Raspberry Pi or other devices, keep these best practices in mind to ensure that your containers are secure, reliable, and performant.

Updated on: 28-Jun-2023

651 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements