By Jan Toebes
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.
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.
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.
- 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.
- 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:
- After downloading the image, install it on a sd-card by running the command:
sudo dd if=path_of_your_image.img of=/dev/diskn bs=1m
- When there is no HDMI output at boot, remove the config.txt on the SD-card. It will magically work!
- Login using root / root.
- 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:
pacman -Syu pacman -S wireless_tool
2. Setup the configuration, by running:
3. Autostart the wifi with:
netctl list netctl enable wlan0-[name]
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
- Enhanced security for the LXC driver.
- .Dockerignore support.
- Pause containers during Docker commit.
- Add –tail to Docker logs.
You will install Docker and start is as a service on system boot by the commands:
pacman -S Docker systemctl enable Docker
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:
# 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"]
And it works!
Just by running four little steps, you are able to use Docker on your Raspberry Pi! Good luck!