chakokuのブログ(rev4)

テック・コミック・DTM・・・ごくまれにチャリ

RPiにDockerを入れてthingsboardを入れてみる

まずはRPiにDockerを入れる
Debian | Docker Docs

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

# To install the latest version, run:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# enable to execute docker with login user
sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker

docker が入ると、docker build用のymlがThingsboardから提供されているのでそれをファイルに落として、docker composeでビルド、実行する

version: '3.0'
services:
  mytb:
    restart: always
    image: "thingsboard/tb-postgres"
    ports:
      - "8080:9090"
      - "1883:1883"
      - "7070:7070"
      - "5683-5688:5683-5688/udp"
    environment:
      TB_QUEUE_TYPE: in-memory
    volumes:
      - ~/.mytb-data:/data
      - ~/.mytb-logs:/var/log/thingsboard

永続化のため、~/.mytb-dataを使うようで、この領域を作成する

mkdir -p ~/.mytb-data && sudo chown -R 799:799 ~/.mytb-data
mkdir -p ~/.mytb-logs && sudo chown -R 799:799 ~/.mytb-logs

docker composeで起動しようとすると、PORTが使われていますと怒られる
lsofツールをインストールして、PORTをBindしているサービス(Process)を特定して、止めて問題なければ止める
今回問題になった5683ポートを使っているのはWebIOPyだった。動作確認のために動かしっぱなしであった。これを止める。

$ docker compose up -d
WARN[0000]  thingsboard/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Running 0/1
 ? Container thingsboard-mytb-1  Starting                                                                                                               0.7s
Error response from daemon: driver failed programming external connectivity on endpoint thingsboard-mytb-1 (ae72fxxxxxxxxedc00): failed to bind port 0.0.0.0:5683/udp: Error starting userland proxy: listen udp4 0.0.0.0:5683: bind: address already in use

$ sudo apt install -y lsof

$ sudo lsof -i:5683
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python3 515 root    6u  IPv6  15978      0t0  UDP *:5683

$ ps aux|grep python
root         515  0.0  0.2 244388 17268 ?        Ssl  21:30   0:00 /usr/bin/python3 -m webiopi -l /var/log/webiopi -c /etc/webiopi/config

$ sudo kill -9 515

再度docker composeで起動してみる->起動されたようだ

$ docker compose up -d
WARN[0000] docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Running 1/1
 ? Container thingsboard-mytb-1  Started                                                                                                                1.7s

$ docker ps
CONTAINER ID   IMAGE                     COMMAND         CREATED         STATUS         PORTS                                                                                                                                                                                              NAMES
775xxxxxxx6a5   thingsboard/tb-postgres   "start-tb.sh"   5 minutes ago   Up 3 seconds   0.0.0.0:1883->1883/tcp, :::1883->1883/tcp, 0.0.0.0:7070->7070/tcp, :::7070->7070/tcp, 0.0.0.0:5683-5688->5683-5688/udp, :::5683-5688->5683-5688/udp, 0.0.0.0:8080->9090/tcp, [::]:8080->9090/tcp   thingsboard-mytb-1

コンテナのログを見るとエラーなく起動されたようだが・・・

 $ docker logs 77xxxxxx6a5
The files belonging to this database system will be owned by user "thingsboard".
This user must also own the server process.

The database cluster will be initialized with locale "C.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /data/db ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
syncing data to disk ... ok

Success. You can now start the database server using:
    /usr/lib/postgresql/12/bin/pg_ctl -D /data/db -l logfile start

Starting Postgresql...
waiting for server to start....2024-11-03 13:17:37.914 UTC [30] LOG:  starting PostgreSQL 12.20 (Debian 12.20-1.pgdg120+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
2024-11-03 13:17:37.915 UTC [30] LOG:  listening on IPv6 address "::1", port 5432
2024-11-03 13:17:37.915 UTC [30] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2024-11-03 13:17:37.926 UTC [30] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2024-11-03 13:17:37.975 UTC [31] LOG:  database system was shut down at 2024-11-03 13:17:35 UTC
2024-11-03 13:17:37.992 UTC [30] LOG:  database system is ready to accept connections
 done
server started
Creating database...
CREATE DATABASE
Postgresql is ready
Starting ThingsBoard installation ...
OpenJDK 64-Bit Server VM warning: Option UseBiasedLocking was deprecated in version 15.0 and will likely be removed in a future release.
[0.006s][warning][logging] Output options for existing outputs are ignored.
OpenJDK 64-Bit Server VM warning: Option UseBiasedLocking was deprecated in version 15.0 and will likely be removed in a future release.
  ______    __      _                              ____                               __
 /_  __/   / /_    (_)   ____    ____ _   _____   / __ )  ____   ____ _   _____  ____/ /
  / /     / __ \  / /   / __ \  / __ `/  / ___/  / __  | / __ \ / __ `/  / ___/ / __  /
 / /     / / / / / /   / / / / / /_/ /  (__  )  / /_/ / / /_/ // /_/ /  / /    / /_/ /
/_/     /_/ /_/ /_/   /_/ /_/  \__, /  /____/  /_____/  \____/ \__,_/  /_/     \__,_/
                              /____/

 ===================================================
 :: ThingsBoard ::       (v3.8.1)
 ===================================================

