Files
life-echo/api/Dockerfile
iammm0 d104377d26 feat: 优化Docker构建配置,支持生产环境配置文件
- 更新.gitignore,允许.env.production文件被提交(私密仓库)
- 优化Dockerfile构建流程,使用.env.production作为生产环境配置
- 将.env.production复制为.env,确保生产环境使用正确的配置
- 新增.env.production生产环境配置文件
2026-01-23 10:56:59 +08:00

52 lines
1.4 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
# 复制应用代码(先复制代码,避免覆盖环境变量文件)
COPY . .
# 复制生产环境配置文件到镜像中
# 将 .env.production 复制为 .env供应用运行时使用
# 注意:构建前请确保 .env.production 文件存在
# 这一步放在最后,确保覆盖任何开发环境的 .env 文件
COPY .env.production ./.env
# 创建非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"]