Getting Started with Docker on the World’s Most Popular Single Board Computer
Learn how to install Docker on your Raspberry Pi 4 and use it to run containers for a wide range of applications and projects. …
Updated August 17, 2023
Learn how to install Docker on your Raspberry Pi 4 and use it to run containers for a wide range of applications and projects.
Introduction
Docker is an open-source containerization platform that allows developers to package their applications with all its dependencies into lightweight, portable containers. It’s becoming increasingly popular in the industry as it enables easy deployment, scaling, and management of applications across different platforms and cloud providers. Docker has been around for a while now and is widely supported on various hardware platforms including Raspberry Pi. In this article, we will guide you through the process of installing Docker on your Raspberry Pi 4 and run your first containerized application.
Prerequisites
Before proceeding with the installation, make sure you have the following:
- A Raspberry Pi 4 Model B or later
- An SD card with Raspbian installed (preferably the latest version)
- An internet connection on your Raspberry Pi
- An SSH client to connect to your Raspberry Pi (optional but recommended if you’re not using a screen and keyboard directly connected to the device)
Step 1: Update Your System
To ensure that your system is up-to-date, run the following commands in the terminal:
sudo apt update
sudo apt upgrade
Step 2: Install Docker Dependencies
Docker requires some dependencies to be installed on your Raspberry Pi. Run the following command to install them:
sudo apt install -y curl gnupg2 ca-certificates lsb-release software-properties-common apt-transport-https
Step 3: Add Docker’s GPG Key and Repository
Next, add Docker’s official GPG key and repository to your system. Run the following commands one by one:
curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg | sudo gpg --batch --yes --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Step 4: Install Docker
Now, you can install the latest version of Docker using the following command:
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
Step 5: Start and Enable Docker Service
To start the Docker service and make it run automatically on boot, use the following commands:
sudo systemctl enable --now docker
Step 6: Verify Docker Installation
You can verify that Docker is installed correctly by running the hello-world container. Run this command in your terminal:
docker run hello-world
If everything goes well, you should see a message indicating that the container has been created and ran successfully.
Step 7: Run Your First Container
To run your first Docker container, let’s use the official Nginx image. Run this command:
docker run -d -p 80:80 nginx
This will download the latest Nginx image and start a container running in detached mode with port 80 mapped to the host’s port 80. Open your web browser and enter the IP address of your Raspberry Pi to see the default Nginx welcome page. If everything is working correctly, you should see a “Welcome to nginx” message.
Conclusion
Congratulations! You have successfully installed Docker on your Raspberry Pi 4 and ran your first containerized application. With Docker, you can now package any application or project into lightweight containers and deploy them on your device or any other platform that supports Docker. Happy coding!