Files
life-echo/api/docker-compose.yml
mingjunzhao012665 8567a8fece 功能2和3:改进 Docker 健康检查配置和数据库持久化
功能2:改进 Docker 健康检查配置
- 将健康检查从使用 requests 库改为使用 Python 内置的 http.client
- 减少 Docker 镜像依赖,提高构建效率
- 统一 Dockerfile 和 docker-compose.yml 中的健康检查配置

功能3:改进 Docker 配置和数据库持久化
- 在 Dockerfile 中添加 .env 文件复制支持,允许在构建时打包环境变量
- 改进 docker-compose.yml 中的数据库持久化配置,直接挂载数据库文件
- 添加日志配置,支持日志文件轮转
- 移除无效的 deploy 配置(仅在 Docker Swarm 模式下有效)
- 添加详细的配置注释说明
2026-01-18 17:09:10 +08:00

46 lines
1.7 KiB
YAML
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.
version: '3.8'
services:
api:
build:
context: .
dockerfile: Dockerfile
image: life-echo-api:latest
container_name: life-echo-api-prod
ports:
- "8000:8000"
# 环境变量文件优先级env_file > 镜像中的 .env
# 如果 .env.prod 不存在,容器会使用构建时打包的 .env 文件
env_file:
- .env.prod
volumes:
# 持久化数据库文件(确保数据库文件在容器重启后保留)
# 数据库文件默认在 /app/life_echo.db挂载到宿主机以持久化
- ./life_echo.db:/app/life_echo.db
# 如果需要将数据库存储在 data 目录,可以在 .env 中设置 DATABASE_URL=sqlite+aiosqlite:///./data/life_echo.db
# 然后取消下面的注释并注释掉上面的挂载
# - ./data:/app/data
restart: always
# 健康检查(使用 Python 内置库,无需额外依赖)
healthcheck:
test: ["CMD", "python", "-c", "import http.client; conn = http.client.HTTPConnection('localhost', 8000); conn.request('GET', '/health'); r = conn.getresponse(); exit(0 if r.status == 200 else 1)"]
interval: 30s
timeout: 10s
retries: 3
start_period: 10s
networks:
- life-echo-network
# 日志配置(生产环境推荐)
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
# 注意deploy 配置仅在 Docker Swarm 模式下有效
# 如果使用 docker-compose up非 swarm 模式),资源限制需要使用 ulimits 或其他方式
# 如需资源限制,建议使用 docker run 的 --memory 和 --cpus 参数,或使用 docker-compose 的 ulimits
networks:
life-echo-network:
driver: bridge