外观
使用本地数据库部署 new-api
约 1158 字大约 4 分钟
Docker Composenew-apiPostgreSQLRedis
次阅读
2026-07-07
本文讲解如何使用 Docker Compose 部署 new-api,并复用宿主机上已有的 PostgreSQL 与 Redis,避免在容器内重复部署数据库。
前提条件
- 宿主机已安装并能正常访问 PostgreSQL 与 Redis。
- 本配置使用
network_mode: host,容器直接共享宿主机网络,因此数据库地址填127.0.0.1即可访问宿主机服务。若你的数据库在远程或 Docker 网络中,请相应修改地址。
1. 准备数据库
部署前需在本地 PostgreSQL 中创建专用数据库与用户:
CREATE USER newapi WITH PASSWORD 'password';
CREATE DATABASE newapi OWNER newapi;Redis 无需特殊准备;若与其他业务共用同一 Redis,建议在连接串中指定一个独立的 db 编号(本文使用 8)以避免键冲突。
2. 克隆仓库
git clone https://github.com/QuantumNous/new-api.git
cd new-api仓库自带的 docker-compose.yml 会启动内置的 PostgreSQL/Redis 容器,下一步需将其替换为下方复用本地数据库的版本。
3. 修改 docker-compose.yml
编辑 docker-compose.yml,将内容替换为以下配置,并按需修改标注的必改项:
必改项速览
SQL_DSN:本地 PostgreSQL 连接串(格式postgresql://用户:密码@地址:端口/库名)REDIS_CONN_STRING:本地 Redis 连接串(格式redis://:密码@地址:端口/db,无密码则省略:密码)PORT:new-api 对外端口(见 修改端口)SESSION_SECRET:多节点部署时必须设置为一个固定随机字符串,否则会话异常
services:
new-api:
image: calciumion/new-api:latest
container_name: new-api
restart: always
network_mode: host
command: --log-dir /app/logs
volumes:
- ./data:/data
- ./logs:/app/logs
environment:
# 这里的 postgresql 格式可以从网上来查询
- SQL_DSN=postgresql://newapi:password@127.0.0.1:15432/newapi # ⚠️ IMPORTANT: Change the password in production!
# - SQL_DSN=root:123456@tcp(mysql:3306)/new-api # Point to the mysql service, uncomment if using MySQL
# - LOG_SQL_DSN=postgresql://root:123456@postgres:5432/new-api-log # OPTIONAL: If you want a separate database for logging, uncomment and set this
# - LOG_SQL_DSN=clickhouse://default:123456@clickhouse:9000/new_api_logs # OPTIONAL: Use ClickHouse for logs only; also uncomment clickhouse in depends_on and the clickhouse service below
# - LOG_SQL_CLICKHOUSE_TTL_DAYS=0 # OPTIONAL: ClickHouse log retention days. Unset or 0 disables automatic deletion; set to e.g. 30 to keep 30 days
# 这里的 redos 格式可以从网上来查询
- REDIS_CONN_STRING=redis://:password@127.0.0.1:6379/8
- TZ=Asia/Shanghai
- ERROR_LOG_ENABLED=true # 是否启用错误日志记录 (Whether to enable error log recording)
- BATCH_UPDATE_ENABLED=true # 是否启用批量更新 (Whether to enable batch update)
- NODE_NAME=new-api-node-1 # 节点名称,用于审计日志中标识节点身份;多节点/容器部署时建议设置 (Node name used in audit logs; recommended when running multiple instances or in containers)
# - STREAMING_TIMEOUT=300 # 流模式无响应超时时间,单位秒,默认120秒,如果出现空补全可以尝试改为更大值 (Streaming timeout in seconds, default is 120s. Increase if experiencing empty completions)
# - RELAY_IDLE_CONN_TIMEOUT=90 # Relay HTTP 客户端空闲连接超时时间,单位秒,默认跟随 Go 标准库,设置为0表示不限制 (Relay HTTP client idle keep-alive timeout in seconds, defaults to Go standard library; set 0 to disable)
# - SESSION_SECRET=random_string # 多机部署时设置,必须修改这个随机字符串!! (multi-node deployment, set this to a random string!!!!!!!)
# - SYNC_FREQUENCY=60 # Uncomment if regular database syncing is needed
# - GOOGLE_ANALYTICS_ID=G-XXXXXXXXXX # Google Analytics 的测量 ID (Google Analytics Measurement ID)
# - UMAMI_WEBSITE_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx # Umami 网站 ID (Umami Website ID)
# - UMAMI_SCRIPT_URL=https://analytics.umami.is/script.js # Umami 脚本 URL,默认为官方地址 (Umami Script URL, defaults to official URL)
healthcheck:
test: ["CMD-SHELL", "wget -q -O - http://localhost:3000/api/status | grep -o '\"success\":\\s*true' || exit 1"]
interval: 30s
timeout: 10s
retries: 34. 修改端口(可选)
new-api 默认监听 3000 端口。由于本配置使用 network_mode: host,不能通过 ports 映射改端口,而需通过 PORT 环境变量指定。例如改为 10086:
services:
new-api:
# ... 其他配置省略
environment:
- PORT=10086 # 新增:指定 new-api 监听端口
- SQL_DSN=postgresql://newapi:password@127.0.0.1:15432/newapi
# ... 其余环境变量同步修改健康检查端口
healthcheck 中的端口默认写死 3000,修改 PORT 后必须同步改为新端口,否则容器健康检查会一直失败:
healthcheck:
test: ["CMD-SHELL", "wget -q -O - http://localhost:10086/api/status | grep -o '\"success\":\\s*true' || exit 1"]
interval: 30s
timeout: 10s
retries: 35. 启动服务
# 启动服务
docker compose up -d
# 查看日志
docker compose logs -f new-api6. 访问
服务启动成功后,访问http://服务器IP:3000 将自动引导到初始化页面。按照页面指引手动设置管理员账号和密码(仅首次安装需要)。完成初始化后即可使用所设置的管理员账号登录系统。