问题描述
创建了一个python测试文件:
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/', methods=['GET'])
def start_conversation():
"""
创建新对话线程
返回:{ "thread_id": "唯一对话ID" }
"""
try:
return {"message": "Hello World"}
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(host='localhost', port=8099, debug=True, use_reloader=False)
部署到了docker上,并打开了8099端口,运行后也执行成功了,docker提示:
2025-04-22 16:28:48 WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
2025-04-22 16:28:48 * Running on http://localhost:8099
2025-04-22 16:28:48 Press CTRL+C to quit
2025-04-22 16:28:47 * Serving Flask app 'main'
2025-04-22 16:28:47 * Debug mode: on
但是在访问 http://localhost:8099/ 时提示拒绝访问。
解决方案:
-
验证容器网络:
进入容器内部测试是否能访问服务:docker exec -it your-container-name bash curl http://localhost:8099
如果输出:
bash: curl: command not found
说明容器没有crul,需要安装:
进入容器内部:docker exec -it <容器ID> bash apt-get update && apt-get install -y curl curl http://localhost:8099
-
检查 Docker Desktop 设置
- 打开 Docker Desktop → Settings → Resources → Network。
- 确保 DNS 服务器 设置为
自动
。 - 尝试 Reset Docker(重置 Docker 网络)。
补充容器ID获取方法
运行:
docker ps
CONTAINER ID为容器ID:
CONTAINER ID IMAGE PORTS NAMES
5865d94c6a2a your-image 0.0.0.0:8099->8099/tcp your-container
如果没有容器你的容器,说明未正确启动。