Answers: <p>Yes, when using Docker Swarm you'll still want to use Gunicorn in your Docker containers.</p>
  
  <p>Gunicorn is necessary to facilitate the communication between the server and your web application. In the case of a Dockerized Flask application this is still the case as Gunicorn handles the communication between the Flask application and the Docker container. Adding Docker Swarm to this stack basically just means that you have more instances of your container, and that Swarm will handle balancing the incoming requests to each instance.</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-11-27T14:47:52.870Z
Answers: 2017-11-27T14:47:52.870Z
Answers: Gunther
Body: <p>so I have this nice rest API implemented in Flask running in docker and I am thinking about scaling. This is how I progressed:</p>
  
  <pre><code>CMD ["flask", "run", "--host=0.0.0.0"]
  </code></pre>
  
  <p>and</p>
  
  <pre><code>docker run -d -p 5000:5000 pyrest-alpine
  </code></pre>
  
  <p>works fine</p>
  
  <pre><code>CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "pyrest:app"]
  </code></pre>
  
  <p>and</p>
  
  <pre><code>docker run -d -p 5000:5000 pyrest-alpine
  </code></pre>
  
  <p>This also works fine, Gunicorn provided some scaling via prefork worker model in a single container now I want to scale via replication of Docker swarm with healthchecks where both </p>
  
  <pre><code>CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "pyrest:app"]
  </code></pre>
  
  <p>and</p>
  
  <pre><code>CMD ["flask", "run", "--host=0.0.0.0"]
  </code></pre>
  
  <p>work just fine with </p>
  
  <pre><code>docker service create --name pyrest-swarm --replicas 2 -p 5000:5000 --health-interval=2s --health-timeout=10s --health-retries=3--health-cmd "curl 0.0.0.0:5000/status || exit 1" pyrest-alpine
  </code></pre>
  
  <p>My question is Do I still use gunicorn when using docker swarm replication?</p>
  
Tags: <docker><docker-swarm>
Title: Flask, Gunicorn and Docker Swarm
UserDisplayName: Ondra N.
ThreadText: # Flask, Gunicorn and Docker Swarm
  
  (Asked by: username_0 on 2017-11-22)
  
  so I have this nice rest API implemented in Flask running in docker and I am thinking about scaling. This is how I progressed:
  
  ```
  CMD ["flask", "run", "--host=0.0.0.0"]
  
  ```
  
  and
  
  ```
  docker run -d -p 5000:5000 pyrest-alpine
  
  ```
  
  works fine
  
  ```
  CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "pyrest:app"]
  
  ```
  
  and
  
  ```
  docker run -d -p 5000:5000 pyrest-alpine
  
  ```
  
  This also works fine, Gunicorn provided some scaling via prefork worker model in a single container now I want to scale via replication of Docker swarm with healthchecks where both
  
  ```
  CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "pyrest:app"]
  
  ```
  
  and
  
  ```
  CMD ["flask", "run", "--host=0.0.0.0"]
  
  ```
  
  work just fine with
  
  ```
  docker service create --name pyrest-swarm --replicas 2 -p 5000:5000 --health-interval=2s --health-timeout=10s --health-retries=3--health-cmd "curl 0.0.0.0:5000/status || exit 1" pyrest-alpine
  
  ```
  
  My question is Do I still use gunicorn when using docker swarm replication?
  
  **Tags:** docker, docker-swarm
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-11-27. Score: 2 | Accepted answer
  
  Yes, when using Docker Swarm you'll still want to use Gunicorn in your Docker containers.
  
  Gunicorn is necessary to facilitate the communication between the server and your web application. In the case of a Dockerized Flask application this is still the case as Gunicorn handles the communication between the Flask application and the Docker container. Adding Docker Swarm to this stack basically just means that you have more instances of your container, and that Swarm will handle balancing the incoming requests to each instance.
  
  
  ---
  
  