build(deployment): 优化 Docker 部署配置

- 新增 .dockerignore 文件,排除不必要的文件和目录
- 修改 Dockerfile,使用环境变量 SERVER_PORT 替代固定端口 5000
- 更新 docker-compose.yaml,添加容器名称、调整端口映射并启用自动重启
- 优化 main.py 中的 chat_completions 函数逻辑
This commit is contained in:
wenyongda 2025-06-27 13:48:44 +08:00
parent 2d3bc1971c
commit 88b3259ba0
4 changed files with 16 additions and 20 deletions

5
.dockerignore Normal file
View File

@ -0,0 +1,5 @@
.env.example
.git
.gitignore
*.md
images/

View File

@ -15,7 +15,7 @@ COPY . .
# 暴露 Flask 应用监听的端口
# 注意EXPOSE 只是声明端口,不会实际发布端口,需要在运行容器时进行端口映射
EXPOSE 5000
EXPOSE ${SERVER_PORT}
# 定义容器启动时执行的命令
# 这里使用 Gunicorn 作为生产级的 WSGI 服务器,而不是 Flask 内置的开发服务器

View File

@ -1,22 +1,11 @@
services:
web:
build: ./ # 指定 Dockerfile 的构建上下文路径
container_name: OpenDify
ports:
- "181:5000" # 端口映射:主机端口:容器端口
- "${SERVER_PORT}:${SERVER_PORT}" # 端口映射:主机端口:容器端口
restart: always
volumes:
- ./:/app # 挂载本地代码到容器,方便开发时修改代码立即生效
environment: # 环境变量,例如 Flask 的开发模式
FLASK_ENV: development
# depends_on: # 如果有其他服务,例如数据库,可以添加依赖
# - db
# db:
# image: postgres:13-alpine
# environment:
# POSTGRES_DB: mydatabase
# POSTGRES_USER: user
# POSTGRES_PASSWORD: password
# volumes:
# - db_data:/var/lib/postgresql/data
# volumes: # 如果有持久化数据需求,例如数据库数据
# db_data:

View File

@ -558,8 +558,10 @@ def chat_completions():
# time.sleep(delay)
if current_answer == "<think>\n\n":
current_answer = "<think>"
yield send_char(current_answer, message_id)
current_answer = "\n"
yield send_char(current_answer, message_id)
logger.info(f"message_id: {message_id}")
# 立即继续处理下一个请求
continue