
After using Docker on a ‘normal’ machine I was wondering if Docker would work on a Raspberry Pi. Using Docker on the Raspberry Pi was quite simple and the required steps are described in this article. The only disadvantage is that you cannot run ‘standard’ images like the ones I created in previous blogs. The Raspberry Pi has an ARM architecture. That means that you have to choose an image that works on ARM like the Raspbian image I used in this example.
Installing Arch Linux on a SD card
For more information see: Arch Linux installation guide
- Download Arch Linux latest version
- Unzip the Arch Linux file
- Copy Arch Linux using the following command (replace the path and the sdX):
dd bs=1M if=/path/to/ArchLinuxARM*.img of=/dev/sdX - Optional: increase the partition. Default only about 2GB of the SD card is used
- Remove the sd card from the computer and insert it into the Raspberry Pi
- Start the Raspberry Pi
- Optional: login, username and password are both ‘root’
SSH to the Raspberry Pi
Keyboard, mouse and screen are not necessary, the steps below describe how the Raspberry Pi can be used with just an ethernet and a power cable.
- Execute the command ‘arp -a’ on your computer it will give you some output like the following: alarmpi.local (RASPBERRYIPADDRESS)
- Execute the command ‘ssh root@RASPBERRYIPADDRESS’ with password ‘root’
- You can now execute commands on your Raspberry Pi
Install Docker
Update the repositories:
pacman -Syy
Install Docker:
pacman -S docker
Start Docker service
Before using Docker the service should be started. If you forget to start the service you will get an error like the following: ‘dial unix /var/run/docker.sock: no such file or directory’.
Starting the service:
systemctl start docker
Start the service on system boot:
systemctl enable docker
Create a Dockerfile
Standard images such as the Ubuntu images I used in previous blogs don’t work on the Raspberry Pi. Create an image that is compatible with the ARM architecture of the Raspberry Pi. The following script creates a container based on Raspbian with OpenJDK, Tomcat and a sample application.
FROM resin/rpi-raspbian RUN apt-get update RUN apt-get install -y openjdk-7-jre-headless wget RUN wget -O /tmp/tomcat7.tar.gz http://mirror.cogentco.com/pub/apache/tomcat/tomcat-7/v7.0.52/bin/apache-tomcat-7.0.52.tar.gz RUN (cd /opt && tar zxf /tmp/tomcat7.tar.gz) RUN (mv /opt/apache-tomcat* /opt/tomcat) ENV JAVA_HOME /usr/lib/jvm/java-1.7.0-openjdk-armhf RUN wget http://www.slashdot.org -P /opt/tomcat/webapps/slashdot RUN wget http://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.war -P /opt/tomcat/webapps EXPOSE 8080 CMD ["/opt/tomcat/bin/catalina.sh", "run"]
Create and run the Docker container
Create the container:
docker build -t tomcat .
Run the container:
docker run -p 8080:8080 -d tomcat
The Raspberry Pi is not quick, so wait a couple of minutes and grab a coffee. After that you can see the application working on http://RASPBERRYIPADDRESS:8080/slashdot/ or http://RASPBERRYIPADDRESS:8080/sample/.
Conclusion
Virtual machines are not an option as the resources of the Raspberry Pi are limited. Using Docker gives us the advantages of virtual machines like security, separation of concerns and ease of distribution. On the other side using Docker requires far less resources than running virtual machines. Docker processes run straight on the host giving native CPU performance. Using Docker requires a small overhead for memory and network. For more information about the impact of Docker on theresources see Docker introduction. This makes Docker a very interesting tool to use on the Raspberry Pi!
2 comments
Interesting post Johan. You mention just at the end that running the container takes a couple of minutes. Given that the container was built in the previous step, I’m surprised that the run takes so long. Is this a startup time (i.e. once up it runs much as you’d expect a Pi to run) or is there a noticable slowdown overall due to having docker underneath the apps inside the container?
Thanks and keep up the good work 🙂
Giovanni
Good question, in short Docker is not the reason for the slow performance. A Docker container starts in milliseconds. Starting Tomcat however takes some time, on my laptop 2-3 seconds. But the Raspberry Pi is a lot slower and takes about 3 minutes (Tomcat default) to start. This can be improved, removing the standard apps from Tomcat already reduces the startup time to about 2 minutes. So the run time of the container includes starting Tomcat and the application which takes quite some time. I didn’t notice any significant overhead with Docker.
If you’re interested in this topic you can also have a look at my latest blog. It includes a presentation from JavaOne where I gave a presentation about Docker on the Raspberry Pi.
Johan Janssen