Files
life-echo/api/Dockerfile
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

52 lines
1.3 KiB
Docker
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.
# Life Echo API Dockerfile
FROM python:3.11-slim
# 设置工作目录
WORKDIR /app
# 安装系统依赖
RUN apt-get update && apt-get install -y \
gcc \
g++ \
libc6-dev \
libffi-dev \
libxml2-dev \
libxslt1-dev \
libjpeg-dev \
zlib1g-dev \
libpng-dev \
libfreetype6-dev \
&& rm -rf /var/lib/apt/lists/*
# 复制依赖文件
COPY requirements.txt .
# 安装Python依赖
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir -r requirements.txt
# 复制 .env 文件到镜像中(用于打包环境变量)
# 注意:构建前请确保 .env 文件存在,否则构建会失败
# 如果 .env 文件不存在,可以创建 .env.example 并复制为 .env
COPY .env* ./
# 复制应用代码
COPY . .
# 创建非root用户
RUN useradd -m -u 1000 appuser && \
chown -R appuser:appuser /app
# 切换到非root用户
USER appuser
# 暴露端口
EXPOSE 8000
# 健康检查(使用 Python 内置的 http.client无需额外依赖
HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
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)" || exit 1
# 启动命令
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]