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
Update the repositories:
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/.
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!