Setup Docker on Raspberry Pi
CloudContainersHow ToRaspberry PiTech/WebTutorials/DIY

How to Set Up Docker on Raspberry Pi

Introduction

Docker is a lightweight containerization platform that allows you to run applications in isolated environments. Using Docker on a Raspberry Pi can enhance development flexibility, simplify deployments, and optimize resource utilization. This guide covers everything from installation to running your first container on a Raspberry Pi.

Read This: How to Install Raspberry Pi OS on Raspberry Pi

Prerequisites

Before installing Docker, ensure you have the following:

  • A Raspberry Pi (Raspberry Pi 4 recommended for better performance)
  • A microSD card (16GB or higher recommended)
  • Raspberry Pi OS (64-bit preferred for better compatibility with Docker images)
  • Internet connection
  • SSH access or physical access to the Raspberry Pi

Step 1: Update and Upgrade Your System

Before installing Docker, update the system packages:

sudo apt update && sudo apt upgrade -y

This ensures your system has the latest updates and security patches.

Step 2: Install Docker

The recommended way to install Docker on a Raspberry Pi is using the official script provided by Docker:

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

This script automatically detects your system architecture and installs the appropriate version of Docker.

Step 3: Add User to the Docker Group

By default, Docker requires root privileges. To run Docker commands without using sudo, add your user to the docker group:

sudo usermod -aG docker $USER

Apply the changes by logging out and logging back in, or use:

newgrp docker

Step 4: Verify Docker Installation

To check if Docker is installed correctly, run:

docker --version

This should display the installed Docker version.

You can also test Docker by running the Hello World container:

docker run hello-world

If Docker is working properly, you will see a message confirming the successful setup.

Step 5: Enable Docker to Start on Boot

To ensure Docker starts automatically after rebooting your Raspberry Pi, enable the service:

sudo systemctl enable docker

To start Docker immediately:

sudo systemctl start docker

Step 6: Running a Test Container

Now that Docker is installed, let’s run a sample container. The following command runs an Nginx web server:

docker run -d -p 80:80 --name webserver nginx
  • -d: Runs the container in detached mode.
  • -p 80:80: Maps port 80 of the container to port 80 on the Raspberry Pi.
  • --name webserver: Names the container webserver.
  • nginx: The official Nginx image from Docker Hub.

You can access the Nginx server by entering your Raspberry Pi’s IP address in a browser.

Step 7: Managing Docker Containers

List Running Containers

docker ps

To see all containers (including stopped ones):

docker ps -a

Stop a Running Container

docker stop webserver

Remove a Container

docker rm webserver

Remove Unused Images

To clean up unused images and free space:

docker image prune -a

Step 8: Installing Docker Compose

Docker Compose allows you to manage multi-container applications. Install it with:

sudo apt install -y python3-pip
sudo pip3 install docker-compose

Verify the installation:

docker-compose --version

Conclusion

You have successfully installed and configured Docker on your Raspberry Pi. With Docker, you can easily deploy applications, set up lightweight services, and manage containers efficiently. Start experimenting with different images and explore Docker’s powerful ecosystem to maximize the potential of your Raspberry Pi.

Troubleshooting

Docker Command Not Found

If Docker commands do not work, try restarting the system:

sudo reboot

Permission Denied When Running Docker

Ensure your user is in the docker group:

sudo usermod -aG docker $USER

Then log out and log back in.

Checking Docker Logs

If Docker is not working properly, check logs for errors:

sudo journalctl -u docker --no-pager

Now, you’re ready to build and deploy applications efficiently with Docker on your Raspberry Pi!

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

4 thoughts on “How to Set Up Docker on Raspberry Pi

Leave a Reply

Your email address will not be published. Required fields are marked *