[ MeetUp#2] Docker & Linux containers in Raspberry Pi

See Docker Cli commands at https://docs.docker.com/engine/reference/commandline/cli/

If you have burned the CONFIGURED IMAGE, you can go straight to section 3. Container is already built to execute the reading of the sensor

1. Prepare the RPi with Hypriot OS 0.7.0 Berry (valid for RPi2 & RPi3)

http://raspberryparatorpes.net/sistemas-operativos/hypriotos-un-raspbian...

http://blog.hypriot.com/getting-started-with-docker-on-your-arm-device/

  • user : pi        password : raspberry
  • user : root     password : hypriot

Currently we are using Hypriot 1.4.0, whose credentials are:

  • user : pirate  password : hypriot

Hypriot OS configuration of Raspbian is based on Occi, a small configuration helper for the Raspberry Pi by Adafruit. Occi is part of Adafruit Pi Finder, a tool that will ssh into the fresh new Pi, update it, set up the wifi SSID and password, set a custom hostname of your choice, and install Occidentalis, a collection of really handy software.

At this point, it is worth mentioning that Adafruit provides its own Raspbian repo https://apt.adafruit.com to make easier the installation of typical tools for the Raspberry Pi. 

Stay as root for all the commands below (sudo -s to make sure):

Edit /boot/occidentalis.txt to set your wifi credentials (ssid and password):

https://github.com/hypriot/flash#occidentalistxt

Execute sudo occi to apply the full configuration in /boot/occidentalis.txt

If you want to configure other network options (such a s a fixed IP address), you should do it directly in /etc/network/interfaces file as usual. It is also useful to install the package wireless-tools to be able to get info of wifi network.

Afterwards you can bring up the wifi wlan0 interface and connect through SSH to finish the configuration of the Spanish keyboard layout (or whatever you wish to use):

$ ifdown && ifup -v wlan0

Finally, let us add the spanish keyboard layout and set it as the default:

https://www.turnkeylinux.org/forum/support/20130912/keyboard-layout-help

$ apt-get install keyboard-configuration
# install will ask you the configuration layout you wish to set

# To apply the selected keyboard layout (or change to another one)
$ dpkg-reconfigure keyboard-configuration

Bug: for some reason, although in /etc/default/keyboard configuration option XKBLAYOUT="es" is right, the local console does not seem to apply it even after reboot???

http://raspberrypi.stackexchange.com/questions/10060/raspbian-keyboard-l...

 

2. A first try with a container of a tiny webserver

http://blog.hypriot.com/getting-started-with-docker-on-your-arm-device/

docker run -d -p 80:80 hypriot/rpi-busybox-httpd

Check if your container is running by typing:

docker ps

#For listing all containers, even those that were exited
docker ps -a

Now you can open up your browser on your workstation and type in the IP address of your Raspberry Pi to see that it really works!

http://192.168.43.181:80

3. Docker Sensor Fu on a Raspberry Pi

http://blog.hypriot.com/post/docker-sensor-fu-on-a-raspberry-pi/

Login as "root", create a directory named "DOCKER" since you will be placing there your Docker files.

The Dockerfile to build the image is as follows (name it as Dockerfile and put it in your working directory):

FROM resin/rpi-raspbian:jessie
MAINTAINER Govinda Fichtner <govinda@hypriot.com>

# Install dependencies
RUN apt-get update && apt-get install -y \
    git-core \
    build-essential \
    gcc \
    python \
    python-dev \
    python-pip \
    python-virtualenv \
    --no-install-recommends && \
    rm -rf /var/lib/apt/lists/*

RUN pip install pyserial
RUN git clone git://git.drogon.net/wiringPi
RUN cd wiringPi && ./build
RUN pip install wiringpi2

# Define working directory
WORKDIR /data
VOLUME /data

CMD ["/bin/bash"]

Using this Dockerfile we can now build our image:

$ docker build -t hypriot/wiringpi:latest .
...
$ docker images
REPOSITORY         TAG      IMAGE ID      CREATED             SIZE
hypriot/wiringpi   latest   599e98bd4c20  30 seconds ago      302.2 MB

Now the image has everything that we need to compile software. On top of that it also has the WiringPi libraries on board.

Next we can run this image to build the software that actually reads the sensor:

$ docker run --device /dev/ttyAMA0:/dev/ttyAMA0 --device /dev/mem:/dev/mem --privileged -ti hypriot/wiringpi bash

# The output forwards the console to the command line of the container
root@84f66e14df05:/data#

To ssh into a running container simply write:

docker attach 84f66e14df05

where the argument is the CONTAINER ID (output of docker ps command)

To test if we can access the GPIO’s we can run:

root@84f66e14df05:/data# gpio readall

And to test if the pins work properly, you can run the GPIO test (make sure nothing is connected to the GPIOs): 

cd /wiringPi/gpio/
./pintest

Let´s get back to /data directory, clone and compile the library for the DHT22 temperature/humidity sensor:

cd /data
git clone https://github.com/technion/lol_dht22
cd lol_dht22
./configure && make

Bear in mind that if you exit the container all these files will be lost.

An now read the sensor (data pin is wiringPi number 8, corresponding to SDA.1, physical nº 3; the other 2 cables are connected to 5 V (nº 2) and ground (nº 6)):

root@84f66e14df05:/data/lol_dht22# ./loldht 8

Raspberry Pi wiringPi DHT22 reader
www.lolware.net
Humidity = 34.00 % Temperature = 22.80 *

This technique allows us to deploy our sensor reading software easily and reliably to every device that is running a Docker Engine. No installation hassle. No dependency hell. Let's see.

3.1 Additions to the Docker file to get the full stack of DHT22 sensor

We need to extend our Dockerfile and these 3 lines:

RUN git clone https://github.com/technion/lol_dht22 /lol_dht22_src
RUN cd lol_dht22_src && ./configure && make
RUN mv /lol_dht22_src/loldht /loldht && rm -Rf /lol_dht22_src

The finished file looks like this:

FROM resin/rpi-raspbian:jessie
MAINTAINER Govinda Fichtner <govinda@hypriot.com>

# Install dependencies
RUN apt-get update && apt-get install -y \
    git-core \
    build-essential \
    gcc \
    python \
    python-dev \
    python-pip \
    python-virtualenv \
    --no-install-recommends && \
    rm -rf /var/lib/apt/lists/*

RUN pip install pyserial
RUN git clone git://git.drogon.net/wiringPi
RUN cd wiringPi && ./build
RUN pip install wiringpi2

RUN git clone https://github.com/technion/lol_dht22 /lol_dht22_src
RUN cd lol_dht22_src && ./configure && make
RUN mv /lol_dht22_src/loldht /loldht && rm -Rf /lol_dht22_src

# Define working directory
WORKDIR /data
VOLUME /data

CMD ["/bin/bash"]

After rebuilding the image with this updated Dockerfile we can now run (remember to do this from the directory where Dockerfile is placed):

docker run --device /dev/ttyAMA0:/dev/ttyAMA0 --device /dev/mem:/dev/mem --privileged -ti hypriot/wiringpi /loldht 8

And will obtain an equivalent output:

Raspberry Pi wiringPi DHT22 reader
www.lolware.net
Data not good, skip
Humidity = 35.00 % Temperature = 22.00 *C