Docker Containers on Raspberry Pi

Thanks to the work pioneered by the Hypriot team, Docker now runs on ARM processors.

This is totally great because then we can use Docker on the Raspberry Pi!

In this tutorial we’ll see how easy we can deploy and manage Docker containers on our Pi using Space.sh.

All commands below are using Space.sh and the Space modules.
If you are not working directly on the Pi, using a monitor or logged in via SSH or VNC, this is fine. We will wrap each command using the Space SSH module so that it will be run on the Pi instead.

Install Space.sh

Install Space.sh on your computer or on the Pi with the following command:

curl https://get.space.sh | sudo sh

1. Install Docker and Docker Compose

While working with containers on Raspberry Pi, we really recommend using HypriotOS as the go to Operating System, since it comes prepackaged with Docker Engine and Docker Compose.

On Raspbian, installing Docker Engine is fairly straightforward but installing Docker Compose is not as easy just yet.

So, from here on we expect the OS to be HypriotOS.

Docker Examples

List all running containers

space -m docker /ls_by_status/ -e status=Up
dc884dbfc5c5

This gives a list of running container identifiers, suitable to be used with xargs, for instance.

Removing by status

Removing all containers that are marked with status exit, as a way of cleaning up:

space -m docker /rm_by_status/ -e status=Exited
ced32d77adef

Remote execution and wrapping

All the commands mentioned so far will run docker where they are executed (as expected).
However, if we want to run this remotely on the Pi over SSH, we will tell Space.sh that it is to wrap the command in SSH and execute it on the remote target.
We do this by adding -m ssh /wrap/ -e SSHHOST=IP -e SSHUSER=pi to the command, like so:

space -m ssh /wrap/ -e SSHHOST=192.168.10.15 -e SSHUSER=pi -m docker /ls_by_status/ -e status=Up

Serving static content via a web server

Suppose the static contents are located in the www directory, similar to the following commands:

mkdir www
echo "Hello World" > www/index.html

We use Space.sh to run and serve that content leveraging an existing Nginx Docker container:

space -m docker /run/ -e image=nginx -e flags="-v /home/space/www/:/usr/share/nginx/html:ro -p 8086:80"

The first flag -v mounts a read-only volume for the container to access, while the second flag maps the port 8086 to 80, where the server will be running. It is now possible to browse to http://localhost:8086 or simply curl http://localhost:8086/index.html to acess the “Hello World” index file.

The same concept applies while maintaining and accessing the Pi via SSH: one can always wrap the Space command calls using the -m ssh /wrap/ construct and leverage remote execution for even faster turnaround and automation. The same command could then be expressed in a single statement:

space -m ssh /wrap/ -e SSHHOST=192.168.10.15 -e SSHUSER=pi -m docker /run/ -e image=nginx -e flags="-v /home/pi/www/:/usr/share/nginx/html:ro -p 8086:80"

For more Docker, Docker-Compose, Docker volumes examples, see the Space.sh Docker modules space-sh/docker, space-sh/docker-compose, space-sh/docker-volumes