当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

meinheld-gunicorn-flask-docker

授权协议 MIT License
开发语言 Python
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 平学
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Test Deploy

Supported tags and respective Dockerfile links

Discouraged tags

To learn more about why Alpine images are discouraged for Python read the note at the end: �� Alpine Python Warning.

Deprecated

These tags are no longer supported:


Note: There are tags for each build date. If you need to "pin" the Docker image version you use, you can select one of those tags. E.g. tiangolo/meinheld-gunicorn-flask:python3.7-2019-10-15.

meinheld-gunicorn-flask

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

GitHub repo: https://github.com/tiangolo/meinheld-gunicorn-flask-docker

Docker Hub image: https://hub.docker.com/r/tiangolo/meinheld-gunicorn-flask/

Description

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.

Technical Details

Meinheld

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

Gunicorn

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

Flask

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

Alternatives

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-gunicorn. That's the one you would use for other WSGI frameworks, like Django.

�� WARNING: You Probably Don't Need this Docker Image

You are probably using Kubernetes or similar tools. In that case, you probably don't need this image (or any other similar base image). You are probably better off building a Docker image from scratch.


If you have a cluster of machines with Kubernetes, Docker Swarm Mode, Nomad, or other similar complex system to manage distributed containers on multiple machines, then you will probably want to handle replication at the cluster level instead of using a process manager in each container that starts multiple worker processes, which is what this Docker image does.

In those cases (e.g. using Kubernetes) you would probably want to build a Docker image from scratch, installing your dependencies, and running a single process instead of this image.

For example, using Gunicorn you could have a file app/gunicorn_conf.py with:

# Gunicorn config variables
loglevel = "info"
errorlog = "-"  # stderr
accesslog = "-"  # stdout
worker_tmp_dir = "/dev/shm"
graceful_timeout = 120
timeout = 120
keepalive = 5
threads = 3

And then you could have a Dockerfile with:

FROM python:3.9

WORKDIR /code

COPY ./requirements.txt /code/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

COPY ./app /code/app

CMD ["gunicorn", "--conf", "app/gunicorn_conf.py", "--bind", "0.0.0.0:80", "app.main:app"]

You can read more about these ideas in the FastAPI documentation about: FastAPI in Containers - Docker as the same ideas would apply to other web applications in containers.

When to Use this Docker Image

A Simple App

You could want a process manager running multiple worker processes in the container if your application is simple enough that you don't need (at least not yet) to fine-tune the number of processes too much, and you can just use an automated default, and you are running it on a single server, not a cluster.

Docker Compose

You could be deploying to a single server (not a cluster) with Docker Compose, so you wouldn't have an easy way to manage replication of containers (with Docker Compose) while preserving the shared network and load balancing.

Then you could want to have a single container with a process manager starting several worker processes inside, as this Docker image does.

Prometheus and Other Reasons

You could also have other reasons that would make it easier to have a single container with multiple processes instead of having multiple containers with a single process in each of them.

For example (depending on your setup) you could have some tool like a Prometheus exporter in the same container that should have access to each of the requests that come.

In this case, if you had multiple containers, by default, when Prometheus came to read the metrics, it would get the ones for a single container each time (for the container that handled that particular request), instead of getting the accumulated metrics for all the replicated containers.

Then, in that case, it could be simpler to have one container with multiple processes, and a local tool (e.g. a Prometheus exporter) on the same container collecting Prometheus metrics for all the internal processes and exposing those metrics on that single container.


Read more about it all in the FastAPI documentation about: FastAPI in Containers - Docker, as the same concepts apply to other web applications in containers.

How to use

You don't have to clone this repo.

You can use this image as a base image for other images.

Assuming you have a file requirements.txt, you could have a Dockerfile like this:

FROM tiangolo/meinheld-gunicorn-flask:python3.9

COPY ./requirements.txt /app/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt

COPY ./app /app

It will expect a file at /app/app/main.py.

Or otherwise a file at /app/main.py.

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:

MODULE_NAME

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/main.py or
  • main if there's a file /app/main.py

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

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

VARIABLE_NAME

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__)

@api.route("/")
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

APP_MODULE

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

GUNICORN_CONF

The path to a Gunicorn Python configuration file.

By default:

  • /app/gunicorn_conf.py if it exists
  • /app/app/gunicorn_conf.py if it exists
  • /gunicorn_conf.py (the included default)

You can set it like:

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

WORKERS_PER_CORE

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.

WEB_CONCURRENCY

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.

HOST

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 127.0.0.1, 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:

  • 0.0.0.0

PORT

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

BIND

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:

  • 0.0.0.0:80

You can set it like:

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

LOG_LEVEL

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

Logs are sent to the container's stderr and stdout, meaning you can view the logs with the docker logs -f your_container_name_here command.

