Skip to content

Commit

Permalink
🎉 Add Python 3.6 and 3.7, Alpine, tests, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tiangolo committed Jan 4, 2019
0 parents commit 835d80f
Show file tree
Hide file tree
Showing 29 changed files with 919 additions and 0 deletions.
109 changes: 109 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
.mypy_cache
.vscode

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties

9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sudo: required

language: python

services:
- docker

script:
- bash scripts/test.sh
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 Sebastián Ramírez

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
[![Build Status](https://travis-ci.org/tiangolo/uvicorn-gunicorn-docker.svg?branch=master)](https://travis-ci.org/tiangolo/uvicorn-gunicorn-docker)

## Supported tags and respective `Dockerfile` links

* [`python3.7`, `latest` _(Dockerfile)_](https://github.com/tiangolo/uvicorn-gunicorn-docker/blob/master/python3.7/Dockerfile)
* [`python3.6` _(Dockerfile)_](https://github.com/tiangolo/uvicorn-gunicorn-docker/blob/master/python3.6/Dockerfile)
* [`python3.6-alpine3.8` _(Dockerfile)_](https://github.com/tiangolo/uvicorn-gunicorn-docker/blob/master/python3.6-alpine3.8/Dockerfile)
* [`python3.7-alpine3.8` _(Dockerfile)_](https://github.com/tiangolo/uvicorn-gunicorn-docker/blob/master/python3.7-alpine3.8/Dockerfile)

# uvicorn-gunicorn

**Docker** image with **Uvicorn** managed by **Gunicorn** for high-performance web applications in **Python 3.7** and **Python 3.6** in a single container with auto-tuning. Optionally with Alpine Linux.


## Description

This [**Docker**](https://www.docker.com/) image allows you to create [**Python**](https://www.python.org/) web applications that run with [**Uvicorn**](https://www.uvicorn.org/) and [**Gunicorn**](https://gunicorn.org/) in a single container.

Python web applications running with **Uvicorn** (using the "ASGI" specification) have shown to have [some of the best performances in third-party benchmarks](https://www.techempower.com/benchmarks/#section=test&runid=a979de55-980d-4721-a46f-77298b3f3923&hw=ph&test=fortune&l=zijzen-7). On par with **Go** and **Node.js** (in many cases, showing superior performance).

**Uvicorn** is a lightning-fast "ASGI" server. It runs asynchronous Python web code in a single process. You can use **Gunicorn** to manage it and run multiple of these concurrent processes. That way, you get the best of concurrency and parallelism.

This image has an "auto-tuning" system included, so that you can just add your code and get a very **high performance** automatically.

The image will set a sensible configuration based on the server it is running on (and the amount of CPU cores available) without making sacrifices.

It has sensible defaults, but you can configure it with environment variables or override the configuration files.

There is also an Alpine version. If you want it, use one of the Alpine tags from above.

This image was created to be the base image for [**tiangolo/uvicorn-gunicorn-starlette**](https://github.com/tiangolo/uvicorn-gunicorn-starlette-docker) and [**tiangolo/uvicorn-gunicorn-fastapi**](https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker) but could be used as the base image to run any Python web application that uses the ASGI specification.

If you are creating a new [**Starlette**](https://www.starlette.io/) web application you should use [**tiangolo/uvicorn-gunicorn-starlette**](https://github.com/tiangolo/uvicorn-gunicorn-starlette-docker) instead.

If you are creating a new [**FastAPI**](https://fastapi.tiangolo.com/) web application you should use [**tiangolo/uvicorn-gunicorn-fastapi**](https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker) instead.

**Note**: Unless you are doing something more technically advanced, you probably should be using [**Starlette**](https://www.starlette.io/) with [**tiangolo/uvicorn-gunicorn-starlette**](https://github.com/tiangolo/uvicorn-gunicorn-starlette-docker) or [**FastAPI**](https://fastapi.tiangolo.com/) with [**tiangolo/uvicorn-gunicorn-fastapi**](https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker).


**GitHub repo**: <https://github.com/tiangolo/uvicorn-gunicorn-docker>

**Docker Hub image**: <https://hub.docker.com/r/tiangolo/uvicorn-gunicorn/>

## How to use

* You shouldn't have to clone the GitHub repo. You should use it as a base image for other images, using this in your `Dockerfile`:

```Dockerfile
FROM tiangolo/uvicorn-gunicorn:python3.7

COPY ./app /app
```

* It will expect a file at `/app/app/main.py`, or a file at `/app/main.py`. And will expect that file to contain a variable `app` with your "ASGI" application.

## 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 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`
* `VARIABLE_NAME`: the variable inside of the Python module that contains the ASGI application. By default:
* `app`
* `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`
* `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)
* `WORKERS_PER_CORE`: this image will check how many CPU cores are available in the current server running your application. It will set the number of workers to the CPU cores times this value. By default:
* `2`
* `WEB_CONCURRENCY`: override the automatic definition of number of workers. By default:
* set to the number of CPU cores in the current server times the variable `WORKERS_PER_CORE`, so, in a server with 2 cores, by default it will be set to `4`.
* `HOST`: the host used by Gunicorn. 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 ports (like `8080`) you can set it with this variable. By default:
* `80`
* `BIND`: The 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 to:
* `0.0.0.0:80`
* `LOG_LEVEL`: the log level for Gunicorn. One of:
* `debug`
* `info`
* `warning`
* `error`
* `critical`
* Set to `info` by default. If you need to squeeze more performance sacrificing logging, set it to `warning`.

## License

This project is licensed under the terms of the MIT license.
14 changes: 14 additions & 0 deletions docker-compose.build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '3'
services:
python3.6:
build: ./python3.6
image: tiangolo/uvicorn-gunicorn:python3.6
python3.6-alpine3.8:
build: ./python3.6-alpine3.8
image: tiangolo/uvicorn-gunicorn:python3.6-alpine3.8
python3.7:
build: ./python3.7
image: tiangolo/uvicorn-gunicorn:python3.7
python3.7-alpine3.8:
build: ./python3.7-alpine3.8
image: tiangolo/uvicorn-gunicorn:python3.7-alpine3.8
21 changes: 21 additions & 0 deletions python3.6-alpine3.8/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM python:3.6-alpine3.8

LABEL maintainer="Sebastian Ramirez <[email protected]>"

RUN apk add --no-cache --virtual .build-deps gcc libc-dev make \
&& pip install uvicorn gunicorn \
&& apk del .build-deps gcc libc-dev make

COPY ./entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

COPY ./gunicorn_conf.py /gunicorn_conf.py

COPY ./app /app
WORKDIR /app/

ENV PYTHONPATH=/app

EXPOSE 80

ENTRYPOINT ["/entrypoint.sh"]
23 changes: 23 additions & 0 deletions python3.6-alpine3.8/app/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sys


class App:
def __init__(self, scope):
self.scope = scope

async def __call__(self, receive, send):
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [[b"content-type", b"text/plain"]],
}
)
version = f"{sys.version_info.major}.{sys.version_info.minor}"
message = f"Hello world! From Uvicorn with Gunicorn in Alpine. Using Python {version}".encode(
"utf-8"
)
await send({"type": "http.response.body", "body": message})


app = App
26 changes: 26 additions & 0 deletions python3.6-alpine3.8/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env sh
set -e

if [ -f /app/app/main.py ]; then
DEFAULT_MODULE_NAME=app.main
elif [ -f /app/main.py ]; then
DEFAULT_MODULE_NAME=main
fi
MODULE_NAME=${MODULE_NAME:-$DEFAULT_MODULE_NAME}
VARIABLE_NAME=${VARIABLE_NAME:-app}
export APP_MODULE=${APP_MODULE:-"$MODULE_NAME:$VARIABLE_NAME"}

if [ -f /app/gunicorn_conf.py ]; then
DEFAULT_GUNICORN_CONF=/app/gunicorn_conf.py
elif [ -f /app/app/gunicorn_conf.py ]; then
DEFAULT_GUNICORN_CONF=/app/app/gunicorn_conf.py
else
DEFAULT_GUNICORN_CONF=/gunicorn_conf.py
fi
export GUNICORN_CONF=${GUNICORN_CONF:-$DEFAULT_GUNICORN_CONF}

if [ -z "$@" ]; then
gunicorn -k uvicorn.workers.UvicornWorker -c "$GUNICORN_CONF" "$APP_MODULE"
else
exec "$@"
fi
42 changes: 42 additions & 0 deletions python3.6-alpine3.8/gunicorn_conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import json
import multiprocessing
import os

workers_per_core_str = os.getenv("WORKERS_PER_CORE", "2")
web_concurrency_str = os.getenv("WEB_CONCURRENCY", None)
host = os.getenv("HOST", "0.0.0.0")
port = os.getenv("PORT", "80")
bind_env = os.getenv("BIND", None)
use_loglevel = os.getenv("LOG_LEVEL", "info")
if bind_env:
use_bind = bind_env
else:
use_bind = f"{host}:{port}"

cores = multiprocessing.cpu_count()
workers_per_core = float(workers_per_core_str)
default_web_concurrency = workers_per_core * cores
if web_concurrency_str:
web_concurrency = int(web_concurrency_str)
assert web_concurrency > 0
else:
web_concurrency = int(default_web_concurrency)

# Gunicorn config variables
loglevel = use_loglevel
workers = web_concurrency
bind = use_bind
keepalive = 120
errorlog = "-"

# For debugging and testing
log_data = {
"loglevel": loglevel,
"workers": workers,
"bind": bind,
# Additional, non-gunicorn variables
"workers_per_core": workers_per_core,
"host": host,
"port": port,
}
print(json.dumps(log_data))
19 changes: 19 additions & 0 deletions python3.6/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM python:3.6

LABEL maintainer="Sebastian Ramirez <[email protected]>"

RUN pip install uvicorn gunicorn

COPY ./entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

COPY ./gunicorn_conf.py /gunicorn_conf.py

COPY ./app /app
WORKDIR /app/

ENV PYTHONPATH=/app

EXPOSE 80

ENTRYPOINT ["/entrypoint.sh"]
Loading

0 comments on commit 835d80f

Please sign in to comment.