Docker is a lightweight containerization platform that allows you to run and manage applications in isolated environments. Installing Docker on a Raspberry Pi enables you to use this powerful tool for a wide range of projects, from hosting web applications to running IoT services. This guide walks you through installing Docker on a Raspberry Pi.
Prerequisites
- Raspberry Pi: A Raspberry Pi 3, 4, or later is recommended.
- Operating System: Raspberry Pi OS (32-bit or 64-bit).
- Internet Connection: Ensure the Raspberry Pi is connected to the internet.
- Terminal Access: SSH into your Raspberry Pi or use a directly connected keyboard and monitor.
-
Updated OS: Ensure your Raspberry Pi OS is up to date by running:
sudo apt update && sudo apt upgrade -y
Step 1: Install Docker
Docker provides an official convenience script for quick installation.
Using the Convenience Script
-
Download and execute the Docker installation script:
curl -fsSL https://get.docker.com -o get-docker.sh sudo sh get-docker.sh
-
Add your user to the
docker
group to run Docker commands withoutsudo
:sudo usermod -aG docker $USER
-
Reboot your Raspberry Pi or log out and log back in for the group changes to take effect:
sudo reboot
Verify the Installation
After rebooting, verify that Docker is installed and running:
docker --version
You should see the Docker version displayed.
Step 2: Test Docker Installation
Run a test container to ensure Docker is functioning correctly:
docker run hello-world
This command downloads a test image and runs it in a container. If successful, you will see a "Hello from Docker!" message.
Step 3: Install Docker Compose (Optional)
Docker Compose is a tool for defining and running multi-container Docker applications.
Install Docker Compose
-
Download the latest Docker Compose binary:
sudo curl -L "https://github.com/docker/compose/releases/download/v2.19.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
-
Apply executable permissions to the binary:
sudo chmod +x /usr/local/bin/docker-compose
-
Verify the installation:
docker-compose --version
You should see the Docker Compose version displayed.
Step 4: Enable Docker to Start at Boot
To ensure Docker starts automatically when your Raspberry Pi boots, enable the Docker service:
sudo systemctl enable docker
You can check the status of the Docker service with:
sudo systemctl status docker
Step 5: Run a Sample Docker Application
Let’s run a simple web server to demonstrate Docker’s functionality:
-
Pull the Nginx image:
docker pull nginx
-
Run the Nginx container:
docker run -d -p 80:80 --name webserver nginx
-
Open a browser on your Raspberry Pi or another device on the same network and go to
http://<raspberry-pi-ip>
. You should see the Nginx welcome page. -
To stop the container, run:
docker stop webserver
-
To remove the container:
docker rm webserver
Tips for Using Docker on Raspberry Pi
- Optimize for ARM Architecture: Ensure that the images you use are compatible with the ARM architecture (e.g., Raspberry Pi-specific images).
-
Use Portainer for Management: Install Portainer to manage Docker containers via a web interface:
docker volume create portainer_data docker run -d -p 9000:9000 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest
- Monitor Resources: Raspberry Pi has limited resources; monitor CPU and memory usage to avoid overloading.
Troubleshooting
-
Docker Command Requires
sudo
: Ensure your user is added to thedocker
group:sudo usermod -aG docker $USER
Then reboot the Raspberry Pi.
-
Service Not Starting: Check the Docker service status:
sudo systemctl status docker
-
Permission Denied: Ensure the
/usr/local/bin/docker-compose
file has executable permissions:sudo chmod +x /usr/local/bin/docker-compose
Applications of Docker on Raspberry Pi
- Hosting web applications (e.g., WordPress, Nextcloud)
- Running IoT platforms (e.g., Home Assistant, Node-RED)
- Building development environments
- Learning containerization and orchestration
Conclusion
Installing Docker on a Raspberry Pi opens up a world of possibilities for deploying lightweight, isolated applications. By following this guide, you can set up Docker and Docker Compose to manage containers and explore the potential of containerized environments on your Raspberry Pi. Start experimenting with Docker to unlock new capabilities for your projects!