Raspberry PI Docker IOTserver 2.0


Installation according IOTstack and many thanks to Andreas Spiess for the good tutorial.
Raspberry PI 4 with 4 GB memory (2 GB will do)

  • Install Raspberry PI OS Lite (64-BIT) on a External SSD USB hard disk with the user pi
  • Boot the RP from SSD
  • sudo apt update && sudo apt upgrade -y

Automatic installation

  • sudo curl -fsSL https://raw.githubusercontent.com/SensorsIot/IOTstack/master/install.sh | bash
  • reboot (automatic)

Log 2 Ram to reduce diskwriting

  • change directory > cd IOTstack
  • start menu > ./menu.sh
  • Miscellaneous commands
  • Disable swap by setting swappiness to 0
  • Uninstall Swapfile (Disables swap)
  • install log2ram to decrease the load on sd card, moves /var/log into ram
    • Reboot to activate log2ram

Build a stack of containers

  • cd ~/IOTstack
  • ./menu.sh
  • Build Stack (stretch window)
  • select containers to build
    • influxdb (database)
    • mosquitto (message service)
    • portainer-ce (to manage containers)
  • Enter to start build
  • Enter the right timezone for domoticz
    • TZ=Europe/Amsterdam
  • Start the stack > docker commands
    • Start stack
  • Update npm
    • npm install -g npm@9.1.2

Container Commands

docker container my_command

create — Create a container from an image.
start — Start an existing container.
run — Create a new container and start it.
ls — List running containers.
inspect — See lots of info about a container.
logs — Print logs.
stop — Gracefully stop running container.
kill —Stop main process in container abruptly.
rm— Delete a stopped container.

Connect to programs

  • connect to Node-Red > Docker ip:1880
  • connect to Portainer > Docker ip:9000
  • connect to Grafana > Docker ip:3000
  • Influx GUI InfluxDbStudio
  • Connect to influxdb > docker exec -it influxdb /bin/bash
    • influx
    • show databases
    • use “name of the database”
    • show MEASUREMENTS
    • First 10 > SELECT * FROM “name of measurement” LIMIT 10
    • Last 10 SELECT * FROM “name of measurement” ORDER BY DESC LIMIT 10
    • SELECT * FROM Vloerverwarming WHERE Zon_Intensiteit > 200
  • Connect to mariadb > docker exec -it mariadb bash

Add a USB port to a container

  • Edit the docker-compose.yml file
  • Add under the container
    • devices: (if not already there)
  • Add under devices
    • – /dev/ttyUSB0:/dev/ttyUSB0

