From 835d80faafcccf5b38857cc083de6bab873e52cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Fri, 4 Jan 2019 15:40:14 +0400 Subject: [PATCH] :tada: Add Python 3.6 and 3.7, Alpine, tests, docs --- .gitignore | 109 +++++++++++++++++++++++++++ .travis.yml | 9 +++ LICENSE | 21 ++++++ README.md | 94 +++++++++++++++++++++++ docker-compose.build.yml | 14 ++++ python3.6-alpine3.8/Dockerfile | 21 ++++++ python3.6-alpine3.8/app/main.py | 23 ++++++ python3.6-alpine3.8/entrypoint.sh | 26 +++++++ python3.6-alpine3.8/gunicorn_conf.py | 42 +++++++++++ python3.6/Dockerfile | 19 +++++ python3.6/app/main.py | 23 ++++++ python3.6/entrypoint.sh | 26 +++++++ python3.6/gunicorn_conf.py | 42 +++++++++++ python3.7-alpine3.8/Dockerfile | 21 ++++++ python3.7-alpine3.8/app/main.py | 23 ++++++ python3.7-alpine3.8/entrypoint.sh | 26 +++++++ python3.7-alpine3.8/gunicorn_conf.py | 42 +++++++++++ python3.7/Dockerfile | 19 +++++ python3.7/app/main.py | 23 ++++++ python3.7/entrypoint.sh | 26 +++++++ python3.7/gunicorn_conf.py | 42 +++++++++++ scripts/lint.sh | 7 ++ scripts/test.sh | 5 ++ tests/__init__.py | 0 tests/test_defaults.py | 51 +++++++++++++ tests/test_env_vars_1.py | 52 +++++++++++++ tests/test_env_vars_2.py | 36 +++++++++ tests/test_env_vars_3.py | 50 ++++++++++++ tests/utils.py | 27 +++++++ 29 files changed, 919 insertions(+) create mode 100755 .gitignore create mode 100644 .travis.yml create mode 100755 LICENSE create mode 100644 README.md create mode 100644 docker-compose.build.yml create mode 100644 python3.6-alpine3.8/Dockerfile create mode 100755 python3.6-alpine3.8/app/main.py create mode 100644 python3.6-alpine3.8/entrypoint.sh create mode 100644 python3.6-alpine3.8/gunicorn_conf.py create mode 100644 python3.6/Dockerfile create mode 100755 python3.6/app/main.py create mode 100644 python3.6/entrypoint.sh create mode 100644 python3.6/gunicorn_conf.py create mode 100644 python3.7-alpine3.8/Dockerfile create mode 100755 python3.7-alpine3.8/app/main.py create mode 100644 python3.7-alpine3.8/entrypoint.sh create mode 100644 python3.7-alpine3.8/gunicorn_conf.py create mode 100644 python3.7/Dockerfile create mode 100755 python3.7/app/main.py create mode 100644 python3.7/entrypoint.sh create mode 100644 python3.7/gunicorn_conf.py create mode 100644 scripts/lint.sh create mode 100644 scripts/test.sh create mode 100644 tests/__init__.py create mode 100644 tests/test_defaults.py create mode 100644 tests/test_env_vars_1.py create mode 100644 tests/test_env_vars_2.py create mode 100644 tests/test_env_vars_3.py create mode 100644 tests/utils.py diff --git a/.gitignore b/.gitignore new file mode 100755 index 00000000..2d83f6c7 --- /dev/null +++ b/.gitignore @@ -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 + diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..ba2437a2 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +sudo: required + +language: python + +services: + - docker + +script: +- bash scripts/test.sh diff --git a/LICENSE b/LICENSE new file mode 100755 index 00000000..feec529f --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 00000000..e30a8196 --- /dev/null +++ b/README.md @@ -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**: + +**Docker Hub image**: + +## 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. diff --git a/docker-compose.build.yml b/docker-compose.build.yml new file mode 100644 index 00000000..2b937bc9 --- /dev/null +++ b/docker-compose.build.yml @@ -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 diff --git a/python3.6-alpine3.8/Dockerfile b/python3.6-alpine3.8/Dockerfile new file mode 100644 index 00000000..1818fe03 --- /dev/null +++ b/python3.6-alpine3.8/Dockerfile @@ -0,0 +1,21 @@ +FROM python:3.6-alpine3.8 + +LABEL maintainer="Sebastian Ramirez " + +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"] diff --git a/python3.6-alpine3.8/app/main.py b/python3.6-alpine3.8/app/main.py new file mode 100755 index 00000000..fe822a70 --- /dev/null +++ b/python3.6-alpine3.8/app/main.py @@ -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 diff --git a/python3.6-alpine3.8/entrypoint.sh b/python3.6-alpine3.8/entrypoint.sh new file mode 100644 index 00000000..babab7b5 --- /dev/null +++ b/python3.6-alpine3.8/entrypoint.sh @@ -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 diff --git a/python3.6-alpine3.8/gunicorn_conf.py b/python3.6-alpine3.8/gunicorn_conf.py new file mode 100644 index 00000000..c63c8e84 --- /dev/null +++ b/python3.6-alpine3.8/gunicorn_conf.py @@ -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)) diff --git a/python3.6/Dockerfile b/python3.6/Dockerfile new file mode 100644 index 00000000..a5a6877c --- /dev/null +++ b/python3.6/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.6 + +LABEL maintainer="Sebastian Ramirez " + +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"] diff --git a/python3.6/app/main.py b/python3.6/app/main.py new file mode 100755 index 00000000..b06d59e7 --- /dev/null +++ b/python3.6/app/main.py @@ -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. Using Python {version}".encode( + "utf-8" + ) + await send({"type": "http.response.body", "body": message}) + + +app = App diff --git a/python3.6/entrypoint.sh b/python3.6/entrypoint.sh new file mode 100644 index 00000000..babab7b5 --- /dev/null +++ b/python3.6/entrypoint.sh @@ -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 diff --git a/python3.6/gunicorn_conf.py b/python3.6/gunicorn_conf.py new file mode 100644 index 00000000..c63c8e84 --- /dev/null +++ b/python3.6/gunicorn_conf.py @@ -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)) diff --git a/python3.7-alpine3.8/Dockerfile b/python3.7-alpine3.8/Dockerfile new file mode 100644 index 00000000..60cfba35 --- /dev/null +++ b/python3.7-alpine3.8/Dockerfile @@ -0,0 +1,21 @@ +FROM python:3.7-alpine3.8 + +LABEL maintainer="Sebastian Ramirez " + +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"] diff --git a/python3.7-alpine3.8/app/main.py b/python3.7-alpine3.8/app/main.py new file mode 100755 index 00000000..fe822a70 --- /dev/null +++ b/python3.7-alpine3.8/app/main.py @@ -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 diff --git a/python3.7-alpine3.8/entrypoint.sh b/python3.7-alpine3.8/entrypoint.sh new file mode 100644 index 00000000..babab7b5 --- /dev/null +++ b/python3.7-alpine3.8/entrypoint.sh @@ -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 diff --git a/python3.7-alpine3.8/gunicorn_conf.py b/python3.7-alpine3.8/gunicorn_conf.py new file mode 100644 index 00000000..c63c8e84 --- /dev/null +++ b/python3.7-alpine3.8/gunicorn_conf.py @@ -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)) diff --git a/python3.7/Dockerfile b/python3.7/Dockerfile new file mode 100644 index 00000000..613da015 --- /dev/null +++ b/python3.7/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.7 + +LABEL maintainer="Sebastian Ramirez " + +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"] diff --git a/python3.7/app/main.py b/python3.7/app/main.py new file mode 100755 index 00000000..b06d59e7 --- /dev/null +++ b/python3.7/app/main.py @@ -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. Using Python {version}".encode( + "utf-8" + ) + await send({"type": "http.response.body", "body": message}) + + +app = App diff --git a/python3.7/entrypoint.sh b/python3.7/entrypoint.sh new file mode 100644 index 00000000..babab7b5 --- /dev/null +++ b/python3.7/entrypoint.sh @@ -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 diff --git a/python3.7/gunicorn_conf.py b/python3.7/gunicorn_conf.py new file mode 100644 index 00000000..c63c8e84 --- /dev/null +++ b/python3.7/gunicorn_conf.py @@ -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)) diff --git a/scripts/lint.sh b/scripts/lint.sh new file mode 100644 index 00000000..a38f39aa --- /dev/null +++ b/scripts/lint.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +set -x + +autoflake --remove-all-unused-imports --recursive --remove-unused-variables --in-place ./ --exclude=__init__.py +isort --multi-line=3 --trailing-comma --force-grid-wrap=0 --combine-as --line-width 88 --recursive --apply ./ +black ./ diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100644 index 00000000..dfcc3a92 --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -e + +docker-compose -f docker-compose.build.yml build +pytest tests diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_defaults.py b/tests/test_defaults.py new file mode 100644 index 00000000..e39e285f --- /dev/null +++ b/tests/test_defaults.py @@ -0,0 +1,51 @@ +import time + +import docker +import pytest +import requests + +from .utils import get_config, stop_previous_container + +client = docker.from_env() + +@pytest.mark.parametrize( + "image,response_text", + [ + ( + "tiangolo/uvicorn-gunicorn:python3.6", + "Hello world! From Uvicorn with Gunicorn. Using Python 3.6", + ), + ( + "tiangolo/uvicorn-gunicorn:python3.7", + "Hello world! From Uvicorn with Gunicorn. Using Python 3.7", + ), + ( + "tiangolo/uvicorn-gunicorn:python3.6-alpine3.8", + "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.6", + ), + ( + "tiangolo/uvicorn-gunicorn:python3.7-alpine3.8", + "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.7", + ), + ], +) +def test_defaults(image, response_text): + stop_previous_container(client) + container = client.containers.run( + image, + name="uvicorn-gunicorn-test", + ports={"80": "8000"}, + detach=True, + ) + config_data = get_config(container) + assert config_data["workers_per_core"] == 2 + assert config_data["host"] == "0.0.0.0" + assert config_data["port"] == "80" + assert config_data["loglevel"] == "info" + assert config_data["workers"] > 2 + assert config_data["bind"] == "0.0.0.0:80" + time.sleep(1) + response = requests.get("http://127.0.0.1:8000") + assert response.text == response_text + container.stop() + container.remove() diff --git a/tests/test_env_vars_1.py b/tests/test_env_vars_1.py new file mode 100644 index 00000000..344f1fa5 --- /dev/null +++ b/tests/test_env_vars_1.py @@ -0,0 +1,52 @@ +import time + +import docker +import pytest +import requests + +from .utils import get_config, stop_previous_container + +client = docker.from_env() + + +@pytest.mark.parametrize( + "image,response_text", + [ + ( + "tiangolo/uvicorn-gunicorn:python3.6", + "Hello world! From Uvicorn with Gunicorn. Using Python 3.6", + ), + ( + "tiangolo/uvicorn-gunicorn:python3.7", + "Hello world! From Uvicorn with Gunicorn. Using Python 3.7", + ), + ( + "tiangolo/uvicorn-gunicorn:python3.6-alpine3.8", + "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.6", + ), + ( + "tiangolo/uvicorn-gunicorn:python3.7-alpine3.8", + "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.7", + ), + ], +) +def test_env_vars_1(image, response_text): + stop_previous_container(client) + container = client.containers.run( + image, + name="uvicorn-gunicorn-test", + environment={"WORKERS_PER_CORE": 1, "PORT": "8000", "LOG_LEVEL": "warning"}, + ports={"8000": "8000"}, + detach=True, + ) + config_data = get_config(container) + assert config_data["workers_per_core"] == 1 + assert config_data["host"] == "0.0.0.0" + assert config_data["port"] == "8000" + assert config_data["loglevel"] == "warning" + assert config_data["bind"] == "0.0.0.0:8000" + time.sleep(1) + response = requests.get("http://127.0.0.1:8000") + assert response.text == response_text + container.stop() + container.remove() diff --git a/tests/test_env_vars_2.py b/tests/test_env_vars_2.py new file mode 100644 index 00000000..a6c5194c --- /dev/null +++ b/tests/test_env_vars_2.py @@ -0,0 +1,36 @@ +import docker +import pytest + +from .utils import get_config, get_process_names, stop_previous_container + +client = docker.from_env() + + +@pytest.mark.parametrize( + "image", + [ + ("tiangolo/uvicorn-gunicorn:python3.6"), + ("tiangolo/uvicorn-gunicorn:python3.7"), + ("tiangolo/uvicorn-gunicorn:python3.6-alpine3.8"), + ("tiangolo/uvicorn-gunicorn:python3.7-alpine3.8"), + ], +) +def test_env_vars_2(image): + stop_previous_container(client) + container = client.containers.run( + image, + name="uvicorn-gunicorn-test", + environment={"WEB_CONCURRENCY": 1, "HOST": "127.0.0.1"}, + ports={"80": "8000"}, + detach=True, + ) + process_names = get_process_names(container) + config_data = get_config(container) + assert config_data["workers"] == 1 + assert len(process_names) == 1 + assert config_data["host"] == "127.0.0.1" + assert config_data["port"] == "80" + assert config_data["loglevel"] == "info" + assert config_data["bind"] == "127.0.0.1:80" + container.stop() + container.remove() diff --git a/tests/test_env_vars_3.py b/tests/test_env_vars_3.py new file mode 100644 index 00000000..df980360 --- /dev/null +++ b/tests/test_env_vars_3.py @@ -0,0 +1,50 @@ +import time + +import docker +import pytest +import requests + +from .utils import get_config, stop_previous_container + +client = docker.from_env() + + +@pytest.mark.parametrize( + "image,response_text", + [ + ( + "tiangolo/uvicorn-gunicorn:python3.6", + "Hello world! From Uvicorn with Gunicorn. Using Python 3.6", + ), + ( + "tiangolo/uvicorn-gunicorn:python3.7", + "Hello world! From Uvicorn with Gunicorn. Using Python 3.7", + ), + ( + "tiangolo/uvicorn-gunicorn:python3.6-alpine3.8", + "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.6", + ), + ( + "tiangolo/uvicorn-gunicorn:python3.7-alpine3.8", + "Hello world! From Uvicorn with Gunicorn in Alpine. Using Python 3.7", + ), + ], +) +def test_env_bind(image, response_text): + stop_previous_container(client) + container = client.containers.run( + image, + name="uvicorn-gunicorn-test", + environment={"BIND": "0.0.0.0:8080", "HOST": "127.0.0.1", "PORT": "9000"}, + ports={"8080": "8000"}, + detach=True, + ) + config_data = get_config(container) + assert config_data["host"] == "127.0.0.1" + assert config_data["port"] == "9000" + assert config_data["bind"] == "0.0.0.0:8080" + time.sleep(1) + response = requests.get("http://127.0.0.1:8000") + assert response.text == response_text + container.stop() + container.remove() diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 00000000..06a51bdb --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,27 @@ +import json +from docker.errors import NotFound + + +def get_process_names(container): + top = container.top() + process_commands = [p[7] for p in top["Processes"]] + gunicorn_processes = [p for p in process_commands if "gunicorn" in p] + return gunicorn_processes + + +def get_config(container): + gunicorn_processes = get_process_names(container) + first_process = gunicorn_processes[0] + first_part, partition, last_part = first_process.partition("-c") + gunicorn_conf = last_part.strip().split()[0] + result = container.exec_run(f"python {gunicorn_conf}") + return json.loads(result.output.decode()) + + +def stop_previous_container(client): + try: + previous = client.containers.get("uvicorn-gunicorn-test") + previous.stop() + previous.remove() + except NotFound: + return None