Blog

Docker on a raspberry pi

25 Aug, 2014

This blog describes how easy it is to use docker in combination with a Raspberry Pi. Because of docker, deploying software to the Raspberry Pi is a piece of cake.
What is a raspberry pi?
The Raspberry Pi is a credit-card sized computer that plugs into your TV and a keyboard. It is a capable little computer which can be used in electronics projects and for many things that your desktop PC does, like spreadsheets, word-processing and games. It also plays high-definition video. A raspberry pi runs linux, has an ARM processor of 700 MHZ and internal memory of 512 MB. Last but not least, it only costs around  35 Euro.

A raspberry pi

A raspberry pi version B


Because of the price, size and performance, the raspberry pi is a step to the ‘Internet of things’ principle. With a raspberry pi it is possible to control and connect everything to everything. For instance, my home project which is an raspberry pi controlling a robot.

 

Raspberry Pi in action


What is docker?
Docker is an open platform for developers and sysadmins to build, ship and run distributed applications. With Docker, developers can build any app in any language using any toolchain. “Dockerized” apps are completely portable and can run anywhere. A dockerized app contains the application, its environment, dependencies and even the OS.
Why combine docker and raspberry pi?
It is nice to work with a Raspberry Pi because it is a great platform to connect devices. Deploying anything however, is kind of a pain. With dockerized apps we can develop and test our application on our own home machine, when it works we can deploy it to the raspberry. We can do this without any pain or worries about corruption of the underlying operating system and tools. And last but not least, you can easily undo your tryouts.
What is better than I expected
First of all; it was relatively easy to install docker on the raspberry pi. When you use the Arch Linux operating system, docker is already part of the package manager! I expected to do a lot of cross-compiling of the docker application, because the raspberry pi uses an ARM-architecture (instead of the default x86 architecture), but someone did this already for me!
Second of all; there are a bunch of ready-to-use docker-images especially for the raspberry pi. To run dockerized applications on the raspberry pi you are depending on the base images. These base images must also support the ARM-architecture. For each situation there is an image, whether you want to run node.js, python, ruby or just java.
The worst thing that worried me was the performance of running virtualized software on a raspberry pi. But it all went well and I did not notice any performance reduce. Docker requires far less resources than running virtual machines. A docker proces runs straight on the host, giving native CPU performance. Using Docker requires a small overhead for memory and network.
What I don’t like about docker on a raspberry pi
The slogan of docker to ‘build, ship and run any app anywhere’ is not entirely valid. You cannot develop your Dockerfile on your local machine and deploy the same application directly to your raspberry pi. This is because each dockerfile includes a core image. For running your application on your local machine, you need a x86-based docker-image. For your raspberry pi you need an ARM-based image. That is a pity, because this means you can only build your docker-image for your Raspberry Pi on the raspberry pi, which is slow.
I tried several things.

  1. I used the emulator QEMU to emulate the rasberry pi on a fast Macbook. But, because of the inefficiency of the emulation, it is just as slow as building your dockerfile on a raspberry pi.
  2. I tried cross-compiling. This wasn’t possible, because the commands in your dockerfile are replayed on a running image and the running raspberry-pi image can only be run on … a raspberry pi.

How to run a simple node.js application with docker on a raspberry pi  
Step 1: Installing Arch Linux
The first step is to install arch linux on an SD card for the raspberry pi. The preferred OS for the raspberry pi is a debian based OS: Raspbian, which is nicely configured to work with a raspberry pi. But in this case, the ArchLinux is better because we use the OS only to run docker on it. Arch Linux is a much smaller and a more barebone OS. The best way is by following the steps at http://archlinuxarm.org/platforms/armv6/raspberry-pi. In my case, I use version 3.12.20-4-ARCH. In addition to the tutorial:

  1. After downloading the image, install it on a sd-card by running the command:
    [code]sudo dd if=path_of_your_image.img of=/dev/diskn bs=1m[/code]
  2. When there is no HDMI output at boot, remove the config.txt on the SD-card. It will magically work!
  3. Login using root / root.
  4. Arch Linux will use 2 GB by default. If you have a SD-card with a higher capacity you can resize it using the following steps http://gleenders.blogspot.nl/2014/03/raspberry-pi-resizing-sd-card-root.html

Step 2: Installing a wifi dongle
In my case I wanted to connect a wireless dongle to the raspberry pi, by following these simple steps

  1. Install the wireless tools:
    [code]
    pacman -Syu
    pacman -S wireless_tool
    [/code]
  2. Setup the configuration, by running:
    [code]wifi-menu[/code]
  3. Autostart the wifi with:
    [code]
    netctl list
    netctl enable wlan0-[name]
    [/code]

