In this article I will describe step by step how to install Docker on a Raspberry Pi. Personally, I recommend using the Lite version of Raspbian so that more resources (such as RAM and CPU) are available.
After enabling SSH and carrying out the usual configuration via raspi-config, the first step is to run the update and upgrade commands:
sudo apt-get update && sudo apt-get upgrade -y
Now download and install Docker with the following commands:
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
The installation will take a few minutes to complete. Then run the following command to check the version currently installed:
sudo docker version
By default, only a user with administrator privileges can run Docker commands.
To run Docker commands as a non-root user (without prefixing them with sudo), you need to add the user to the docker group created during installation. To do so, type:
sudo usermod -aG docker $USER
$USER is an environment variable containing your username.
So if you are using the pi user, the command will be:
sudo usermod -aG docker pi
At this point I recommend restarting the Raspberry Pi with the usual command:
sudo reboot now
Now reconnect via SSH and, to check that you can run docker commands without sudo, run the following command, which downloads a test image, runs it in a container, prints a “Hello from Docker” message and exits:
docker run hello-world
Now that Docker is set up on the Raspberry Pi, let’s look at the basic Docker concepts and commands.
A Docker image is made up of a series of filesystem layers representing the instructions in the image’s Dockerfile that make up an executable software application. An image is an immutable binary file that includes the application and all its other dependencies, such as libraries, binaries and the instructions needed to run the application.
Most Docker images are available on Docker Hub. This is a cloud-based registry service which, among other things, is used to store Docker images in public or private repositories.
To search for an image in the Docker Hub registry, use the docker search command. For example, to search for a Debian image, type:
docker search debian
An instance of an image is called a container. A container represents a runtime for a single application, process or service.
It may not be the most accurate comparison, but if you are a programmer you can think of a Docker image as a class and a Docker container as an instance of a class.
To start, stop, remove and manage a container, use the docker container command. For example, the following command starts a Docker container based on the Debian image. If the image is not available locally, it will be downloaded first:
docker run debian
The Debian container will stop immediately after starting because it has no long-running process and no other command has been provided. The container started, ran an empty command and then exited.
The -it option lets you interact with the container via the command line. To start an interactive container, type:
docker run -it debian /bin/bash
Output:root@ee67tfc65c8c:/#
As you can see from the output above, once the container has started, the command prompt changes, which means you are now working from inside the container.
To list the running Docker containers, use the following command:
docker container ls
If you have no running containers, the output will be empty.
To view all containers, use the -a option:
docker container ls -a
To delete one or more containers, simply copy the container ID (or IDs) and paste them after the container rm command:
docker container rm c66980v5665r
Optionally, you can install the following packages to enable installing containers via Docker Compose
sudo apt-get install libffi-dev libssl-dev
sudo apt install python3-dev -y
sudo apt-get install -y python3 python3-pip
sudo pip3 install docker-compose
**** UPDATE ****
installing Docker and Docker Compose on a Raspberry Pi has recently become simpler.
Run the following command to install Docker
sudo apt-get install docker
and to install Docker Compose
sudo apt-get install docker-compose
to allow the $USER group to run containers
sudo usermod -aG docker $USER
Enjoy!
