Installing and using Docker on Raspberry Pi

The following is a very basic intro to running applications inside Docker containers on a Raspberry Pi, intended for someone who has never used Docker before, but wants to try it out. This was also my first step into Docker on Pi, so I documented it as I went along.

There are a few different tutorials out there for installing Docker on a Raspberry Pi. I found this one works well, and follows the same basic instructions as is recommended for installing Docker on a Linux PC.

These are the commands I ran on the Pi (in case that site disappears):

sudo apt-get install apt-transport-https \
                       ca-certificates \

curl -fsSL | sudo apt-key add -

apt-key fingerprint 58118E89F3A912897C070ADBF76221572C52609D

sudo add-apt-repository \
       "deb \
       raspbian-$(lsb_release -cs) \

echo "deb raspbian-jessie main" | sudo tee -a /etc/apt/sources.list

sudo apt-get -y install docker-engine

You can test that it’s working with the following command:

sudo docker run hello-world

To start playing with putting your app in a Docker container, first create a file called Dockerfile with the following content:

FROM resin/rpi-raspbian

This simply means we want to create an image that inherits from the resin/rpi-raspbian image, which provides us with a basic Raspbian environment inside a container. (There may be better options out there for base images - this one worked quickly. Curious if others have experience or suggestions around this…)

Then, we’ll build it into a container image called “mycontainer”:

sudo docker build -t mycontainer .

When that’s done, you should see it in the list of images that are available on your system:

sudo docker images

To start up a new container based on that image, run:

sudo docker run --rm -it mycontainer

This puts you directly into a bash shell within the container.

I also tested a simple Python 3 container, which puts you into a Python shell. Just change your Dockerfile to:

FROM python:3

Then rebuild and run.

sudo docker build -t mycontainer .
sudo docker run --rm -it mycontainer

The above is just a very simple way to get started with Docker on a Raspberry Pi.

In most cases, you will want to build out your Dockerfile to include everything necessary to run your app, and set it up to auto-start the container on system boot.

The official documentation for Docker goes into a lot more detail. This page has a good example of how to set up a Flask server inside a Docker container: - which helps to demonstrate how a Dockerfile works and what the whole process of “Dockerizing” an app looks like.

To access the Pi’s GPIO within a Docker container, start with this simple Dockerfile:

FROM resin/rpi-raspbian
RUN apt-get update
RUN apt-get install -y wiringpi

That simply creates an image based on Raspbian, updates package repos, and installs wiringpi.

Build and run it with two extra arguments in docker run:

sudo docker build -t mycontainer .
sudo docker run --rm -it --cap-add SYS_RAWIO --device /dev/mem mycontainer

The extra arguments (--cap-add SYS_RAWIO --device /dev/mem) tell Docker to give the container access to the host system’s GPIO hardware directly.

Then, in the bash shell, read from the GPIO to test that it’s working:

gpio readall

To turn on GPIO pin 24:

gpio -g mode 24 out
gpio -g write 24 1

And off:

gpio -g write 24 0

Just running curl -sSL | sh from worked for me to get docker installed.

1 Like