diff --git a/.dockerignore b/.dockerignore index e311563..716de06 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,4 @@ # 构建的相关文件 -deploy README.md # Python 相关文件 @@ -10,6 +9,3 @@ venv/ .idea/ *.log *.db - -# 允许拷贝的文件 -!deploy/start.sh \ No newline at end of file diff --git a/deploy/Dockerfile b/deploy/Dockerfile deleted file mode 100644 index 16d4530..0000000 --- a/deploy/Dockerfile +++ /dev/null @@ -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 diff --git a/deploy/dev/Dockerfile b/deploy/dev/Dockerfile new file mode 100644 index 0000000..66f86b2 --- /dev/null +++ b/deploy/dev/Dockerfile @@ -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"] \ No newline at end of file diff --git a/deploy/dev/docker-compose.yaml b/deploy/dev/docker-compose.yaml new file mode 100644 index 0000000..3d1270a --- /dev/null +++ b/deploy/dev/docker-compose.yaml @@ -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 \ No newline at end of file diff --git a/deploy/dev/start.sh b/deploy/dev/start.sh new file mode 100644 index 0000000..86ae3a4 --- /dev/null +++ b/deploy/dev/start.sh @@ -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 \ No newline at end of file diff --git a/deploy/docker-compose.yaml b/deploy/docker-compose.yaml deleted file mode 100644 index 7d357d8..0000000 --- a/deploy/docker-compose.yaml +++ /dev/null @@ -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 \ No newline at end of file diff --git a/deploy/start.sh b/deploy/start.sh deleted file mode 100644 index 1171931..0000000 --- a/deploy/start.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -# 这里可以设置同步时间等操作 - -# 运行程序 -echo "Starting application..." - -# 这里可以修改运行命令 -gunicorn -b 0.0.0.0:5000 app:app \ No newline at end of file