Single board computer projects

Using Docker

last updated: 2021-06-21

under construction

Intro

Everybody is talking of Docker. As a new OpenHAB version (3) is out, and I have to update my home server. Docker should allow a painless migration because both systems (v2, v3) can run simultaneously. Here I try to document the important steps to get everything running.

A good starting point are the docs.docker.com pages: https://docs.docker.com/get-started/overview/.

Install Docker

Install Docker engine on Debian (buster)

Infos can be found here: https://docs.docker.com/engine/install/debian/

On my udoo server (x86_64) I'm running Debian 10 (buster). First we install the following packages if they are missing:

    sudo apt update
    sudo apt upgrade
    sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release

Add GPG key and add the stable repo to your sources.list:

    curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o \
    /usr/share/keyrings/docker-archive-keyring.gpg
    echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] \
    https://download.docker.com/linux/debian \ $(lsb_release -cs) stable" | sudo tee \
    /etc/apt/sources.list.d/docker.list > /dev/null

Now we install the Docker engine:

    sudo apt-get update
    sudo apt-get install docker-ce docker-ce-cli containerd.io

Test if everything is ok:

    sudo docker run hello-world

On Debian (Ubuntu), the Docker service starts on boot by default.

Install Docker engine on Raspi OS

For the Raspberry Pi, installing using the repository is not yet supported. But we get a convenience script :).

    sudo apt update
    sudo apt upgrade
    sudo curl -sSl https://get.docker.com | sh
    sudo docker run hello-world

On Debian (Ubuntu), the Docker service starts on boot by default. If it does not on Raspi we can use systemctl with enable. Other possibilities for systemctl are status, stop and start (restart):

    sudo systemctl enable docker
    sudo systemctl status docker

Docker as a non-root user

If we want to run a container without sudo we get an error. This is because docker group is created but no users are added to it. So we need to use sudo or better we engage optional procedures for configuring Linux hosts to work better with Docker. These steps are explained here: https://docs.docker.com/engine/install/linux-postinstall/.

    sudo groupadd docker
    sudo usermod -aG docker $USER
    newgrp docker
    docker run hello-world

Docker commands

All docker commands must be used with sudo if we didn't add a user to the docker group!

List version:

    docker version

View container (and get names created by docker):

    docker ps -a

View images:

    docker images

Get a newer image (update e.g. ubuntu image):

    docker image pull ubuntu

Run a container (-i interactive, -t pseudo-terminal, --name name, -h host-name). We can specify a version with a colon. Best to give an own name for the container.

    docker run -it ubuntu`
    docker run -it ubuntu:18.10`
    docker run -it --name myubuntu -h ubuhost ubuntu:18.10`

With run there is always a new container, so use docker start with your container name to run the same container (without -t)!

    docker start -i myubuntu`

With exec start a second process (second terminal to run e.g. a command) of the same container. We can stop it with Q:

    docker exec -it myubuntu /usr/bin/top

Docker-compose

As we need more container (openHAB, influxDB, Grafana) we will use Docker-compose to call our containers. Docker-compose uses a YAML text file to run the different container. More infos: https://docs.docker.com/compose/

Install on Debian

Only on Linux we need to install Docker-compose (https://docs.docker.com/compose/install/). The current version is 1.29.2. For another version, change the command!

    sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/ \
    docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose

Install on Raspi:

For the Raspberry Pi we use pip:

    sudo apt install -y libffi-dev libssl-dev python3-dev python3 python3-pip
    sudo pip3 install docker-compose

Test the installation.

    docker-compose --version

Now let's have a look at the following YAML file from https://hub.docker.com/r/openhab/openhab/:

    # openhab.yml
    version: '3.8'

    services:
      openhab:
        image: "openhab/openhab:3.0.2"
        container_name: openhab
        restart: always
        network_mode: host
        volumes:
          - "/etc/localtime:/etc/localtime:ro"
          - "/etc/timezone:/etc/timezone:ro"
          - "./openhab/openhab_addons:/openhab/addons"
          - "./openhab_conf:/openhab/conf"
          - "./openhab_userdata:/openhab/userdata"
        environment:
          OPENHAB_HTTP_PORT: "9080"
          OPENHAB_HTTPS_PORT: "9443"
          EXTRA_JAVA_OPTS: "-Duser.timezone=Europe/Luxembourg"
        ports:
          - "8080:8080"
          - "8443:8443"  

We use the network of the host, because openHAB requires UPnP discovery. Unfortunately port mapping is incompatible with this network_mode. Because I use 2 versions of openHAB, I change the ports from 8xxx to 9xxx. The openHAB directories are created from docker-compose.

We save the file as text-only with the name openhab.yml and start it with the up command:

    docker-compose -f openhab.yml up --remove-orphans -d

Without -f and the filename docker-compose searches for a file named docker-compose.yml in the current directory. The option -d stands for detached mode, meaning the container is running in the background (default = foreground).

To stop or (re)start the running container we use:

    docker-compose -f openhab.yml stop
    docker-compose -f openhab.yml start

mosquitto.yml

version: '3.8'

services: mosquitto: image: eclipse-mosquitto:latest containername: mosquitto restart: always networkmode: host volumes: - "${PWD}/mosquitto/conf:/mosquitto/config/" - "${PWD}/mosquitto/data/:/mosquitto/data/" - "${PWD}/mosquitto/log/:/mosquitto/log/"

allow_anonymous true

mosquitto.conf

    # Listener
    listener 1883

    # Persitence
    persistence true
    persistence_location /mosquitto/data/

    # Logging
    log_dest file /mosquitto/log/mosquitto.log

    # Security
    clientid_prefixes xxx
    allow_anonymous false

    # Default authentication and topic access control
    password_file /home/pi/mosquitto/my_passfile

Downloads

Interesting links