docker compose中的Flask应用返回空响应

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

docker compose Flask app returning empty response

问题

我试图使用docker-compose玩一个包含flask的docker容器。这个flask应用是一个hello world应用,在我在主机上测试时可以正常工作。

docker-compose文件如下:

version: '3'
services:
  web:
    image: ubuntu
    build:
      context: ./
      dockerfile: Dockerfile
    ports:
      - "6000:5000"

我的Dockerfile如下:

FROM ubuntu

RUN apt-get update
RUN apt-get install -y python3 python3-pip

RUN pip3 install Flask

COPY app.py .

EXPOSE 5000

CMD ["python3", "app.py"]

这个hello world应用看起来是这样的:

from flask import Flask, request

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello, World!"

if __name__ == "__main__":
    app.run(debug=True)

当我使用docker-compose up -d启动容器时,没有错误。当我在localhost:6000上使用curl时,我得到了这个:

curl -X PUT localhost:6000
curl: (52) Empty reply from server

看起来应用正在响应,但与我在主机上运行时的响应方式不同,而是在curl时返回一个空字符串而不是"hello world"。我做错了什么?

英文:

I am trying to play with a dockerized flask container using docker-compose. The flask app is a hello world app that works correctly when I test it on my host.

The docker-compose file looks like this:

version: '3'
services:
  web:
    image: ubuntu
    build:
      context: ./
      dockerfile: Dockerfile
    ports:
      - "6000:5000"

My dockerfile looks like this:

FROM ubuntu

RUN apt-get update
RUN apt-get install -y python3 python3-pip

RUN pip3 install Flask

COPY app.py .

EXPOSE 5000

CMD ["python3", "app.py"]

The hello world app looks like this:

from flask import Flask, request

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello, World!"

if __name__ == "__main__":
    app.run(debug=True)

When I bring up the containers using docker-compose up -d, there is no error. When I curl to localhost:6000, I get this :

curl -X PUT localhost:6000
curl: (52) Empty reply from server

It seems like the app is responding but not how it responds when I run it on a my host and just returns an empty string instead of "hello world" when I curl to it. What am I doing wrong?

答案1

得分: 1

  1. 你需要使你的 Flask 应用可以外部访问:app.run(host="0.0.0.0", debug=True)
  2. 你正在使用 curl 发送 PUT 请求,但你的应用只允许 GET 请求(这是默认设置,你可以在路由中添加允许的方法,像这样:@app.route("/", methods=["GET", "PUT"]))。
  3. 这个不是一个实际的 bug,而是一个建议:如果你需要你的应用能够被 Web 浏览器访问,请不要使用 6000 端口。至少其中一些浏览器会显示 ERR_UNSAFE_PORT 错误并阻止请求。不过,它会通过 curl 工作。
英文:

There are several issues here:

  1. You need to make your flask app accessible externally: app.run(host="0.0.0.0", debug=True).
  2. You are sending a PUT request from curl but your app only allows GET (this is the default, you can add allowed methods to your route like this: @app.route("/", methods=["GET", "PUT"]).
  3. This one is not actually a bug but rather a recommendation: don't use port 6000 if you need your app to be accessible via web browsers. At least some of them will display an ERR_UNSAFE_PORT error and will block the request. It will work via curl though.

huangapple
  • 本文由 发表于 2023年2月16日 12:48:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467961.html
匿名

发表评论

匿名网友

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

确定