Files
lobehub/docs/self-hosting/platform/docker.zh-CN.mdx
Arvin Xu 4a87b31246 📝 docs: improve docs (#12013)
Update docs
2026-01-31 19:46:44 +08:00

161 lines
5.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: 使用 Docker 部署 LobeHub 数据库
description: 详细步骤教你如何在 Docker 中部署 LobeHub 服务端数据库。
tags:
- Docker
- LobeHub
- 数据库部署
- Postgres
---
# 使用 Docker 部署 LobeHub
<div style={{display:"flex", gap: 4}}>
[![][docker-release-shield]][docker-release-link]
[![][docker-size-shield]][docker-size-link]
[![][docker-pulls-shield]][docker-pulls-link]
</div>
<Callout type="info">
本文已经假定你了解了 LobeHub
的部署基本原理和流程,因此只包含核心环境变量配置的内容。如果你还不了解 LobeHub
的部署原理,请先查阅 [使用服务端数据库部署](/zh/docs/self-hosting/server-database) 。
此外,针对国内的腾讯云储存桶用户,可查询[配置腾讯云 COS
存储服务](/zh/docs/self-hosting/advanced/s3/tencent-cloud)。
</Callout>
## 在 Linux 服务器上部署
以下是在 Linux 服务器上部署 LobeHub 的流程:
<Steps>
### 创建 Postgres 数据库实例
请按照你自己的诉求创建一个 Postgres 数据库实例,例如:
```sh
docker network create pg
docker run --name my-postgres --network pg -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d paradedb/paradedb:latest-pg17
```
上述指令会创建一个名为 `my-postgres`,并且网络为 `pg` 的 PG 实例,其中 `paradedb/paradedb:latest-pg17` 是一个 Postgres 17 的镜像,且默认安装了 pgvector 和 pg\_search 插件。
<Callout type="info">
ParadeDB 镜像包含了 pgvector向量搜索和 pg\_search全文搜索插件是 LobeHub 实现 RAG 和知识库搜索的重要构件。
</Callout>
<Callout type="warning">
以上指令得到的 pg 实例并没有指定持久化存储位置,因此仅用于测试 /
演示,生产环境请自行配置持久化存储。
</Callout>
### 创建名为 `lobe-chat.env` 文件用于存放环境变量:
点击下方按钮生成所需密钥:
<GenerateSecret envName="KEY_VAULTS_SECRET" />
<GenerateSecret envName="AUTH_SECRET" />
点击下方按钮一键生成 `JWKS_KEY`(用于签名和验证 JWT
<GenerateJWKSKey />
```shell
# 网站域名
APP_URL=https://your-prod-domain.com
# DB 必须的环境变量
# 用于加密敏感信息的密钥,可以使用 openssl rand -base64 32 生成
KEY_VAULTS_SECRET='jgwsK28dspyVQoIf8/M3IIHl1h6LYYceSYNXeLpy6uk='
# Postgres 数据库连接字符串
# 格式postgres://username:password@host:port/dbname如果你的 pg 实例为 Docker 容器,请使用容器名
DATABASE_URL=postgres://postgres:mysecretpassword@my-postgres:5432/postgres
# 身份验证Better Auth
# 会话加密密钥使用以下命令生成openssl rand -base64 32
AUTH_SECRET=jgwsK28dspyVQoIf8/M3IIHl1h6LYYceSYNXeLpy6uk=
# JWKS 密钥,用于签名和验证 JWT
JWKS_KEY='{"keys":[...]}'
# S3 相关
S3_ACCESS_KEY_ID=xxxxxxxxxx
S3_SECRET_ACCESS_KEY=xxxxxxxxxx
# 用于 S3 API 访问的域名
S3_ENDPOINT=https://xxxxxxxxxx.r2.cloudflarestorage.com
S3_BUCKET=LobeHub
# S3_REGION=ap-chengdu # 如果需要指定地域
```
### 启动 lobehub docker 镜像
```sh
docker run -it -d -p 3210:3210 --network pg --env-file lobe-chat.env --name lobehub lobehub/lobehub
```
你可以使用下述指令检查日志:
```sh
docker logs -f lobehub
```
如果你在容器中看到了以下日志,则说明已经启动成功:
```log
[Database] Start to migration...
✅ database migration pass.
-------------------------------------
▲ Next.js 14.x.x
- Local: http://localhost:3210
- Network: http://0.0.0.0:3210
✓ Starting...
✓ Ready in 95ms
```
</Steps>
<Callout type="tip">
在我们官方的 Docker 镜像中,会在启动镜像前自动执行数据库 schema 的 migration
,我们的官方镜像承诺「空数据库 ->
完整表」这一段自动建表的稳定性。因此我们建议你的数据库实例使用一个空表实例,进而省去手动维护表结构或者
migration 的麻烦。
</Callout>
## 在本地Mac / Windows 上使用
LobeHub 也支持直接在本地的 Mac/Windows 本地使用。
在此我们已假设你的本地有一个 5432 端口可用,账号为 `postgres` ,密码是 `mysecretpassword` 的 pg 实例,它在 `localhost:5432` 可用。
那么你需要执行的脚本指令为:
```shell
$ docker run -it -d --name lobehub -p 3210:3210 \
-e DATABASE_URL=postgres://postgres:mysecretpassword@host.docker.internal:5432/postgres \
-e KEY_VAULTS_SECRET=jgwsK28dspyVQoIf8/M3IIHl1h6LYYceSYNXeLpy6uk= \
-e AUTH_SECRET=jgwsK28dspyVQoIf8/M3IIHl1h6LYYceSYNXeLpy6uk= \
-e JWKS_KEY='{"keys":[...]}' \
-e APP_URL=http://localhost:3210 \
-e S3_ACCESS_KEY_ID=xxxxxxxxxx \
-e S3_SECRET_ACCESS_KEY=xxxxxxxxxx \
-e S3_ENDPOINT=https://xxxxxxxxxx.r2.cloudflarestorage.com \
-e S3_BUCKET=LobeHub \
lobehub/lobehub
```
<Callout type="tip">
`Docker` 在 `Windows` 和 `macOS` 上走的是虚拟机方案,如果使用 `localhost` / `127.0.0.1`
,将会走到自身容器的 `localhost`,此时请尝试用 `host.docker.internal` 替代 `localhost`
</Callout>
[docker-pulls-link]: https://hub.docker.com/r/lobehub/lobehub
[docker-pulls-shield]: https://img.shields.io/docker/pulls/lobehub/lobehub?color=45cc11&labelColor=black&style=flat-square
[docker-release-link]: https://hub.docker.com/r/lobehub/lobehub
[docker-release-shield]: https://img.shields.io/docker/v/lobehub/lobehub?color=369eff&label=docker&labelColor=black&logo=docker&logoColor=white&style=flat-square&sort=semver
[docker-size-link]: https://hub.docker.com/r/lobehub/lobehub
[docker-size-shield]: https://img.shields.io/docker/image-size/lobehub/lobehub?color=369eff&labelColor=black&style=flat-square&sort=semver