title: Request to Flask Application Does Not Get Through to Docker Container
body: <p>I have written a «Hello World» application using Python (3.7.4) and Flask (<code>hello.py</code>):</p>
  
  <pre><code>from flask import Flask
  
  app = Flask(__name__)
  
  @app.route('/')
  def hello_world():
      return 'Hello, World!'
  
  if __name__ == '__main__':
      app.run(host='0.0.0.0')
  </code></pre>
  
  <p>I use the following versions of Flask and gunicorn (<code>requirements.txt</code>):</p>
  
  <pre><code>Flask==1.1.1
  gunicorn==19.9.0
  </code></pre>
  
  <p>Which I install as follows:</p>
  
  <pre><code>$ sudo pip install -r requirements.txt
  </code></pre>
  
  <p>Since I'm running the example on Arch Linux, <code>pip</code> is actually <code>pip3</code>.</p>
  
  <p>When I start the application:</p>
  
  <pre><code>$ gunicorn hello:app
  </code></pre>
  
  <p>The application works as expected:</p>
  
  <pre><code>$ curl localhost:8000
  Hello, World!
  </code></pre>
  
  <p>Now I want to run it using Docker (Version 19.03.3-ce), for which I have written this <code>Dockerfile</code>:</p>
  
  <pre><code>FROM python:3.7.4
  
  RUN mkdir /app
  WORKDIR /app
  
  COPY requirements.txt /app/
  RUN pip install -r requirements.txt
  
  COPY hello.py /app
  
  EXPOSE 8000
  
  CMD ["gunicorn", "hello:app", "--log-file=-"]
  </code></pre>
  
  <p>I build the image and run the container with a shell script (<code>run.sh</code>):</p>
  
  <pre><code>#!/bin/sh
  
  docker build . -t flaskdemo
  docker run -it --rm --name flaskdemoapp -p 8000:8000 flaskdemo
  </code></pre>
  
  <p>Which works as expected:</p>
  
  <pre><code>$ ./run.sh
  Sending build context to Docker daemon  66.05kB
  Step 1/8 : FROM python:3.7.4
   ---&gt; 9fa56d0addae
  Step 2/8 : RUN mkdir /app
   ---&gt; Using cache
   ---&gt; 81a38303aabd
  Step 3/8 : WORKDIR /app
   ---&gt; Using cache
   ---&gt; a73abfac4cdf
  Step 4/8 : COPY requirements.txt /app/
   ---&gt; Using cache
   ---&gt; e7507f42bbae
  Step 5/8 : RUN pip install -r requirements.txt
   ---&gt; Using cache
   ---&gt; 06c4adb1310b
  Step 6/8 : COPY hello.py /app
   ---&gt; Using cache
   ---&gt; fa5d374f6c93
  Step 7/8 : EXPOSE 8000
   ---&gt; Using cache
   ---&gt; df5e0d5ada7d
  Step 8/8 : CMD ["gunicorn", "hello:app", "--log-file=-"]
   ---&gt; Using cache
   ---&gt; 08babb3f604a
  Successfully built 08babb3f604a
  Successfully tagged flaskdemo:latest
  [2019-10-19 13:09:43 +0000] [1] [INFO] Starting gunicorn 19.9.0
  [2019-10-19 13:09:43 +0000] [1] [INFO] Listening at: http://127.0.0.1:8000 (1)
  [2019-10-19 13:09:43 +0000] [1] [INFO] Using worker: sync
  [2019-10-19 13:09:43 +0000] [8] [INFO] Booting worker with pid: 8
  </code></pre>
  
  <p>Unfortunately, the request does not come through to the container:</p>
  
  <pre><code>$ curl localhost:8000
  curl: (56) Recv failure: Connection reset by peer
  </code></pre>
  
  <p>There is no additional log line from the container.</p>
  
  <p>Docker works fine on my machine in general. For example, when I run <code>httpd</code>:</p>
  
  <pre><code>$ docker run -p 8080:80 httpd
  </code></pre>
  
  <p>I get a response to my request:</p>
  
  <pre><code>$ curl localhost:8080
  &lt;html&gt;&lt;body&gt;&lt;h1&gt;It works!&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>So I guess there must be something wrong with my <code>Dockerfile</code> or the way I run the container.</p>
  
  <p>PS: Feel free to clone the code from <a href="https://github.com/patrickbucher/flaskdemoapp.git" rel="nofollow noreferrer">my GitHub repo</a></p>