区分测试生产环境部署配置

This commit is contained in:
不胜舟
2026-01-16 19:21:30 +08:00
parent 372b0030c3
commit 97eda740fe
7 changed files with 79 additions and 61 deletions
-4
View File
@@ -1,5 +1,4 @@
# 构建的相关文件 # 构建的相关文件
deploy
README.md README.md
# Python 相关文件 # Python 相关文件
@@ -10,6 +9,3 @@ venv/
.idea/ .idea/
*.log *.log
*.db *.db
# 允许拷贝的文件
!deploy/start.sh
-31
View File
@@ -1,31 +0,0 @@
FROM python:3.11-bookworm
# 复制项目文件
RUN mkdir -p /app
COPY . /app/
COPY ./dockerdata/start.sh /app/start.sh
WORKDIR /app/
# 设置必备项
ENV TIME_ZONE Asia/Shanghai
RUN echo "${TIME_ZONE}" > /etc/timezone \
&& ln -sf /usr/share/zoneinfo/${TIME_ZONE} /etc/localtime
# 安装必备库
RUN python3 -m pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
# 安装
RUN python3 -m pip install gunicorn -i https://pypi.tuna.tsinghua.edu.cn/simple/
# 初始化数据库
RUN flask db init
RUN flask db migrate
RUN flask db upgrade
RUN flask admin init
# 运行命令
RUN chmod +x start.sh
# 保持执行
CMD /bin/sh
+25
View File
@@ -0,0 +1,25 @@
FROM python:3.11-bookworm
# 设置时区
ENV TIME_ZONE Asia/Shanghai
RUN echo "${TIME_ZONE}" > /etc/timezone \
&& ln -sf /usr/share/zoneinfo/${TIME_ZONE} /etc/localtime
WORKDIR /app
# 1. [优化] 先只拷贝依赖文件,利用 Docker 缓存
# 只要 requirements.txt 没变,这一步和 pip install 就会直接用缓存,极大加快构建速度
COPY requirements.txt /app/
# 2. 安装依赖 (使用清华源)
RUN python3 -m pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/ \
&& python3 -m pip install --no-cache-dir gunicorn -i https://pypi.tuna.tsinghua.edu.cn/simple/
# 3. 拷贝其余项目代码
COPY . /app/
# 4. 确保脚本有执行权限 (直接处理源路径,防止移动失败)
RUN chmod +x /app/deploy/dev/start.sh
# 5. 指定启动入口
CMD ["/bin/bash", "/app/deploy/dev/start.sh"]
+29
View File
@@ -0,0 +1,29 @@
version: '3.8'
services:
flask:
build:
context: ../.. # 指向项目根目录 (pear-admin-flask)
dockerfile: deploy/dev/Dockerfile
container_name: flask_dev
restart: always
ports:
- "5000:5000"
# [优化] 挂载卷:将本地代码映射到容器内部
# 效果:你在 PyCharm 修改代码,容器里立马生效,不需要重启容器
volumes:
- ../..:/app
# [优化] 环境变量
environment:
- FLASK_DEBUG=0 # 是否开启 Flask 调试模式
- FLASK_APP=app.py
# 网络设置
networks:
- dev_network
networks:
dev_network:
driver: bridge
+25
View File
@@ -0,0 +1,25 @@
#!/bin/bash
# 确保进入工作目录
cd /app
echo "=== [Step 1] 检查并初始化数据库 ==="
# 使用 || true 确保即使命令报错(例如migrations文件夹已存在)也不会中断脚本
flask db init || true
flask db migrate || true
flask db upgrade || true
# (可选) 每次重启尝试初始化数据,如果已存在则会被代码逻辑跳过
flask admin init || true
echo "=== [Step 2] 启动服务 (开发模式) ==="
# [优化] 增加 --reload 参数
# 配合 docker-compose 的 volumes,当检测到代码变化时,Gunicorn 会自动重启 worker
# access-logfile - 表示将日志输出到控制台
exec gunicorn --bind 0.0.0.0:5000 \
--workers 2 \
--reload \
--access-logfile - \
--error-logfile - \
app:app
-17
View File
@@ -1,17 +0,0 @@
version: '3'
services:
flask:
build:
context: ..
dockerfile: dockerdata/Dockerfile
container_name: flask
ports:
- "5000:5000"
restart: always
command: sh -c "/app/start.sh"
networks:
- bridge_network
networks:
bridge_network:
driver: bridge
-9
View File
@@ -1,9 +0,0 @@
#!/bin/bash
# 这里可以设置同步时间等操作
# 运行程序
echo "Starting application..."
# 这里可以修改运行命令
gunicorn -b 0.0.0.0:5000 app:app