Thingsboard单体部署Docker去除内置PostgreSQL(使用外部PostgreSQL) - codeHui/IoT-Thingsboard-architecture-and-source-code-analysis GitHub Wiki

问题:

想将Thingsboard用Docker进行单体部署,但发现官方源码提供的单体部署的image,竟然把PostgreSQL也内置进去了。
而我想使用外部的PostgreSQL,比如阿里云上的PostgreSQL。 所以我就需要对Thingsboard的源码进行修改,重新build image image

源码修改

(本文假设你已经知道Thingsboard源码如何用maven和docker打包,不会细节说这部分)

1. 修改thingsboard.yml,将默认PostgreSQL的配置改为外部的PostgreSQL

  1. 我这边将PostgreSQL改成了timescale(PostgreSQL上的一个时序数据库插件),你可以选择不改 image
  2. 将PostgreSQL的url改成外部阿里云PostgreSQL的url image

2. 修改docker build部分的源码

如下图 msa -> tb -> docker-postgres 的就是默认的单体部署Docker配置

  1. 我首先新建了一个docker-cs文件夹,用来取代docker-postgres文件夹
    image
    docker-cs下的dockerfile文件会删除和数据库(PostgreSQL)相关的部分
FROM thingsboard/openjdk17:bookworm-slim


ENV HTTP_BIND_PORT=9090


COPY logback.xml ${pkg.name}.conf start-tb.sh upgrade-tb.sh install-tb.sh ${pkg.name}.deb /tmp/

# added dos2unix (converts the line endings of the mvnw file from DOS/Windows format (CRLF) to Unix/Linux format (LF)), without this, docker build on windows will fail.
RUN apt-get update \
    && apt-get install -y --no-install-recommends wget gnupg2 dos2unix \
    && apt-get update \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get purge -y --auto-remove \
    && dos2unix /tmp/*.sh \
    && dos2unix /tmp/${pkg.name}.conf \
    && chmod a+x /tmp/*.sh \
    && mv /tmp/start-tb.sh /usr/bin \
    && mv /tmp/upgrade-tb.sh /usr/bin \
    && mv /tmp/install-tb.sh /usr/bin \
    && dpkg -i /tmp/${pkg.name}.deb \
    && rm /tmp/${pkg.name}.deb \
    && (systemctl --no-reload disable --now ${pkg.name}.service > /dev/null 2>&1 || :) \
    && mv /tmp/logback.xml ${pkg.installFolder}/conf \
    && mv /tmp/${pkg.name}.conf ${pkg.installFolder}/conf \
    && mkdir -p /data \
    && chown -R ${pkg.user}:${pkg.user} /data \
    && chown -R ${pkg.user}:${pkg.user} /var/log/${pkg.name} \
    && chmod 555 ${pkg.installFolder}/bin/${pkg.name}.jar

USER ${pkg.user}

EXPOSE 9090
EXPOSE 1883
EXPOSE 5683/udp
EXPOSE 5685/udp
EXPOSE 5686/udp

VOLUME ["/data"]

CMD ["start-tb.sh"]
  1. 删除docker文件夹下项目启动脚本有关数据库的部分 image
  2. 修改tb文件夹下的pom.xml,刪除有关Cassandra和image push等不用的部分 这样重新打包下,就会用docker-cs文件夹下dockerfile打包出无内置数据库的image image
<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.thingsboard</groupId>
        <version>3.8.1</version>
        <artifactId>msa</artifactId>
    </parent>
    <groupId>org.thingsboard.msa</groupId>
    <artifactId>tb</artifactId>
    <packaging>pom</packaging>

    <name>ThingsBoard Docker Images</name>
    <url>https://thingsboard.io</url>
    <description>ThingsBoard Docker Images</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <main.dir>${basedir}/../..</main.dir>
        <pkg.name>thingsboard</pkg.name>
        <tb-postgres.docker.name>tb-ce</tb-postgres.docker.name>
        <pkg.installFolder>/usr/share/${pkg.name}</pkg.installFolder>
        <pkg.upgradeVersion>3.8.1</pkg.upgradeVersion>
        <dockerfile.skip>false</dockerfile.skip>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.thingsboard</groupId>
            <artifactId>application</artifactId>
            <version>${project.version}</version>
            <classifier>deb</classifier>
            <type>deb</type>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-tb-postgres-deb</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.thingsboard</groupId>
                                    <artifactId>application</artifactId>
                                    <classifier>deb</classifier>
                                    <type>deb</type>
                                    <destFileName>${pkg.name}.deb</destFileName>
                                    <outputDirectory>${project.build.directory}/docker-ce</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                   <execution>
                        <id>copy-docker-tb-postgres-config</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/docker-ce</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>docker</directory>
                                    <filtering>true</filtering>
                                </resource>
                                <resource>
                                    <directory>docker-ce</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>build-docker-tb-postgres-image</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                        <configuration>
                            <skip>${dockerfile.skip}</skip>
                            <repository>${docker.repo}/${tb-postgres.docker.name}</repository>
                            <verbose>true</verbose>
                            <googleContainerRegistryEnabled>false</googleContainerRegistryEnabled>
                            <contextDirectory>${project.build.directory}/docker-ce</contextDirectory>
                            <noCache>true</noCache>
                        </configuration>
                    </execution>
                    <execution>
                        <id>tag-docker-tb-postgres-image</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>tag</goal>
                        </goals>
                        <configuration>
                            <skip>${dockerfile.skip}</skip>
                            <repository>${docker.repo}/${tb-postgres.docker.name}</repository>
                            <tag>${project.version}</tag>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>jenkins</id>
            <name>Jenkins Repository</name>
            <url>https://repo.jenkins-ci.org/releases</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>


4.测试 打包成功后,能看见新的image
image
然后用下面命令就可以run起来,然后访问 http://localhost:8080/login
docker run -it -p 8080:9090 -p 1883:1883 --name tbCe --user root thingsboard/tb-ce

额外源码解释

也可以在pom.xml里面进行数据库url修改

我上面教程的第一步是在thingsboard.yml里面对数据库url默认值进行了修改,然后重新maven打包,但你可以直接在Dockerfile里面进行覆盖(如下图原始的dockerfile的这几个参数,就是和thingsboard.yml的参数一致,这里配置了就可以覆盖)
(我目前保持在thingsboard.yml里修改数据库的一个原因是,我是在本地单独启动的application项目,连接阿里云的Postgresql,对其进行数据库初始化)
image
更推荐的方式是把数据库配置放到docker-compose.yml里面,如Thingsboard官方install文档里一样,也就是docker的image里面不会有数据库的账号密码
image

pom.xml是根据application下打包出来的deb文件,进而打包出docker image的

image

优化讨论

我目前发现上面的pom.xml build一次要10分钟,有点慢,不知道是不是可以优化下 包括其他发现可以优化的地方,欢迎分享下 欢迎讨论区留言

⚠️ **GitHub.com Fallback** ⚠️