gpt4 book ai didi

python - Docker无法连接到Flask

转载 作者:行者123 更新时间:2023-12-02 19:00:40 27 4
gpt4 key购买 nike

我正在尝试将一个简单的flask应用程序移至docker,但是无法从浏览器访问我的flask应用程序。

项目树

├───project
│ │ dockerfile
│ │ requirements.txt
│ │
│ └───app
│ server.py

docker文件
FROM ubuntu:latest

RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential

ADD . /app
WORKDIR /app

RUN pip install -r requirements.txt

ENTRYPOINT ["python"]
CMD ["app/server.py", "--host=0.0.0.0"]


requirements.txt
Flask==0.12.2

server.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Flask Dockerized'

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')

docker 命令:
docker build -t test-server:latest .
docker run -d -p 5000:5000 test-server

docker 日志
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 224-123-338

docker --version: Docker version 19.03.5, build 633a0ea
docker ps
1c54ccfc61bf        test-server         "python app/server.p…"   6 seconds ago       Up 5 seconds        0.0.0.0:5000->5000/tcp   elated_solomon

最佳答案

通过设置app.run(debug=True)将此设置为在 Debug模式下运行,表明您认为正在设置的主机已被覆盖:

❰mm92400❙~/test❱✔≻ docker run -it -p 5000:5000 testimage
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 963-222-479


因此,将主机设置为 app.run(host='0.0.0.0'):

❰mm92400❙~/test❱✔≻ docker run -it -p 5000:5000 testimage
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 200-559-808

和浏览器连接正常

172.17.0.1 - - [06/Mar/2020 15:59:42] "GET / HTTP/1.1" 200 -

关于python - Docker无法连接到Flask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60567391/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com