docker compose Flask app returning empty response
Question:
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?
Answers:
There are several issues here:
- You need to make your flask app accessible externally:
app.run(host="0.0.0.0", debug=True)
.
- 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"])
.
- 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.
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?
There are several issues here:
- You need to make your flask app accessible externally:
app.run(host="0.0.0.0", debug=True)
. - 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"])
. - 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.