How To Install and Use Docker on Raspberry Pi

Posted 

4 min read

Docker is a containerization platform that allows you to quickly build, test and deploy applications as portable, self-sufficient containers that can virtually run everywhere.

In this tutorial, we will explain how to install Docker on Raspberry Pi and explore the basic Docker concepts and commands.

Prerequisites

We’re assuming that you have Raspbian installed on your Raspberry Pi. Docker doesn’t need a graphical interface and it is best is to use the Raspbian Stretch Lite image and enable SSH. This way your Raspberry Pi will have much more available processing power and memory to run the Docker containers.

Install Docker on Raspberry Pi

Installing Docker on Raspberry Pi is just a matter of running a few commands.

First, download the Docker installation script using the following curl command:

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

Once the download is complete, execute the script by typing:

sh get-docker.sh 

The script will detect the Linux distribution, install the required packages, and start Docker.

The process may take a few minutes and when it is completed the script will output information about Docker version and how to use Docker as a non-root user.

That’s it. Docker has been installed on your Pi board.

Executing the Docker Command Without Sudo

By default, only a user with administrative privileges can execute Docker commands.

To run Docker commands as a non-root user without prepending sudo you’ll need to add your user to the docker group which is created during the installation. To do that by type:

sudo usermod -aG docker $USER

$USER is an environment variable that holds your username.

Log out and log back in so that the group membership is refreshed.

To verify that you can run docker commands without sudo run the following command which will download a test image, run it in a container, print a “Hello from Docker” message and exit:

docker container run hello-world

The output will look like something like this:

How to use Docker

Now that Docker is set up on your Raspberry Pi, let’s go over the basic docker concepts and commands.

Docker Images

A Docker image is made up of a series of filesystem layers representing instructions in the image’s Dockerfile that make up an executable software application. An image is an immutable binary file including the application and all other dependencies such as libraries, binaries, and instructions necessary for running the application.

Most Docker images are available on Docker Hub. It is a cloud-based registry service which among other functionalities is used for keeping the Docker images either in a public or private repository.

To search for an image from the Docker Hub registry, use the docker search command. For example, to search for a Debian image, you would type:

docker search debian

Docker Containers

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 appropriate comparison but if you are a programmer you can think of a Docker image as class and 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 will start a Docker container based on the Debian image. If you don’t have the image locally, it will be downloaded first:

docker container run debian

The Debian container will stop immediately after booting up because it does not have a long-running process and no other command is provided. The container booted up, ran an empty command and then exited.

The switch -it allows you to interact with the container through the command line. To start an interactive container type:

docker container run -it debian /bin/bash
root@ee86c8c81b3b:/#

As you can see from the output above once the container is started the command prompt is changed which means that you’re now working from inside the container.

To list active Docker containers, use the following command:

docker container ls

If you don’t have any running containers the output will be empty.

To view both active and inactive containers, pass it the -a switch:

docker container ls -a

To delete one or more containers just copy the container ID (or IDs) and paste them after the container rm command:

docker container rm c55680af670c

Conclusion

You have learned how to install Docker on your Raspberry Pi machine and how to run Docker containers. For more information about this topic check the official Docker documentation.

If you have any question, please leave a comment below.