Custom Gunicorn configuration file

The image includes a default Gunicorn Python config file at /gunicorn_conf.py.

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

You can override it by including a file in:

  • /app/gunicorn_conf.py
  • /app/app/gunicorn_conf.py
  • /gunicorn_conf.py

Custom /app/prestart.sh

If you need to run anything before starting the app, you can add a file prestart.sh 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/prestart.sh 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/prestart.sh file run your Python script, with something like:

#! /usr/bin/env bash

# Run custom Python script before starting
python /app/my_custom_prestart_script.py

�� Alpine Python Warning

In short: You probably shouldn't use Alpine for Python projects, instead use the slim Docker image versions.


Do you want more details? Continue reading ��

Alpine is more useful for other languages where you build a static binary in one Docker image stage (using multi-stage Docker building) and then copy it to a simple Alpine image, and then just execute that binary. For example, using Go.

But for Python, as Alpine doesn't use the standard tooling used for building Python extensions, when installing packages, in many cases Python (pip) won't find a precompiled installable package (a "wheel") for Alpine. And after debugging lots of strange errors you will realize that you have to install a lot of extra tooling and build a lot of dependencies just to use some of these common Python packages. ��

This means that, although the original Alpine image might have been small, you end up with a an image with a size comparable to the size you would have gotten if you had just used a standard Python image (based on Debian), or in some cases even larger. ��

And in all those cases, it will take much longer to build, consuming much more resources, building dependencies for longer, and also increasing its carbon footprint, as you are using more CPU time and energy for each build. ��

If you want slim Python images, you should instead try and use the slim versions that are still based on Debian, but are smaller. ��

Tests

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

Release Notes

Latest Changes

  • �� Add note to discourage Alpine with Python. PR #64 by @tiangolo.
  • �� Remove support for Python 2.7. PR #63 by @tiangolo.
  • �� Add Kubernetes warning, when to use this image. PR #62 by @tiangolo.
  • ✏️ Fix typo duplicate "Note" in Readme. PR #61 by @tiangolo.
  • Refactor dependencies to improve Dependabot updates and reduce disk size used. PR #60 by @tiangolo.
  • �� Update Latest Changes GitHub Action. PR #59 by @tiangolo.
  • �� Add Dependabot and external dependencies to get automated upgrade PRs. PR #51 by @tiangolo.
  • Add support for Python 3.9 and Python 3.9 Alpine. PR #50 by @tiangolo.
  • �� Format GitHub Action latest-changes. PR #42 by @tiangolo.
  • �� Add GitHub Action latest-changes, update issue-manager, add funding. PR #41 by @tiangolo.
  • Add Python 3.8 with Alpine 3.11. PR #28.
  • Fix typo in README. PR #18 by @tahmid-choyon.
  • Add support for Python 3.8. PR #27.
  • Refactor build setup:
    • Use GitHub actions for CI.
    • Simplify, centralize, and deduplicate code and configs.
    • Update tests.
    • Move from Pipenv to Poetry.
    • PR #26.

0.3.0

  • Refactor tests to use env vars and add image tags for each build date, like tiangolo/meinheld-gunicorn-flask:python3.7-2019-10-15. PR #17.

0.2.0

  • Add support for Python 2.7 (you should use Python 3.7 or Python 3.6). PR #11.

  • Update Travis CI configuration. PR #10 by @cclauss.

0.1.0

  • Add support for /app/prestart.sh.

