build(deployment): 优化 Docker 部署配置
- 新增 .dockerignore 文件,排除不必要的文件和目录 - 修改 Dockerfile,使用环境变量 SERVER_PORT 替代固定端口 5000 - 更新 docker-compose.yaml,添加容器名称、调整端口映射并启用自动重启 - 优化 main.py 中的 chat_completions 函数逻辑
This commit is contained in:
parent
2d3bc1971c
commit
88b3259ba0
5
.dockerignore
Normal file
5
.dockerignore
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
.env.example
|
||||||
|
.git
|
||||||
|
.gitignore
|
||||||
|
*.md
|
||||||
|
images/
|
||||||
@ -15,11 +15,11 @@ COPY . .
|
|||||||
|
|
||||||
# 暴露 Flask 应用监听的端口
|
# 暴露 Flask 应用监听的端口
|
||||||
# 注意:EXPOSE 只是声明端口,不会实际发布端口,需要在运行容器时进行端口映射
|
# 注意:EXPOSE 只是声明端口,不会实际发布端口,需要在运行容器时进行端口映射
|
||||||
EXPOSE 5000
|
EXPOSE ${SERVER_PORT}
|
||||||
|
|
||||||
# 定义容器启动时执行的命令
|
# 定义容器启动时执行的命令
|
||||||
# 这里使用 Gunicorn 作为生产级的 WSGI 服务器,而不是 Flask 内置的开发服务器
|
# 这里使用 Gunicorn 作为生产级的 WSGI 服务器,而不是 Flask 内置的开发服务器
|
||||||
# 你需要先在 requirements.txt 中添加 gunicorn
|
# 你需要先在 requirements.txt 中添加 gunicorn
|
||||||
# CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app:app"]
|
# CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app:app"]
|
||||||
# 如果只是测试或简单应用,也可以直接用 Flask 开发服务器
|
# 如果只是测试或简单应用,也可以直接用 Flask 开发服务器
|
||||||
CMD ["python", "main.py"]
|
CMD ["python", "main.py"]
|
||||||
|
|||||||
@ -1,22 +1,11 @@
|
|||||||
services:
|
services:
|
||||||
web:
|
web:
|
||||||
build: ./ # 指定 Dockerfile 的构建上下文路径
|
build: ./ # 指定 Dockerfile 的构建上下文路径
|
||||||
|
container_name: OpenDify
|
||||||
ports:
|
ports:
|
||||||
- "181:5000" # 端口映射:主机端口:容器端口
|
- "${SERVER_PORT}:${SERVER_PORT}" # 端口映射:主机端口:容器端口
|
||||||
|
restart: always
|
||||||
volumes:
|
volumes:
|
||||||
- ./:/app # 挂载本地代码到容器,方便开发时修改代码立即生效
|
- ./:/app # 挂载本地代码到容器,方便开发时修改代码立即生效
|
||||||
environment: # 环境变量,例如 Flask 的开发模式
|
environment: # 环境变量,例如 Flask 的开发模式
|
||||||
FLASK_ENV: development
|
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:
|
|
||||||
8
main.py
8
main.py
@ -517,14 +517,14 @@ def chat_completions():
|
|||||||
) as response:
|
) as response:
|
||||||
generate.message_id = None
|
generate.message_id = None
|
||||||
buffer = ""
|
buffer = ""
|
||||||
|
|
||||||
for raw_bytes in response.iter_raw():
|
for raw_bytes in response.iter_raw():
|
||||||
if not raw_bytes:
|
if not raw_bytes:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
buffer += raw_bytes.decode('utf-8')
|
buffer += raw_bytes.decode('utf-8')
|
||||||
|
|
||||||
while '\n' in buffer:
|
while '\n' in buffer:
|
||||||
line, buffer = buffer.split('\n', 1)
|
line, buffer = buffer.split('\n', 1)
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
@ -558,8 +558,10 @@ def chat_completions():
|
|||||||
# time.sleep(delay)
|
# time.sleep(delay)
|
||||||
if current_answer == "<think>\n\n":
|
if current_answer == "<think>\n\n":
|
||||||
current_answer = "<think>"
|
current_answer = "<think>"
|
||||||
|
yield send_char(current_answer, message_id)
|
||||||
|
current_answer = "\n"
|
||||||
yield send_char(current_answer, message_id)
|
yield send_char(current_answer, message_id)
|
||||||
|
logger.info(f"message_id: {message_id}")
|
||||||
# 立即继续处理下一个请求
|
# 立即继续处理下一个请求
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user