Export / Import influx database

  • Check version
    • Shell in the influxdb container > docker exec -it influxdb /bin/bash
    • Version Info > influxd version
    • https://docs.influxdata.com/influxdb/v1.8/administration/backup_and_restore/
    • https://docs.influxdata.com/influxdb/v2.0/backup-restore/

  • EXPORT
  • if needed create export directory > sudo mkdir ~/IOTstack/backups/influxdb/db/export
  • sudo rm ~/IOTstack/backups/influxdb/db/export/*.*
  • ls ~/IOTstack/backups/influxdb/db/export/
  • Shell in the influxdb container > docker exec -it influxdb /bin/bash
    • influxd backup -portable -database DB_LE_HEATING /var/lib/influxdb/backup/export
    • exit container shell
  • sudo chmod 777 -R ~/IOTstack/backups/influxdb/db/export
  • —————————————————————————————————-
  • IMPORT
  • change over to new machine
  • if needed create an import directory > sudo mkdir ~/IOTstack/backups/influxdb/db/import
  • sudo chown pi:pi ~/IOTstack/backups/influxdb/db/import
  • sudo rm ~/IOTstack/backups/influxdb/db/import/*.*
  • ls -l ~/IOTstack/backups/influxdb/db/import/
  • Copy exported data
    • sudo scp pi@192.168.166.202:~/IOTstack/backups/influxdb/db/export/*.* ~/IOTstack/backups/influxdb/db/import
  • Shell in the influxdb container > docker exec -it influxdb /bin/bash
    • influxd restore -portable -db DB_LE_HEATING /var/lib/influxdb/backup/import
    • exit container shell

Backup to dropbox

This is the first time you run this script, please follow the instructions:

(note: Dropropbox will change there API from 30.9.2021.
When using dropbox_uploader.sh configured in the past with the old API, have a look at README.md, before continue.)

1) Open the following URL in your Browser, and log in using your account: https://www.dropbox.com/developers/apps
2) Click on “Create App”, then select “Choose an API: Scoped Access”
3) “Choose the type of access you need: App folder”
4) Enter the “App Name” that you prefer (e.g. MyUploader501174821780), must be unique

Now, click on the “Create App” button.

5) Now the new configuration is opened, switch to tab “permissions” and check “files.metadata.read/write” and “files.content.read/write”
Now, click on the “Submit” button.

6) Now to tab “settings” and provide the following information:
App key:


Backup Docker manual to a PC

STEP 01
Script to collect and compress the necessary docker files.
./BackupDocker.sh at the Raspberry PI
And make it executable > chmod 755 BackupDocker.sh


BackUpDate=`date +"%Y-%m-%d"`
#
# BackupDocker.sh
#
sudo tar --exclude='backups' -cvf /home/pi/"$BackUpDate".Docker01.volumes.tar /home/pi/IOTstack/volumes/*
sudo chown pi:pi /home/pi/"$BackUpDate".Docker01.volumes.tar
#
sudo tar --exclude=’backups’ -cvf /home/pi/"$BackUpDate".Docker01.services.tar /home/pi/IOTstack/services/*
sudo chown pi:pi /home/pi/"$BackUpDate".Docker01.services.tar
#
sudo tar --exclude='backups' -cvf /home/pi/"$BackUpDate".Docker01.compose.tar /home/pi/IOTstack/docker-compose.yml
sudo chown pi:pi /home/pi/"$BackUpDate".Docker01.compose.tar
#
echo "-"
echo "-"
echo "-"
echo "-"
echo "Below the date of the backup needed to copy the to a PC"
echo "-"
echo "$BackUpDate"
echo "-"
echo "-"
echo "-"
echo "-"

STEP 02
PC script (PSCP is needed)
At a Windows PC, download the docker backup files
BackUpDocker.cmd

@echo off
REM
REM BackUpDocker.cmd
REM
set /p pw= "Please enter the pasword of the docker machine :"
cls
set /p DateOfBackup= "Please enter the date of the backup :"
echo '%DateOfBackup%'
set /p IP= "Please enter the ip of the docker machine :"
echo '%IP%
if not exist c:\backupDocker (
	mkdir c:\backupDocker
	)
PSCP -pw %pw% pi@%IP%:/home/pi/%DateOfBackup%.Docker01.volumes.tar c:\backupDocker
PSCP -pw %pw% pi@%IP%:/home/pi/%DateOfBackup%.Docker01.services.tar c:\backupDocker
PSCP -pw %pw% pi@%IP%:/home/pi/%DateOfBackup%.Docker01.compose.tar c:\backupDocker
pause

Restore on a new docker server

STEP 03 Stop all running containers and delete:
-The directory volumes > rm -rfv volumes
-The directory services > rm -rfv services
-File docker-compose.yml > rm docker-compose.yml



#!/bin/bash
#
# RemoveDockerFiles.sh
#
#
cd IOTstack
sudo rm -rfv volumes
sudo rm -rfv services
sudo rm docker-compose.yml
ls -l
#

STEP 04 (If you move your Docker machines)
PC script (PSCP is needed)
At a Windows PC, restore the docker backup files to the new Docker Server
RestoreDocker.cmd

@echo off
REM
REM RestoreDocker.cmd
REM
set /p pw= "Please enter the pasword of the docker machine :"
cls
set /p DateOfBackup= "Please enter the date of the backup :"
echo '%DateOfBackup%'
set /p IP= "Please enter the ip of the docker machine :"
echo '%IP%
PSCP -pw %pw% c:\backupDocker\%DateOfBackup%.Docker01.volumes.tar pi@%IP%:/home/pi
PSCP -pw %pw% c:\backupDocker\%DateOfBackup%.Docker01.services.tar pi@%IP%:/home/pi
PSCP -pw %pw% c:\backupDocker\%DateOfBackup%.Docker01.compose.tar pi@%IP%:/home/pi
pause

STEP 04 Script to restore the necessary docker files.


#!/bin/bash
#
# RestoreDocker.sh
#
BackUpDate=`date +"%Y-%m-%d"`
#
#sudo rm *.tar
#
sudo tar -xvf /home/pi/"$BackUpDate".Docker01.volumes.tar -C /
#
sudo tar -xvf /home/pi/"$BackUpDate".Docker01.services.tar -C /
#
sudo tar -xvf /home/pi/"$BackUpDate".Docker01.compose.tar -C /
#

STEP 05 Start the docker containers from the IOTstack menu


Simple intranet webserver

  • image > httpd:latest
  • Volume > /usr/local/apache2/htdocs
  • HTML page (in the volume)
    • sudo nano /var/lib/docker/volumes/Intranet_Webserver/_data/index.html
<html><body><h1>Rv14</h1>
<a href="http://192.168.x.x:x" target="_blank">Garage deur + Verlichting</a>><o:p></o:p></span></p>
<h1>
Location 2 
</h1>
<a href="http://192.168.x.x:x" target="_blank">Kachel Node Red</a><o:p></o:p></span></p>
</body></html>

YML file


networks:
  default:
    driver: bridge
    ipam:
      driver: default
  nextcloud:
    driver: bridge
    internal: true
    ipam:
      driver: default
services:
  portainer-ce:
    container_name: # Name of the container, free to choose
    image: # Name of the image to download
    restart: unless-stopped
    ports:
    - "HOST:CONTAINER" # include the " "
    volumes:
    - HOST:CONTAINER
    

services:
portainer-ce:
container_name: portainer-ce
image: portainer/portainer-ce
restart: unless-stopped
ports:
– “8000:8000”
– “9000:9000”
# HTTPS
– “9443:9443”
volumes:
– /var/run/docker.sock:/var/run/docker.sock
– ./volumes/portainer-ce/data:/data

OctoPrint

List USB devices
- ls -1 /dev/serial/by-id
Add USB device to Octoprint
echo OCTOPRINT_DEVICE_PATH=/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0

Wire Guard Easy with Portainer

docker run --rm -it ghcr.io/wg-easy/wg-easy wgpw 'secret'

Use Portainer to setup a new container.

  • Create a volume > WireGuard_config
  • Create a volume > WireGuard_modules
  • Create a new container
    • Name > wg-easy
    • Image > ghcr.io/wg-easy/wg-easy
    • Map additional ports
      • 51820:51820/udp
      • 51821:51821/tcp
    • Map volumes
      • Container > /etc/wireguard to volume WireGuard_config
      • Container > /lib/modules to volume WireGuard_modules
    • Environments
      • PASSWORD_HASH > Just generated and modified hash
      • WG_HOST > External IP
    • Restart policy
      • Unless stopped
    • Runtime & resources
      • Add sysctl
        • net.ipv4.ip_forward > 1
        • net.ipv4.conf.all.src_valid_mark >1
    • Capabilities
    • NET_ADMIN > switch on
    • SYS_MODULE > switch on

Connect a external USB drive for volumes

  • Install IOTstack on SD-card
  • Prepare external drive
    • lsblk
    • format external drive
      • sudo umount /dev/sda1
      • sudo mkfs.ext4 /dev/sda
    • Mount external drive
      • sudo mkdir -p /mnt/iotdata
      • sudo blkid
      • sudo nano /etc/fstab
      • UUID=abcd-1234 /mnt/iotdata ext4 defaults,noatime 0 2
    • Test the mount
      • sudo mount -a
      • df -h
    • Create volumes to external drive
      • sudo mkdir /mnt/iotdata/volumes
      • sudo ln -s /mnt/iotdata/volumes ~/IOTstack/volumes
      • sudo chown pi:pi ~/IOTstack/volumes

PI Hole

eth dc:a6:32:7d:9e:d5
WLan dc:a6:32:7d:9e:d6