Starting ThingsBoard Installation...
Installing DataBase schema for entities...
Installing SQL DataBase schema part: schema-entities.sql
Installing SQL DataBase schema indexes part: schema-entities-idx.sql
Installing SQL DataBase schema PostgreSQL specific indexes part: schema-entities-idx-psql-addon.sql
Installing SQL DataBase schema views and functions: schema-views-and-functions.sql
Successfully executed query: DROP VIEW IF EXISTS device_info_view CASCADE;
Successfully executed query: CREATE OR REPLACE VIEW device_info_view AS SELECT * FROM device_info_active_attribute_view;
Installing DataBase schema for timeseries...
Installing SQL DataBase schema part: schema-ts-psql.sql
Successfully executed query: CREATE TABLE IF NOT EXISTS ts_kv_indefinite PARTITION OF ts_kv DEFAULT;
Loading system data...
Creating JWT admin settings...
Loading system widgets
Loading system SCADA symbols
Creating default notification configs for system admin
Creating default notification configs for all tenants

コンテナの中はJavaPostgreSQLが走っている

 $ docker exec -it 77xxxxxx5 /bin/sh
$ ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
thingsb+       1  0.0  0.0   4332  3048 ?        Ss   13:17   0:00 /bin/bash /usr/bin/start-tb.sh
thingsb+      30  0.0  0.3 217240 28056 ?        Ss   13:17   0:00 /usr/lib/postgresql/12/bin/postgres
thingsb+      32  0.0  0.7 217368 57928 ?        Ss   13:17   0:00 postgres: checkpointer
thingsb+      33  0.0  0.1 217240  8856 ?        Ss   13:17   0:00 postgres: background writer
thingsb+      34  0.0  0.1 217240 10128 ?        Ss   13:17   0:00 postgres: walwriter
thingsb+      35  0.0  0.1 217780  8204 ?        Ss   13:17   0:00 postgres: autovacuum launcher
thingsb+      36  0.0  0.0  71740  5992 ?        Ss   13:17   0:00 postgres: stats collector
thingsb+      37  0.0  0.0 217664  6864 ?        Ss   13:17   0:00 postgres: logical replication launcher
thingsb+     202 58.4 12.9 6300476 1034560 ?     Sl   13:21  10:00 java -cp /usr/share/thingsboard/bin/thingsboard.jar -Dplatform=deb -Dinstall.data_dir=/usr/share/thingsboard/d
thingsb+     254  0.0  0.2 219864 22480 ?        Ss   13:22   0:00 postgres: thingsboard thingsboard 127.0.0.1(50210) idle
thingsb+     258  0.0  0.2 219940 20616 ?        Ss   13:22   0:00 postgres: thingsboard thingsboard 127.0.0.1(50218) idle
thingsb+     259  0.0  0.2 220984 20560 ?        Ss   13:22   0:00 postgres: thingsboard thingsboard 127.0.0.1(50220) idle
thingsb+     260  0.0  0.2 218960 18568 ?        Ss   13:22   0:00 postgres: thingsboard thingsboard 127.0.0.1(50224) idle
thingsb+     261  0.0  0.2 219612 18768 ?        Ss   13:22   0:00 postgres: thingsboard thingsboard 127.0.0.1(50226) idle
thingsb+     262  0.0  0.2 219756 18052 ?        Ss   13:22   0:00 postgres: thingsboard thingsboard 127.0.0.1(50234) idle
thingsb+     263  0.0  0.1 218168 11840 ?        Ss   13:22   0:00 postgres: thingsboard thingsboard 127.0.0.1(50242) idle
thingsb+     264  0.0  0.1 218168 11900 ?        Ss   13:22   0:00 postgres: thingsboard thingsboard 127.0.0.1(50252) idle
thingsb+     265  0.0  0.1 218168 11836 ?        Ss   13:22   0:00 postgres: thingsboard thingsboard 127.0.0.1(50258) idle
thingsb+     266  0.0  0.1 218168 11964 ?        Ss   13:22   0:00 postgres: thingsboard thingsboard 127.0.0.1(50266) idle
thingsb+     267  0.0  0.1 218168 11904 ?        Ss   13:22   0:00 postgres: thingsboard thingsboard 127.0.0.1(50278) idle
thingsb+     268  0.0  0.1 218168 11900 ?        Ss   13:22   0:00 postgres: thingsboard thingsboard 127.0.0.1(50286) idle
thingsb+     269  0.0  0.1 218168 11892 ?        Ss   13:22   0:00 postgres: thingsboard thingsboard 127.0.0.1(50300) idle
thingsb+     270  0.0  0.1 218168 11956 ?        Ss   13:22   0:00 postgres: thingsboard thingsboard 127.0.0.1(50306) idle
thingsb+     271  0.0  0.1 218168 11896 ?        Ss   13:22   0:00 postgres: thingsboard thingsboard 127.0.0.1(50318) idle
thingsb+     272  0.0  0.1 218168 11956 ?        Ss   13:22   0:00 postgres: thingsboard thingsboard 127.0.0.1(50332) idle
thingsb+     525  1.2  0.0   2324   824 pts/0    Ss   13:38   0:00 /bin/sh
thingsb+     531  100  0.0   8444  3872 pts/0    R+   13:38   0:00 ps aux

RPiのIPを指定して8080ポートでブラウザからアクセスすると、ID/PWDの画面が表示された。

初期アカウントが設定されているので、それを指定してログインすると、ThingsBoardの画面にアクセスできた。

以降は、接続を許可するデバイスを登録して、ダッシュボードを作成すれば、ThingsBoardで計測データがグラフ表示されるはず。

Installing ThingsBoard using Docker (Linux or Mac OS) | ThingsBoard Community Edition
Debian | Docker Docs
Getting Started with ThingsBoard | ThingsBoard Community Edition