How to Install and Use Docker on Raspberry Pi

Ben
Ben
@benjislab

Docker is a powerful tool that allows you to run applications in lightweight, isolated containers. This can be particularly useful on a Raspberry Pi, enabling you to run multiple services and applications efficiently. This guide will walk you through the steps to install and use Docker on your Raspberry Pi.

Prerequisites

Before starting, ensure you have the following:

  • Raspberry Pi (preferably Raspberry Pi 3 or later for better performance)
  • MicroSD card with Raspberry Pi OS installed
  • Stable internet connection
  • Access to the command line (via monitor and keyboard or SSH)

Step 1: Update and Upgrade Your Raspberry Pi

First, make sure your Raspberry Pi is up to date.

  1. Open a terminal on your Raspberry Pi or SSH into it.
  2. Run the following commands to update and upgrade your system:
sudo apt update
sudo apt upgrade -y

Step 2: Install Docker

  1. Download and Run the Docker Installation Script:

Docker provides a convenient installation script for installing Docker on Linux systems, including Raspberry Pi OS.

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
  1. Add Your User to the Docker Group:

=This step allows you to run Docker commands without needing to use sudo.

=sudo usermod -aG docker $USER
  1. Log Out and Back In:

Log out and back in to apply the group changes. Alternatively, you can reboot your Raspberry Pi:

sudo reboot

Step 3: Verify Docker Installation

  1. Check Docker Version:

After logging back in, verify that Docker is installed correctly by checking its version:

docker --version
  1. Run a Test Container:

Run a simple test container to ensure Docker is working correctly:

docker run hello-world

You should see a message confirming that Docker is installed and working properly.

Step 4: Using Docker on Raspberry Pi

Pulling Docker Images

Docker images are pre-configured environments that you can run as containers. You can find many pre-built images on Docker Hub.

  1. Search for an Image:

Search for a specific image on Docker Hub. For example, to find an Nginx image:

docker search nginx
  1. Pull an Image:

Pull the desired image from Docker Hub. For example, to pull the Nginx image:

docker pull nginx

Running Docker Containers

  1. Run a Container:

Run a container from the pulled image. For example, to run an Nginx container:

docker run -d -p 80:80 --name webserver nginx

This command runs an Nginx container in detached mode, maps port 80 on your Raspberry Pi to port 80 on the container, and names the container "webserver".

  1. List Running Containers:

List all running containers:

docker ps
  1. Stop and Remove Containers:

Stop a running container:

docker stop webserver

Remove a stopped container:

docker rm webserver

Managing Docker Containers

  1. View Container Logs:

View the logs of a running container:

docker logs webserver
  1. Access a Container's Shell:

Access the shell of a running container for debugging or configuration:

docker exec -it webserver /bin/bash

Step 5: Setting Up Docker Compose (Optional)

Docker Compose allows you to define and manage multi-container Docker applications. This can be useful for more complex setups.

  1. Install Docker Compose:

Download and install Docker Compose using the following commands:

sudo apt install -y python3-pip
sudo pip3 install docker-compose
  1. Verify Docker Compose Installation:

Check the Docker Compose version:

docker-compose --version
  1. Create a docker-compose.yml File:

Define your multi-container setup in a docker-compose.yml file. For example, to run a simple web server with Nginx and Redis:

version: '3'
services:
   web:
      image: nginx
      ports:
        - "80:80"
   redis:
      image: redis
  1. Start Your Multi-Container Application:

Navigate to the directory containing your docker-compose.yml file and run:

docker-compose up -d

This command starts all the services defined in the docker-compose.yml file in detached mode.

Conclusion

By installing Docker on your Raspberry Pi, you can easily run and manage applications in isolated containers, making your development process more efficient and streamlined. Docker's versatility and lightweight nature make it an excellent tool for maximizing the capabilities of your Raspberry Pi. Follow these steps to get Docker up and running, and start exploring the vast world of containerized applications.