Docker on Raspberry Pi

Published on Tuesday, May 31, 2016

Docker allow us to package our application with all its dependencies into a standardized unit; the application run in the Container that has everything it needs to run and this is kept in isolation from the other Container running on the Server. Its architecturally different from Virtual Machine and are more portable and efficient; they share the kernel and run as an isolated process in user space on the host operating system.

image

To run Docker on Raspberry Pi; we either can run premade images (with Host OS) or we can install docker on the Raspbian. The installation package on official repository is bit outdated and will not run with Docker Hub; the official repository from where we can download Container images with ease. Hypriot has made debian installation packages available on their download page from where we can install the latest package (at the time of this writing; its 1.10.3)

To install it; give the following commands on the Raspbian Jessie Lite

$ curl -sSL https://downloads.hypriot.com/docker-hypriot_1.10.3-1_armhf.deb > docker-hypriot_1.10.3-1_armhf.deb
$ sudo dpkg -i docker-hypriot_1.10.3-1_armhf.deb
$ sudo sh -c 'usermod -aG docker $SUDO_USER'
$ sudo systemctl enable docker.service
$ sudo service docker start

Once docker is installed and running; we need to run an image; given underlying CPU platform is different; we cant just go ahead and install any image from Docker Hub. Fortunately there is “resin/rpi-raspbian:jessie” image that we can use. To download and run this image use docker run –it like this

pi@raspberrypi:~ $ docker run -it resin/rpi-raspbian:jessie
Unable to find image 'resin/rpi-raspbian:jessie' locally
jessie: Pulling from resin/rpi-raspbian
242279a37c38: Pull complete
072ccb327ac8: Pull complete
de6504dccd59: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:534fa5bc3aba67f7ca1b810110fef1802fccf9e52326948208e5eb81eb202710
Status: Downloaded newer image for resin/rpi-raspbian:jessie
root@5b62a2d14818:/#

  • docker run is to run the container
  • -it is to get the interactive terminal when its run
  • root@xxxxx# is the Container shell; note down the value after root@; its the container id

Once we have the container running; we can go ahead and install some package; say NANO using apt-get update and apt-get install nano. When its installed; we need to “commit” the container; think of it similar to source code control system. When we commit the container it creates an image; from which we can start an instance of container similar to how we used resin/rpi-raspbian:jessie image. To commit; exit from the container shell and then issue docker commit command like this

root@5b62a2d14818:/# exit
pi@raspberrypi:~ $ sudo docker commit -m "Added nano" -a "Khurram" 5b62a2d14818 khurram/pi:nano
sha256:99f0053b387ed69f334926726f4ce0fd7c1946e4cc11b65e7a42e6a58eff9685
pi@raspberrypi:~ $

  • 5b62a2d14818 is the container ID that we copied from the container’s shell prompt
  • khurram/pi:nano is the image target; khurram is the user, pi is the name and nano is the tag name

Once committed; we can run it again using docker run specifying image target

$ docker run -it khurram/pi:nano

Once running; we can continue installing other things; node in our case; and when its done we can exit from the container shell and commit the updated container again

  • $ apt-get install nodejs and $ apt-get install npm to install Node and Node Package Manager
  • $ ln –s /usr/bin/nodejs /usr/bin/node to create symbolic link so nodejs can be called as node (npm expects this)

root@e5e7005489a2:/# exit
pi@raspberrypi:~ $ docker commit -m "Added node" -a "Khurram" e5e7005489a2 khurram/pi:node
sha256:6af338545368613b015001afddacc9f8abff5b39d5f2f9111bc643cb47dc87de
pi@raspberrypi:~ $

This way; we should have three images all together by now; one the base raspbian:jessie that we ran first time; and two more that we committed; we can list these images using docker images

pi@raspberrypi:~ $ docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
khurram/pi           node                6af338545368        43 seconds ago      159 MB
khurram/pi           nano                99f0053b387e        17 minutes ago      105.6 MB
resin/rpi-raspbian   jessie              80a737f1a654        6 days ago          80.01 MB

  • If you have created some unwanted image; you can delete that using docker rmi image; eg docker rmi khurram/pi:nano

Using docker info we can learn about the currently configured settings of docker; Docker Root Dir is interesting; it tells where Docker is storing all its data including Containers

pi@raspberrypi:~ $ sudo docker info
Containers: 2
Running: 0
Paused: 0
Stopped: 2
Images: 4
Server Version: 1.10.3
Storage Driver: overlay
Backing Filesystem: extfs
Execution Driver: native-0.2
Logging Driver: json-file
Plugins:
Volume: local
Network: bridge null host
Kernel Version: 4.4.9+
Operating System: Raspbian GNU/Linux 8 (jessie)
OSType: linux
Architecture: armv6l
CPUs: 1
Total Memory: 434.7 MiB
Name: raspberrypi
ID: TNHK:5MI5:JGFD:DE3I:B6MX:VXVB:TCMD:ZTQI:IQKO:NH46:6NXP:OW6O
Debug mode (server): true
File Descriptors: 11
Goroutines: 21
System Time: 2016-05-31T10:08:20.649012315Z
EventsListeners: 0
Init SHA1: 0db326fc09273474242804e87e11e1d9930fb95b
Init Path: /usr/lib/docker/dockerinit
Docker Root Dir: /var/lib/docker
WARNING: No swap limit support
WARNING: No cpu cfs quota support
WARNING: No cpu cfs period support
WARNING: No cpuset support

We can checkout the /var/lib/docker to learn what’s there

pi@raspberrypi:~ $ sudo -i
root@raspberrypi:~# cd /var/lib/docker/
root@raspberrypi:/var/lib/docker# ls
containers  image  network  overlay  tmp  trust  volumes

We can copy the docker image to another server using docker save and docker load; docker save makes a tar file and its syntax is docker save –o tar-file image-name and docker load takes a tar file and its syntax is docker load –i tar-file. This way we can copy our running container to another server (or Raspberry in this case) seamlessly and we can expect that it will “just work” given container has everything it needs; no installation, version conflicts etc. By now we must have got an idea; how useful docker container and images are in long run and why its getting so popular. We can run the container from these images or download new image from Hub; and can run multiple containers of image as required.