HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythondockerMajor

Dockerized Flask: Connection reset by peer

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
flaskpeerdockerizedresetconnection

Problem

Trying to connect to a dockerized Flask app fails with error 104, 'Connection reset by peer' using this minimal example:

app.py:

from flask import Flask
app = Flask(__name__)

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

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


Dockerfile:

FROM python:alpine
RUN pip install flask
COPY . /src/
EXPOSE 5000
ENTRYPOINT ["python", "/src/app.py"]


docker-compose.yml:

…
test:
    build: .
    ports:
        - 127.0.0.1:5000:5000


Flask app seems to be running as expected:

$ docker logs test 
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)


Trying to connect from outside fails:

$ http http://127.0.0.1:5000/
http: error: ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer')) while doing GET request to URL: http://127.0.0.1:5000/


Any ideas, why I can't get to see "Hello World!" here?

Solution

Trying to connect from outside fails

Are you actually connecting from outside? Flask is binding to localhost (127.0.0.1) and that will only be reachable from within the container. If you're on your local machine, you'll need flask to bind to all IP's:

from flask import Flask
app = Flask(__name__)

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

if __name__ == "__main__":
    app.run(host='0.0.0.0')


That works for me successfully.

Code Snippets

from flask import Flask
app = Flask(__name__)

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

if __name__ == "__main__":
    app.run(host='0.0.0.0')

Context

StackExchange DevOps Q#3380, answer score: 27

Revisions (0)

No revisions yet.