Because the raspberry pi is now connected to the network you are able to SSH to it.
Step 3: Installing docker
The actual install of docker is relative easy. There is a docker version compatible with the ARM processor (that is used within the Raspberry Pi). This docker is part of the package manager of Arch Linux and the used version is 1.0.0. At the time of writing this blog docker release version 1.1.2. The missing features are

  1. Enhanced security for the LXC driver.
  2. .dockerignore support.
  3. Pause containers during docker commit.
  4. Add –tail to docker logs.

You will install docker and start is as a service on system boot by the commands:
[code]
pacman -S docker
systemctl enable docker
[/code]

Installing docker with pacman

Installing docker with pacman


Step 4: Run a single nodejs application
After we’ve installed docker on the raspberry pi, we want to run a simple nodejs application. The application we will deploy is inspired on the nodejs web in the tutorial on the docker website: https://github.com/enokd/docker-node-hello/. This nodejs application prints a “hello world” to the console of the webbrowser. We have to change the dockerfile to:
[code]
# DOCKER-VERSION 1.0.0
FROM resin/rpi-raspbian
# install required packages
RUN apt-get update
RUN apt-get install -y wget dialog
# install nodejs
RUN wget http://node-arm.herokuapp.com/node_latest_armhf.deb
RUN dpkg -i node_latest_armhf.deb
COPY . /src
RUN cd /src; npm install
# run application
EXPOSE 8080
CMD ["node", "/src/index.js"][/code]
And it works!
Screen Shot 2014-08-07 at 20.52.09

The webpage that runs in nodejs on a docker image on a raspberry pi


 
Just by running four little steps, you are able to use docker on your raspberry pi! Good luck!
 

guest
14 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Andrew Phillips
Andrew Phillips
7 years ago

Cool! As regards your comment about having to write Dockerfiles specifically for the pi because of the different core image, I can imagine that we’ll see “symlink”-type core images, or core images chosen by some dynamic lookup, pretty soon.
It’s something you could easily implement now with a bit of search’n’replace, too – the XL Deploy Docker plugin supports similar things, as far as I recall.
What kind of use cases do you envisage for this? An easy way to update the software running on the pi? Using pi’s “transparently” as a low-cost platform for enterprise apps? Other..?
Thanks for sharing!

Philipz
Philipz
7 years ago

Docker cross-build is cool issue~~~

Philipz
Philipz
7 years ago

How about use QEMU on PC to do ARM Docker build ???
http://xecdesign.com/qemu-emulating-raspberry-pi-the-easy-way/

Shaun
Shaun
7 years ago

you should check out resin.io , they have a platform to do all of this seamlessly with the code cross-compiled in the cloud.

Philipz
Philipz
7 years ago
dandroid
dandroid
7 years ago

typo: wireless_tools (plural)
thanks for the tutorial!

Jessica Hart
Jessica Hart
7 years ago

One week ago, we released our instructions to installing Docker on the Raspberry Pi.

foo
foo
6 years ago

Docker is not an emulator so i can’t comile on pc for pi.
if i crosscompile then docker is not needed for that.

Bruno Sant'Anna
Bruno Sant'Anna
6 years ago

Just in case,
If anyone give Ubuntu Snappy Core a Try, they also have a docker package under their repository, it worked fine here.
https://developer.ubuntu.com/en/snappy/tutorials/using-snappy/

Mathias Renner
Mathias Renner
6 years ago

We at Hypriot offer downloads of the latest Docker versions that are built for the Raspberry Pi.
Further we created a SD card image, which you can flash on a SD card and boot your Raspberry Pi from it. This will give you Docker on the Raspberry Pi out of the box.
Check it out at http://blog.hypriot.com/downloads

Harley King
Harley King
6 years ago

Thanks for taking the time to write this article. It’s timely as there’s a lot of interest in Docker and RPi’s.
I’m interested in your RPi robot project. Can you tell us more about that? Where’d you get those nice tank treads?

石樱灯笼
石樱灯笼
6 years ago

I think that it is not very necessary using docker on raspberry pi, because most of pi running the same or the similar OS(such as raspbian) so that the environment are same, not as X86_Server.
A matter of opinion.

fiedel
fiedel
6 years ago

Hi All,
When trying to build the image with the above Dockerfile, am getting the following,
$ docker build -t pi/node-web-app .
Sending build context to Docker daemon 4.096 kB
Step 1 : FROM resin/rpi-raspbian
Pulling repository docker.io/resin/rpi-raspbian
Error while pulling image: Get https://index.docker.io/v1/repositories/resin/rpi-raspbian/images: EOF
Can anyone on this? Thanks.

Explore related posts