title: Running Gunicorn Flask app in Docker [CRITICAL] WORKER TIMEOUT when starting up
body: <p>I want to run a Flask web services app with gunicorn in Docker. Upon startup, the app loads a large machine learning model. </p>
  
  <p>However, when I run gunicorn within Docker I received the following timeouts and it just keeps spawning workers.</p>
  
  <pre><code>[2019-12-12 21:52:42 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:1198) 
  [2019-12-12 21:52:42 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:1204)
  [2019-12-12 21:52:42 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:1210)
  [2019-12-12 21:52:42 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:1211)
  [2019-12-12 21:52:42 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:1222)
  [2019-12-12 21:52:42 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:1223)
  [2019-12-12 21:52:42 +0000] [1264] [INFO] Booting worker with pid: 1264
  [2019-12-12 21:52:42 +0000] [1265] [INFO] Booting worker with pid: 1265
  [2019-12-12 21:52:42 +0000] [1276] [INFO] Booting worker with pid: 1276
  [2019-12-12 21:52:42 +0000] [1277] [INFO] Booting worker with pid: 1277
  [2019-12-12 21:52:42 +0000] [1278] [INFO] Booting worker with pid: 1278
  [2019-12-12 21:52:42 +0000] [1289] [INFO] Booting worker with pid: 1289
  </code></pre>
  
  <p>Running it as a flask app within Docker or running the flask app with (or without) gunicorn from the command line works fine. It also works with gunicorn if I remove the machine learning model.</p>
  
  <p>For example:</p>
  
  <pre><code>$python app.py
  
  $gunicorn -b 0.0.0.0:8080 --workers=2 --threads=4 app:app
  
  $gunicorn app:app
  </code></pre>
  
  <p>Here is my Dockerfile with the Flask development server. Works fine. </p>
  
  <pre><code>ADD . /app
  WORKDIR /app
  RUN pip install -r requirements.txt
  CMD python app.py
  </code></pre>
  
  <p>If I run gunicorn as follows it just keeps spawning workers:</p>
  
  <pre><code>CMD gunicorn -b 0.0.0.0:8080 --workers=2 --threads=4 app:app
  or
  CMD ["gunicorn", "app:app"]
  </code></pre>
  
  <p>gunicorn has a --timeout=30 parameter. Defaults to 30 seconds which I increased to 300. This did not appear to have an affect.</p>
  
  <p>Note: I rewrote the app for the Starlette library and received the same results!</p>
  
  <p>Any guidance is appreciated.</p>
  
  <p>Thanks,
  Jay</p>