烧瓶在容器内工作,但在我将其转发时不起作用 [英] flask is working inside container but not when I port forward it

查看:43
本文介绍了烧瓶在容器内工作,但在我将其转发时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要尝试的是:我正在尝试使用docker运行一个简单的flask应用。使用网站作为参考。

What I'm trying: I'm trying to run a simple flask app using docker. Using this site as a reference.

我的dockerfile:

My dockerfile:

FROM ubuntu:latest
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
COPY ./app /app
WORKDIR /app
RUN pip install -r "requirements.txt"
ENTRYPOINT ["python"]
CMD ["app.py"]

Python文件:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello World!'

if __name__ == '__main__':
    app.run(port=5000, debug=True)

requirements.txt :

requirements.txt:

Flask==1.0.2

我在做什么:


  • 使用 docker build构建映像- t simple-flask2。

  • 然后,使用 docker run -p 5000:5000 simple-flask2

  • 当我去 lo时calhost:5000 什么也没出现。

  • 因此,我使用 docker exec -it 3be bash &然后执行 curl localhost:5000 。令我惊讶的是,它正在容器内工作

  • building image using docker build -t simple-flask2 .
  • then, starting the container using docker run -p 5000:5000 simple-flask2
  • the when I go to localhost:5000 nothing appears.
  • So, I opened the container's terminal using docker exec -it 3be bash & then did curl localhost:5000. To my surprise it was working inside the container.

有人可以指出我在想什么吗?我对此很陌生。 TIA:)

Can anyone please point out what am I missing? I'm pretty new to this. TIA :)

推荐答案

我猜它只在本地主机上运行(默认值 host ='127.0 .0.1')。尝试使用所有接口( host ='0.0.0.0'):

I guess it is running only on the localhost (default value host='127.0.0.1') in the container. Try to use all interfaces (host='0.0.0.0'):

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

这篇关于烧瓶在容器内工作,但在我将其转发时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