在Docker中运行Flask应用程序不在浏览器中显示页面。

huangapple go评论80阅读模式
英文:

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(&#39;/&#39;)
def inicio():
    return render_template(&#39;index.html&#39;)

if __name__==&#39;__main__&#39;:
    app.run(debug=True)

index.html

&lt;html&gt;
    &lt;head&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;p&gt;This is a beautiful world indeed&lt;/p&gt;
    &lt;/body&gt;
&lt;/html&gt;

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 [ &quot;python&quot;, &quot;app.py&quot; ]

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.

  1. You should be binding to 0.0.0.0 if you want the container to be accessible from the outside
  2. You should bind the port when running docker

So modify the Python file to have the following:

app.run(debug=True, host=&#39;0.0.0.0&#39;, 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

huangapple
  • 本文由 发表于 2023年7月27日 22:21:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定