Installing Docker on a Raspberry Pi

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

  1. Raspberry Pi: A Raspberry Pi 3, 4, or later is recommended.
  2. Operating System: Raspberry Pi OS (32-bit or 64-bit).
  3. Internet Connection: Ensure the Raspberry Pi is connected to the internet.
  4. Terminal Access: SSH into your Raspberry Pi or use a directly connected keyboard and monitor.
  5. 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

  1. Download and execute the Docker installation script:

    curl -fsSL https://get.docker.com -o get-docker.sh
    sudo sh get-docker.sh
    
  2. Add your user to the docker group to run Docker commands without sudo:

    sudo usermod -aG docker $USER
    
  3. 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

  1. 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
    
  2. Apply executable permissions to the binary:

    sudo chmod +x /usr/local/bin/docker-compose
    
  3. 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:

  1. Pull the Nginx image:

    docker pull nginx
    
  2. Run the Nginx container:

    docker run -d -p 80:80 --name webserver nginx
    
  3. 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.

  4. To stop the container, run:

    docker stop webserver
    
  5. To remove the container:

    docker rm webserver
    

Tips for Using Docker on Raspberry Pi

  1. Optimize for ARM Architecture: Ensure that the images you use are compatible with the ARM architecture (e.g., Raspberry Pi-specific images).
  2. 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
    
  3. Monitor Resources: Raspberry Pi has limited resources; monitor CPU and memory usage to avoid overloading.

Troubleshooting

  1. Docker Command Requires sudo: Ensure your user is added to the docker group:

    sudo usermod -aG docker $USER
    

    Then reboot the Raspberry Pi.

  2. Service Not Starting: Check the Docker service status:

    sudo systemctl status docker
    
  3. 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

  1. Hosting web applications (e.g., WordPress, Nextcloud)
  2. Running IoT platforms (e.g., Home Assistant, Node-RED)
  3. Building development environments
  4. 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!

Leave a comment

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.