Skip to content
Docker image with Meinheld and Gunicorn for Flask applications in Python. Optionally with Alpine Linux.
Branch: master
Clone or download
tiangolo Merge pull request #4 from tiangolo/add-prestart-sh
Add support for /app/
Latest commit db8af21 Feb 8, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
python3.6-alpine3.8 Add images for Python 3.6, 3.7, standard or Alpine Jan 12, 2019
python3.6 Add images for Python 3.6, 3.7, standard or Alpine Jan 12, 2019
python3.7-alpine3.8 Add images for Python 3.6, 3.7, standard or Alpine Jan 12, 2019
python3.7 Add images for Python 3.6, 3.7, standard or Alpine Jan 12, 2019
scripts 🔧 Add scripts, build, lint, test Jan 12, 2019
tests Add support for /app/ Feb 8, 2019
.gitignore 🙈 Add .gitignore and license Jan 12, 2019
.travis.yml 👷 Set up Travis Jan 12, 2019
LICENSE 🙈 Add .gitignore and license Jan 12, 2019
Pipfile Add Pipfile with development dependencies Jan 12, 2019 Add support for /app/ Feb 8, 2019

Build Status

Supported tags and respective Dockerfile links


Docker image with Meinheld managed by Gunicorn for high-performance web applications in Flask using Python 3.7 and 3.6 with performance auto-tuning. Optionally with Alpine Linux.

GitHub repo:

Docker Hub image:


Python Flask web applications running with Meinheld controlled by Gunicorn have some of the best performances achievable by Flask (*).

If you have an already existing application in Flask or are building a new one, this image will give you the best performance possible (or close to that).

This image has an "auto-tuning" mechanism included, so that you can just add your code and get good performance automatically. And without making sacrifices (like logging).

* Note on performance and features

If you are starting a new project, you might benefit from a newer and faster framework like FastAPI (based on ASGI instead of WSGI like Flask and Django), and a Docker image like tiangolo/uvicorn-gunicorn-fastapi.

It would give you about 200% the performance achievable with Flask, even when using this image.

Also, if you want to use new technologies like WebSockets it would be easier with a newer framework based on ASGI, like FastAPI. As the standard ASGI was designed to be able to handle asynchronous code like the one needed for WebSockets.

Python 2.7

Do you need support for Python 2.7?

Let me know in an issue and I'll add it.

But only after knowing that someone actually needs it.

Technical Details


Meinheld is a high-performance WSGI-compliant web server.


You can use Gunicorn to manage Meinheld and run multiple processes of it.


Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions.


This image was created to be an alternative to tiangolo/uwsgi-nginx-flask, providing about 400% the performance of that image.

It is based on the more generic image tiangolo/meinheld-guinicorn. That's the one you would use for other WSGI frameworks, like Django.

How to use

  • You don't need to clone the GitHub repo. You can use this image as a base image for other images, using this in your Dockerfile:
FROM tiangolo/meinheld-gunicorn-flask:python3.7

COPY ./app /app

It will expect a file at /app/app/

Or otherwise a file at /app/

And will expect it to contain a variable app with your "WSGI" application.

Then you can build your image from the directory that has your Dockerfile, e.g:

docker build -t myimage ./

Advanced usage

Environment variables

These are the environment variables that you can set in the container to configure it and their default values:


The Python "module" (file) to be imported by Gunicorn, this module would contain the actual Flask application in a variable.

By default:

  • app.main if there's a file /app/app/ or
  • main if there's a file /app/

For example, if your main file was at /app/custom_app/, you could set it like:

docker run -d -p 80:80 -e MODULE_NAME="custom_app.custom_main" myimage


The variable inside of the Python module that contains the Flask application.

By default:

  • app

For example, if your main Python file has something like:

from flask import Flask
api = Flask(__name__)

def hello():
    return "Hello World from Flask"

In this case api would be the variable with the "Flask application". You could set it like:

docker run -d -p 80:80 -e VARIABLE_NAME="api" myimage


The string with the Python module and the variable name passed to Gunicorn.

By default, set based on the variables MODULE_NAME and VARIABLE_NAME:

  • app.main:app or
  • main:app

You can set it like:

docker run -d -p 80:80 -e APP_MODULE="custom_app.custom_main:api" myimage


The path to a Gunicorn Python configuration file.

By default:

  • /app/ if it exists
  • /app/app/ if it exists
  • / (the included default)

You can set it like:

docker run -d -p 80:80 -e GUNICORN_CONF="/app/" myimage


This image will check how many CPU cores are available in the current server running your container.

It will set the number of workers to the number of CPU cores multiplied by this value.

By default:

  • 2

You can set it like:

docker run -d -p 80:80 -e WORKERS_PER_CORE="3" myimage

If you used the value 3 in a server with 2 CPU cores, it would run 6 worker processes.

You can use floating point values too.

So, for example, if you have a big server (let's say, with 8 CPU cores) running several applications, and you have an ASGI application that you know won't need high performance. And you don't want to waste server resources. You could make it use 0.5 workers per CPU core. For example:

docker run -d -p 80:80 -e WORKERS_PER_CORE="0.5" myimage

In a server with 8 CPU cores, this would make it start only 4 worker processes.


Override the automatic definition of number of workers.

By default:

  • Set to the number of CPU cores in the current server multiplied by the environment variable WORKERS_PER_CORE. So, in a server with 2 cores, by default it will be set to 4.

You can set it like:

docker run -d -p 80:80 -e WEB_CONCURRENCY="2" myimage

This would make the image start 2 worker processes, independent of how many CPU cores are available in the server.


The "host" used by Gunicorn, the IP where Gunicorn will listen for requests.

It is the host inside of the container.

So, for example, if you set this variable to, it will only be available inside the container, not in the host running it.

It's is provided for completeness, but you probably shouldn't change it.

By default:



The port the container should listen on.

If you are running your container in a restrictive environment that forces you to use some specific port (like 8080) you can set it with this variable.

By default:

  • 80

You can set it like:

docker run -d -p 80:8080 -e PORT="8080" myimage


The actual host and port passed to Gunicorn.

By default, set based on the variables HOST and PORT.

So, if you didn't change anything, it will be set by default to:


You can set it like:

docker run -d -p 80:8080 -e BIND="" myimage


The log level for Gunicorn.

One of:

  • debug
  • info
  • warning
  • error
  • critical

By default, set to info.

If you need to squeeze more performance sacrificing logging, set it to warning, for example:

You can set it like:

docker run -d -p 80:8080 -e LOG_LEVEL="warning" myimage

Custom Gunicorn configuration file

The image includes a default Gunicorn Python config file at /

It uses the environment variables declared above to set all the configurations.

You can override it by including a file in:

  • /app/
  • /app/app/
  • /

Custom /app/

If you need to run anything before starting the app, you can add a file to the directory /app. The image will automatically detect and run it before starting everything.

For example, if you want to add Alembic SQL migrations (with SQLALchemy), you could create a ./app/ file in your code directory (that will be copied by your Dockerfile) with:

#! /usr/bin/env bash

# Let the DB start
sleep 10;
# Run migrations
alembic upgrade head

and it would wait 10 seconds to give the database some time to start and then run that alembic command.

If you need to run a Python script before starting the app, you could make the /app/ file run your Python script, with something like:

#! /usr/bin/env bash

# Run custom Python script before starting
python /app/


All the image tags, configurations, environment variables and application options are tested.

Release Notes


  • Add support for /app/


This project is licensed under the terms of the MIT license.

You can’t perform that action at this time.