License

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

  • 使用docker-compose部署flask项目,gunicorn 启动 flask,想要改完代码后,只需刷新页面(或打开页面),就能看到浏览器中反映的更改。 要运行容器以支持开发工作流,我们将执行以下操作: 将源代码装入容器中 安装所有依赖项,包括“dev”依赖项 启动nodemon以监视文件系统更改 docker部署的相关文件 Dockerfile FROM python:3.7 WORKD

  • docker和docker-compose部署flask(flask-script)、gunicorn实践记录 这里不介绍docker和docker-compose的工作原理,只真实的记录本次实践。 循序渐进的来,先用gunicorn启动,再用Dockerfile启动,最后尝试docker-compose编排 1.准备项目目录和模拟代码,先本地用gunicorn启动 ~/flask-demo 目录

  • 准备工作 本文使用环境: Linux 发行版:Ubuntu Focal 20.04.4 (LTS) Linux 内核:Linux 5.4.0-100-generic Docker 版本:20.10.12 Python:3.7.9 Flask:2.0.2 Gunicorn:20.1.0 Nginx:1.21.4 搭建 Linux 环境可参考: Ubuntu安装 Docker 和 Docker Com

  • 第一次生成 Dockerfile,尝试用 dfimage 的方案提取 Dockerfile 的方案失败后,自己简单写出了这个 Dockerfile 模板,语法不再赘述,如果是简单的 Flask 应用,生成 requirements.txt,然后替换 COPY 部分后可以直接使用。 然后是 Gunicorn 和 Flask 的日志融合问题,尤其是访问日志的融合,别走弯路了。 文件代码: # 基于

  • 现成的镜像,已经配置好nginx-gunicorn-flask,可直接部署flask 项目 直接部署flask项目 安装镜像 如果默认源比较慢,可以换成163镜像源 http://hub-mirror.c.163.com docker pull danriti/nginx-gunicorn-flask 运行镜像,将flask项目映射到容器 映射项目到容器 /usr/share/nginx/www/

  • python容器部署 1. 下载镜像 下载镜像地址 我们此处下载docker pull python 2. 容器部署docker run -p 8000:8000 --name python -itd -v /data/www/htdocs/:/www python # 说明 --name python : 把容器命名为python -itd: i 以交互模式运行容器 t  为容器重新分配一个伪输

  • 使用docker部署nginx+flask+gunicorn+mysql项目 目录结构 [root@k8s-master ~]# tree lab_project/ -L 2 lab_project/ ├── docker-compose.yml ├── flask │ ├── Dockerfile │ └── lab_app ├── mysql │ ├── Dockerfile │ └── la

  • docker-compose简单说明 利用docker-compose管理docker容器 操作docker-compose集合了我们操作Dockerfile和Dockerfile里的一下命令, 总结就是,Dockerfile帮我们管理docker镜像,docker-compose帮我们管理docker众多容器。 总之如果你还没用过,试着用起来就知道了,一个很简单的工具。 requirements

  • Docker+Gunicorn+Nginx部署Flask后端 tips: 本文主要介绍如何在docker中部署Flask APP 代码仓库 背景 Flask自带的服务启动,非常方便在开发环境中调试使用,但是用于生产环境却不是好的选择。 一般生产环境中部署Flask都是基于WGSI容器。 生产环境可以用python的虚拟环境来部署Flask,但是部署方式比较麻烦,且不易移植。 Gunicorn Gu

 相关资料
  • Meinheld 是一个高性能的异步 WSGI Web 服务器。是一个兼容 WSGI 的服务器,支持 PEP333 和 PEP3333 规范。Meinheld 利用 greenlet 和 Picoev 实现异步 I/O。 Meinheld 要求 Python 2.x >= 2.6 或者 Python 3.x >= 3.2 ,同时要求 greenlet >= 0.4.5。支持 Linux, Free

  • 问题内容: 我正在使用Flask的内置开发服务器来开发Flask应用程序。我使用Flask-Script启动它。我想切换为使用Gunicorn作为Web服务器。为此,我需要在Flask-Script和Gunicorn之间编写某种集成代码吗?还是Flask-Script与使用Gunicorn运行应用程序无关? 提前致谢! 到@ sean-lynch的道具。以下是根据他的回答而工作,经过测试的代码。我

  • 问题内容: 我正在尝试在Ubuntu 12.04系统中从Supervisor运行Gunicorn。Gunicorn运行Flask应用程序(通过Flask的嵌入式服务器测试的简单REST Web服务)。我已经通过克隆GIT存储库来安装Gunicorn,试图避免“ apt-get install”,因为它在安装时会运行Gunicorn服务器。我不希望它运行,它将仅由Supervisor运行。 因此,安

  • 问题内容: 我有一个Flask应用程序,正在尝试使用Gunicorn和nginx进行部署。但是,尽管它在本地运行良好,但是当我在远程服务器上使用Gunicorn运行时,它会引发TemplateNotFound错误。 我不确定如何开始调试它,更不用说它为什么会失败了……希望对前者有所帮助,即使不是后者。我以为这可能是权限问题,所以将模板文件夹更改为777 …没有运气。这是所有相关的细节: 安装脚本

  • 问题内容: 我是新来的,仅使用nginx提供静态文件。我现在已经安装了flask and gunicorn。如果我运行,然后从服务器中获取它,它将运行良好。但是,如果尝试从浏览器访问它,它将返回404错误(我正在托管位于root用户的wordpress网站的服务器上运行此错误)。 Flask应用程序: 以及我的nginx配置的相关部分: 我希望这是所有相关信息。如果没有,请告诉。谢谢! 问题答案:

  • 问题内容: 我看到人们正在运行Nginx + Gunicorn + Flask之类的设置。 谁能解释在flask前使用Gunicorn有什么好处?为什么不只运行Flask?运行Gunicorn + Flask会消耗更多资源吗?Gunicorn无法响应时,可以重启Flask实例吗? 将nginx放在gunicorn上还有什么目的? 问题答案: 我认为你可能会感到困惑,Flask不是Web服务器,它是