feat: 优化Docker构建配置,支持生产环境配置文件
- 更新.gitignore,允许.env.production文件被提交(私密仓库) - 优化Dockerfile构建流程,使用.env.production作为生产环境配置 - 将.env.production复制为.env,确保生产环境使用正确的配置 - 新增.env.production生产环境配置文件
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -12,6 +12,9 @@ ENV/
|
||||
# 环境变量
|
||||
.env
|
||||
.env.local
|
||||
# 注意:.env.production 应该被提交(私密仓库)
|
||||
# 明确排除 .env.production,确保它可以被提交
|
||||
!.env.production
|
||||
|
||||
# 数据库文件
|
||||
*.db
|
||||
|
||||
32
api/.env.production
Normal file
32
api/.env.production
Normal file
@@ -0,0 +1,32 @@
|
||||
# DeepSeek API 配置(推荐,优先使用)
|
||||
DEEPSEEK_API_KEY=sk-09f17fb61c5a4299a3afc2a01de7af75
|
||||
DEEPSEEK_BASE_URL=https://api.deepseek.com
|
||||
DEEPSEEK_MODEL=deepseek-chat
|
||||
|
||||
# =============================================================================
|
||||
# 数据库配置(必需)
|
||||
# =============================================================================
|
||||
# PostgreSQL 数据库连接 URL
|
||||
# 格式: postgresql://用户名:密码@主机:端口/数据库名
|
||||
DATABASE_URL=postgresql://postgres:postgres@postgresql:5432/life_echo
|
||||
|
||||
# =============================================================================
|
||||
# Redis 配置(必需)
|
||||
# =============================================================================
|
||||
# Redis 连接 URL
|
||||
# 格式: redis://[:密码@]主机:端口[/数据库编号]
|
||||
REDIS_URL=redis://redis:6379/0
|
||||
# Redis 会话过期时间(可选,默认: 86400 秒,即 24 小时)
|
||||
REDIS_SESSION_TTL=86400
|
||||
|
||||
# =============================================================================
|
||||
# 认证配置(必需)
|
||||
# =============================================================================
|
||||
# JWT 签名密钥(必需)
|
||||
# 建议使用随机生成的强密钥,例如: openssl rand -hex 32
|
||||
# 生产环境必须更换为强随机字符串
|
||||
SECRET_KEY=cf47555c7ecbe5ddb7fd2113c59e08a8bcb110810c42f7c644e06a5acc898608
|
||||
# JWT 算法(可选,默认: HS256)
|
||||
ALGORITHM=HS256
|
||||
# 访问令牌过期时间(可选,默认: 120 分钟,即 2 小时)
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES=120
|
||||
@@ -25,14 +25,15 @@ COPY requirements.txt .
|
||||
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 . .
|
||||
|
||||
# 复制生产环境配置文件到镜像中
|
||||
# 将 .env.production 复制为 .env,供应用运行时使用
|
||||
# 注意:构建前请确保 .env.production 文件存在
|
||||
# 这一步放在最后,确保覆盖任何开发环境的 .env 文件
|
||||
COPY .env.production ./.env
|
||||
|
||||
# 创建非root用户
|
||||
RUN useradd -m -u 1000 appuser && \
|
||||
chown -R appuser:appuser /app
|
||||
@@ -48,4 +49,4 @@ 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"]
|
||||
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||
Reference in New Issue
Block a user