From 88b3259ba0651cac92b646eafbbac85cb7a00bd8 Mon Sep 17 00:00:00 2001 From: wenyongda Date: Fri, 27 Jun 2025 13:48:44 +0800 Subject: [PATCH] =?UTF-8?q?build(deployment):=20=E4=BC=98=E5=8C=96=20Docke?= =?UTF-8?q?r=20=E9=83=A8=E7=BD=B2=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 .dockerignore 文件,排除不必要的文件和目录 - 修改 Dockerfile,使用环境变量 SERVER_PORT 替代固定端口 5000 - 更新 docker-compose.yaml,添加容器名称、调整端口映射并启用自动重启 - 优化 main.py 中的 chat_completions 函数逻辑 --- .dockerignore | 5 +++++ Dockerfile | 4 ++-- docker-compose.yaml | 19 ++++--------------- main.py | 8 +++++--- 4 files changed, 16 insertions(+), 20 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..85a2fce --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.env.example +.git +.gitignore +*.md +images/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index e19d0dd..5919dda 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,11 +15,11 @@ COPY . . # 暴露 Flask 应用监听的端口 # 注意:EXPOSE 只是声明端口,不会实际发布端口,需要在运行容器时进行端口映射 -EXPOSE 5000 +EXPOSE ${SERVER_PORT} # 定义容器启动时执行的命令 # 这里使用 Gunicorn 作为生产级的 WSGI 服务器,而不是 Flask 内置的开发服务器 # 你需要先在 requirements.txt 中添加 gunicorn # CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app:app"] # 如果只是测试或简单应用,也可以直接用 Flask 开发服务器 -CMD ["python", "main.py"] \ No newline at end of file +CMD ["python", "main.py"] diff --git a/docker-compose.yaml b/docker-compose.yaml index 4ed80a8..427b9fd 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -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: \ No newline at end of file + FLASK_ENV: development \ No newline at end of file diff --git a/main.py b/main.py index 25ce6de..0e726f8 100644 --- a/main.py +++ b/main.py @@ -517,14 +517,14 @@ def chat_completions(): ) as response: generate.message_id = None buffer = "" - + for raw_bytes in response.iter_raw(): if not raw_bytes: continue try: buffer += raw_bytes.decode('utf-8') - + while '\n' in buffer: line, buffer = buffer.split('\n', 1) line = line.strip() @@ -558,8 +558,10 @@ def chat_completions(): # time.sleep(delay) if current_answer == "\n\n": current_answer = "" + 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