Answers: <blockquote>
    <p>Trying to connect from outside fails</p>
  </blockquote>
  
  <p>Are you actually connecting from <em>outside</em>? 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:</p>
  
  <pre><code>from flask import Flask
  app = Flask(__name__)
  
  @app.route('/')
  def hello():
      return "Hello World!"
  
  if __name__ == "__main__":
      app.run(host='0.0.0.0')
  </code></pre>
  
  <p>That works for me successfully.</p>
  
Answers: CC BY-SA 3.0
Answers: 2018-02-20T18:00:50.723Z
Answers: Thats right. Never thought this is actually a python or flask problem, as I just copied it from [the docs](https://pypi.python.org/pypi/Flask). Thx
Answers: mcnesium
Answers: CC BY-SA 4.0
Answers: 2019-04-15T07:18:54.533Z
Answers: Or with gunicorn: `gunicorn --bind 0.0.0.0:5000 app:app`
Answers: Martin Thoma
Answers: CC BY-SA 4.0
Answers: 2019-09-07T10:46:39.213Z
Answers: If you're using `flask run`, use `--host=0.0.0.0`. Or in the Dockerfile: `CMD ["flask", "run", "--host=0.0.0.0"]`
Answers: sebas
Answers: CC BY-SA 4.0
Answers: 2019-11-19T12:50:25.417Z
Answers: I have been looking for this for 4 days >D lol
Answers: eleijonmarck
Answers: CC BY-SA 4.0
Answers: 2020-09-02T18:55:01.457Z
Answers: +1 You saved my day :)  So happy to find this answer after 20 min and not 4 days
Answers: lanenok
Answers: CC BY-SA 3.0
Answers: 2018-02-20T17:04:14.977Z
Answers: 2018-02-23T15:27:27.460Z
Answers: 2018-02-23T15:27:27.460Z
Answers: BoomShadow
Body: <p>Trying to connect to a dockerized Flask app fails with error 104, 'Connection reset by peer' using this minimal example:</p>
  
  <p>app.py:</p>
  
  <pre><code>from flask import Flask
  app = Flask(__name__)
  
  @app.route("/")
  def hello():
      return "Hello World!"
  
  if __name__ == "__main__":
      app.run()
  </code></pre>
  
  <p>Dockerfile:</p>
  
  <pre><code>FROM python:alpine
  RUN pip install flask
  COPY . /src/
  EXPOSE 5000
  ENTRYPOINT ["python", "/src/app.py"]
  </code></pre>
  
  <p>docker-compose.yml:</p>
  
  <pre><code>…
  test:
      build: .
      ports:
          - 127.0.0.1:5000:5000
  </code></pre>
  
  <p>Flask app seems to be running as expected:</p>
  
  <pre><code>$ docker logs test 
   * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
  </code></pre>
  
  <p>Trying to connect from outside fails:</p>
  
  <pre><code>$ 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/
  </code></pre>
  
  <p>Any ideas, why I can't get to see "Hello World!" here?</p>
  
Tags: <docker><python>
Title: Dockerized Flask: Connection reset by peer
UserDisplayName: mcnesium
ThreadText: # Dockerized Flask: Connection reset by peer
  
  (Asked by: username_0 on 2018-02-20)
  
  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?
  
  **Tags:** docker, python
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-02-20. Score: 27 | Accepted answer
  
  > 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.
  
  > **Comments:**
  > **username_0**: Thats right. Never thought this is actually a python or flask problem, as I just copied it from [the docs](https://pypi.python.org/pypi/Flask). Thx  
  > **username_2**: Or with gunicorn: `gunicorn --bind 0.0.0.0:5000 app:app`  
  > **username_3**: If you're using `flask run`, use `--host=0.0.0.0`. Or in the Dockerfile: `CMD ["flask", "run", "--host=0.0.0.0"]`  
  > **username_4**: I have been looking for this for 4 days >D lol  
  > **username_5**: +1 You saved my day :)  So happy to find this answer after 20 min and not 4 days  
  
  ---
  
  