英文:
Running a Flask App in docker doesn't show a page in browser
问题
我创建了以下的Flask应用:
app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def inicio():
return render_template('index.html')
if __name__=='__main__':
app.run(debug=True)
index.html
<html>
<head>
</head>
<body>
<p>这确实是一个美丽的世界</p>
</body>
</html>
Dockerfile
FROM python:3.9-alpine
COPY . app
COPY ./requirements.txt /app/requirements.txt
WORKDIR app
EXPOSE 5000:5000
RUN pip install -r requirements.txt
CMD [ "python", "app.py" ]
然后我创建了镜像并运行它:
docker build -t myimage .
docker run -t -i myimage
但当我收到链接后,点击它Running on http://127.0.0.1:5000
,它会打开浏览器。然而,什么都没有显示。我做错了什么吗?
英文:
I created the following Flask app:
app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def inicio():
return render_template('index.html')
if __name__=='__main__':
app.run(debug=True)
index.html
<html>
<head>
</head>
<body>
<p>This is a beautiful world indeed</p>
</body>
</html>
Dockerfile
FROM python:3.9-alpine
COPY . app
COPY ./requirements.txt /app/requirements.txt
WORKDIR app
EXPOSE 5000:5000
RUN pip install -r requirements.txt
CMD [ "python", "app.py" ]
Then I created the image and run it:
docker build -t myimage .
docker run -t -i myimage
But when I receive the link, I click on it Running on http://127.0.0.1:5000
and it takes me to a browser. However, nothing displays. Is there anything I am doing wrong?
答案1
得分: 2
以下是翻译好的部分:
这里有两件事你需要修复。
1. 如果你想让容器可以从外部访问,你应该绑定到 `0.0.0.0`。
2. 在运行 Docker 时,你应该绑定端口。
所以修改 Python 文件如下:
```python
app.run(debug=True, host='0.0.0.0', port=5000)
然后:
$ docker build -t myimage .
$ docker run -t -i -p 5000:5000 myimage
英文:
There are two things you need to fix here.
- You should be binding to
0.0.0.0
if you want the container to be accessible from the outside - You should bind the port when running docker
So modify the Python file to have the following:
app.run(debug=True, host='0.0.0.0', port=5000)
and then:
$ docker build -t myimage .
$ docker run -t -i -p 5000:5000 myimage
答案2
得分: 1
EXPOSE
关键字实际上不会在运行时发布端口。
尝试:
docker run -t -i myimage -p 5000:5000
英文:
The EXPOSE
keyword doesn't actually make it so the port is published at runtime
Try:
docker run -t -i myimage -p 5000:5000
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论