GithubHelp home page GithubHelp logo

tiangolo / uvicorn-gunicorn-docker Goto Github PK

View Code? Open in Web Editor NEW
602.0 12.0 252.0 206 KB

Docker image with Uvicorn managed by Gunicorn for high-performance web applications in Python with performance auto-tuning.

License: MIT License

Dockerfile 10.46% Python 84.12% Shell 5.42%
uvicorn gunicorn asgi web python async asyncio docker docker-image uvicorn-gunicorn

uvicorn-gunicorn-docker's Introduction

Test Deploy

Supported tags and respective Dockerfile links

Deprecated tags

🚨 These tags are no longer supported or maintained, they are removed from the GitHub repository, but the last versions pushed might still be available in Docker Hub if anyone has been pulling them:

  • python3.9-alpine3.14
  • python3.8-alpine3.10
  • python3.7-alpine3.8
  • python3.6
  • python3.6-alpine3.8

The last date tags for these versions are:

  • python3.9-alpine3.14-2024-03-11
  • python3.8-alpine3.10-2024-03-11
  • python3.7-alpine3.8-2024-03-11
  • python3.6-2022-11-25
  • python3.6-alpine3.8-2022-11-25

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/uvicorn-gunicorn:python3.7-2019-10-15.

uvicorn-gunicorn

Docker image with Uvicorn managed by Gunicorn for high-performance web applications in Python with performance auto-tuning.

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

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

Description

Python web applications running with Uvicorn (using the "ASGI" specification for Python asynchronous web applications) have shown to have some of the best performances, as measured by third-party benchmarks.

The achievable performance is on par with (and in many cases superior to) Go and Node.js frameworks.

This image has an auto-tuning mechanism included to start a number of worker processes based on the available CPU cores. That way you can just add your code and get high performance automatically, which is useful in simple deployments.

🚨 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 as explained in the docs for FastAPI in Containers - Docker: Build a Docker Image for FastAPI, that same process and ideas could be applied to other ASGI frameworks.


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 (like Gunicorn with Uvicorn workers) in each container, 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 Uvicorn process instead of this image.

For example, your Dockerfile could look like:

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 ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]

You can read more about this in the FastAPI documentation about: FastAPI in Containers - Docker as the same ideas would apply to other ASGI frameworks.

When to Use this Docker Image

A Simple App

You could want a process manager like Gunicorn running Uvicorn workers 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 Gunicorn process manager starting several Uvicorn 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 ideas would apply to any other ASGI framework.

Technical Details

Uvicorn

Uvicorn is a lightning-fast "ASGI" server.

It runs asynchronous Python web code in a single process.

Gunicorn

You can use Gunicorn to start and manage multiple Uvicorn worker processes.

That way, you get the best of concurrency and parallelism in simple deployments.

tiangolo/uvicorn-gunicorn

This image will set a sensible configuration based on the server it is running on (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 a slim version. If you want one of those, use one of the tags from above.

Frameworks

This image was created to be the base image for:

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 web application you should use tiangolo/uvicorn-gunicorn-starlette instead.

If you are creating a new FastAPI web application you should use tiangolo/uvicorn-gunicorn-fastapi instead.

Note: FastAPI is based on Starlette and adds several features on top of it. Useful for APIs and other cases: data validation, data conversion, documentation with OpenAPI, dependency injection, security/authentication and others.

Note: Unless you are doing something more technically advanced, you probably should be using Starlette with tiangolo/uvicorn-gunicorn-starlette or FastAPI with tiangolo/uvicorn-gunicorn-fastapi.

How to use

You don't need to clone the GitHub 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/uvicorn-gunicorn:python3.11

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 "ASGI" application.

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

docker build -t myimage ./
  • Run a container based on your image:
docker run -d --name mycontainer -p 80:80 myimage

You should be able to check it in your Docker container's URL, for example: http://192.168.99.100/ or http://127.0.0.1/ (or equivalent, using your Docker host).

Dependencies and packages

You will probably also want to add any dependencies for your app and pin them to a specific version, probably including Uvicorn and Gunicorn.

This way you can make sure your app always works as expected.

You could install packages with pip commands in your Dockerfile, using a requirements.txt, or even using Poetry.

And then you can upgrade those dependencies in a controlled way, running your tests, making sure that everything works, but without breaking your production application if some new version is not compatible.

Using Poetry

Here's a small example of one of the ways you could install your dependencies making sure you have a pinned version for each package.

Let's say you have a project managed with Poetry, so, you have your package dependencies in a file pyproject.toml. And possibly a file poetry.lock.

Then you could have a Dockerfile using Docker multi-stage building with:

FROM python:3.9 as requirements-stage

WORKDIR /tmp

RUN pip install poetry

COPY ./pyproject.toml ./poetry.lock* /tmp/

RUN poetry export -f requirements.txt --output requirements.txt --without-hashes

FROM tiangolo/uvicorn-gunicorn:python3.11

COPY --from=requirements-stage /tmp/requirements.txt /app/requirements.txt

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

COPY ./app /app

That will:

  • Install poetry and configure it for running inside of the Docker container.
  • Copy your application requirements.
    • Because it uses ./poetry.lock* (ending with a *), it won't crash if that file is not available yet.
  • Install the dependencies.
  • Then copy your app code.

It's important to copy the app code after installing the dependencies, that way you can take advantage of Docker's cache. That way it won't have to install everything from scratch every time you update your application files, only when you add new dependencies.

This also applies for any other way you use to install your dependencies. If you use a requirements.txt, copy it alone and install all the dependencies on the top of the Dockerfile, and add your app code after it.

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

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 ASGI application.

By default:

  • app

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

from fastapi import FastAPI

api = FastAPI()

@api.get("/")
def read_root():
    return {"message": "Hello world!"}

In this case api would be the variable with the "ASGI 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

You can use the config file from this image as a starting point for yours.

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:

  • 1

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.

Note: By default, if WORKERS_PER_CORE is 1 and the server has only 1 CPU core, instead of starting 1 single worker, it will start 2. This is to avoid bad performance and blocking applications (server application) on small machines (server machine/cloud/etc). This can be overridden using WEB_CONCURRENCY.

MAX_WORKERS

Set the maximum number of workers to use.

You can use it to let the image compute the number of workers automatically but making sure it's limited to a maximum.

This can be useful, for example, if each worker uses a database connection and your database has a maximum limit of open connections.

By default it's not set, meaning that it's unlimited.

You can set it like:

docker run -d -p 80:80 -e MAX_WORKERS="24" myimage

This would make the image start at most 24 workers, independent of how many CPU cores are available in the server.

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 2.

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

WORKER_CLASS

The class to be used by Gunicorn for the workers.

By default, set to uvicorn.workers.UvicornWorker.

The fact that it uses Uvicorn is what allows using ASGI applications like FastAPI and Starlette, and that is also what provides the maximum performance.

You probably shouldn't change it.

But if for some reason you need to use the alternative Uvicorn worker: uvicorn.workers.UvicornH11Worker you can set it with this environment variable.

You can set it like:

docker run -d -p 80:8080 -e WORKER_CLASS="uvicorn.workers.UvicornH11Worker" myimage

TIMEOUT

Workers silent for more than this many seconds are killed and restarted.

Read more about it in the Gunicorn docs: timeout.

By default, set to 120.

Notice that Uvicorn and ASGI frameworks like FastAPI and Starlette are async, not sync. So it's probably safe to have higher timeouts than for sync workers.

You can set it like:

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

KEEP_ALIVE

The number of seconds to wait for requests on a Keep-Alive connection.

Read more about it in the Gunicorn docs: keepalive.

By default, set to 2.

You can set it like:

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

GRACEFUL_TIMEOUT

Timeout for graceful workers restart.

Read more about it in the Gunicorn docs: graceful-timeout.

By default, set to 120.

You can set it like:

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

ACCESS_LOG

The access log file to write to.

By default "-", which means stdout (print in the Docker logs).

If you want to disable ACCESS_LOG, set it to an empty value.

For example, you could disable it with:

docker run -d -p 80:8080 -e ACCESS_LOG= myimage

ERROR_LOG

The error log file to write to.

By default "-", which means stderr (print in the Docker logs).

If you want to disable ERROR_LOG, set it to an empty value.

For example, you could disable it with:

docker run -d -p 80:8080 -e ERROR_LOG= myimage

GUNICORN_CMD_ARGS

Any additional command line settings for Gunicorn can be passed in the GUNICORN_CMD_ARGS environment variable.

Read more about it in the Gunicorn docs: Settings.

These settings will have precedence over the other environment variables and any Gunicorn config file.

For example, if you have a custom TLS/SSL certificate that you want to use, you could copy them to the Docker image or mount them in the container, and set --keyfile and --certfile to the location of the files, for example:

docker run -d -p 80:8080 -e GUNICORN_CMD_ARGS="--keyfile=/secrets/key.pem --certfile=/secrets/cert.pem" -e PORT=443 myimage

Note: instead of handling TLS/SSL yourself and configuring it in the container, it's recommended to use a "TLS Termination Proxy" like Traefik. You can read more about it in the FastAPI documentation about HTTPS.

PRE_START_PATH

The path where to find the pre-start script.

By default, set to /app/prestart.sh.

You can set it like:

docker run -d -p 80:8080 -e PRE_START_PATH="/custom/script.sh" myimage

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

You can customize the location of the prestart script with the environment variable PRE_START_PATH described above.

Development live reload

The default program that is run is at /start.sh. It does everything described above.

There's also a version for development with live auto-reload at:

/start-reload.sh

Details

For development, it's useful to be able to mount the contents of the application code inside of the container as a Docker "host volume", to be able to change the code and test it live, without having to build the image every time.

In that case, it's also useful to run the server with live auto-reload, so that it re-starts automatically at every code change.

The additional script /start-reload.sh runs Uvicorn alone (without Gunicorn) and in a single process.

It is ideal for development.

Usage

For example, instead of running:

docker run -d -p 80:80 myimage

You could run:

docker run -d -p 80:80 -v $(pwd):/app myimage /start-reload.sh
  • -v $(pwd):/app: means that the directory $(pwd) should be mounted as a volume inside of the container at /app.
    • $(pwd): runs pwd ("print working directory") and puts it as part of the string.
  • /start-reload.sh: adding something (like /start-reload.sh) at the end of the command, replaces the default "command" with this one. In this case, it replaces the default (/start.sh) with the development alternative /start-reload.sh.

Development live reload - Technical Details

As /start-reload.sh doesn't run with Gunicorn, any of the configurations you put in a gunicorn_conf.py file won't apply.

But these environment variables will work the same as described above:

  • MODULE_NAME
  • VARIABLE_NAME
  • APP_MODULE
  • HOST
  • PORT
  • LOG_LEVEL

🚨 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

Features

  • πŸ‘· Avoid creating unnecessary *.pyc files with PYTHONDONTWRITEBYTECODE=1 and ensure logs are printed immediately with PYTHONUNBUFFERED=1. PR #192 by @estebanx64.

Internal

  • πŸ”§ Add GitHub templates for discussions and issues, and security policy. PR #205 by @alejsdev.
  • πŸ”§ Update latest-changes.yml. PR #198 by @alejsdev.

0.8.0

Features

  • ✨ Add support for multi-arch builds, including support for arm64 (e.g. Mac M1). PR #195 by @tiangolo.

Docs

Refactors

Upgrades

Internal

0.7.0

The highlights of this release are:

  • Support for Python 3.10 and 3.11.
  • Deprecation of Python 3.6.
    • The last Python 3.6 image tag was pushed and is available in Docker Hub, but it won't be updated or maintained anymore.
    • The last image with a date tag is python3.6-2022-11-25.
  • Upgraded versions of all the dependencies.

Features

  • ✨ Add support for Python 3.11. PR #159 by @tiangolo.
  • ⬆️ Upgrade Uvicorn version. PR #161 by @tiangolo.
  • ✨ Add support for Python 3.10. PR #99 by @tiangolo.
  • ⬆️ Upgrade Uvicorn to the last version supporting Python 3.6. PR #155 by @tiangolo.
  • ✨ Add Python 3.9 and Alpine Python 3.9. PR #52 by @graue70.
  • ⬆️ Install uvicorn[standard] to include uvloop and Gunicorn support. PR #54 by @tiangolo.

Breaking Changes

  • πŸ”₯ Deprecate and remove Python 3.6. PR #160 by @tiangolo.

Docs

  • ✏️ Fix typo, delete repeated line in README. PR #147 by @jiyeonseo.
  • πŸ“ Add note to discourage Alpine with Python. PR #96 by @tiangolo.
  • πŸ“ Add warning for Kubernetes, when to use this image. PR #95 by @tiangolo.
  • ✏️ Fix typo duplicate "Note" in Readme. PR #92 by @tiangolo.
  • ✏️ Fix typo (type annotation) in tests. PR #55 by @tiangolo.

Internal

  • ⬆️ Update mypy requirement from ^0.971 to ^0.991. PR #166 by @dependabot[bot].
  • ⬆️ Update autoflake requirement from ^1.3.1 to ^2.0.0. PR #165 by @dependabot[bot].
  • ⬆️ Update black requirement from ^20.8b1 to ^22.10. PR #164 by @dependabot[bot].
  • ⬆️ Update docker requirement from ^5.0.3 to ^6.0.1. PR #163 by @dependabot[bot].
  • πŸ”§ Update Dependabot config. PR #162 by @tiangolo.
  • πŸ‘· Add scheduled CI every Monday. PR #158 by @tiangolo.
  • πŸ‘· Do not run double CI for PRs, run on push only on master. PR #157 by @tiangolo.
  • πŸ‘· Add GitHub Action alls-green. PR #156 by @tiangolo.
  • ⬆️ Update black requirement from ^19.10b0 to ^20.8b1. PR #87 by @dependabot[bot].
  • ⬆️ Update mypy requirement from ^0.770 to ^0.971. PR #143 by @dependabot[bot].
  • ⬆️ Update docker requirement from ^4.2.0 to ^5.0.3. PR #97 by @dependabot[bot].
  • ⬆️ Bump actions/setup-python from 1 to 4.1.0. PR #142 by @dependabot[bot].
  • ⬆️ Update isort requirement from ^4.3.21 to ^5.8.0. PR #88 by @dependabot[bot].
  • ⬆️ Bump tiangolo/issue-manager from 0.2.0 to 0.4.0. PR #85 by @dependabot[bot].
  • ⬆️ Update pytest requirement from ^5.4.1 to ^7.0.1. PR #123 by @dependabot[bot].
  • ⬆️ Bump actions/checkout from 2 to 3.1.0. PR #145 by @dependabot[bot].
  • πŸ“Œ Add external dependencies and Dependabot to get automatic upgrade PRs. PR #84 by @tiangolo.
  • πŸ‘· Update Latest Changes. PR #83 by @tiangolo.
  • πŸ”₯ Remove unused Travis and old GitHub Actions configs. PR #56 by @tiangolo.
  • πŸ‘· Add GitHub Action latest-changes, update issue-manager, add funding. PR #53 by @tiangolo.

0.6.0

  • Add docs about installing and pinning dependencies. PR #41.
  • Add slim version. PR #40.
  • Remove leftover unneeded config for tests. PR #39.
  • Add extra configs, tests, and docs for:
    • WORKER_CLASS
    • TIMEOUT
    • KEEP_ALIVE
    • GRACEFUL_TIMEOUT
    • ACCESS_LOG
    • ERROR_LOG
    • GUNICORN_CMD_ARGS
    • MAX_WORKERS
    • PR #38
  • Set up CI using GitHub actions, they provide more free instances, so builds finish faster (4 min vs 9 min). PR #37.
  • Add support for Python 3.8. PR #36.
  • Refactor tests to remove custom testing Dockerfiles, generate them during tests. PR #35.
  • Refactor and simplify build process to reduce code duplication. PR #34.
  • Disable pip cache during installation with --no-cache-dir. PR #13 by @pmav99.
  • Migrate local development from Pipenv to Poetry. PR #31.
  • Add tests and docs for custom PRE_START_PATH env var. PR #30.
  • Add support for custom PRE_START_PATH env var. PR #12 by @mgfinch.

0.5.0

  • Refactor tests to use env vars and add image tags for each build date, like tiangolo/uvicorn-gunicorn:python3.7-2019-10-15. PR #15.
  • Update Gunicorn worker heartbeat directory to /dev/shm to improve performance. PR #9 by @wshayes.
  • Upgrade Travis. PR #7.

0.4.0

  • Add support for live auto-reload with an additional custom script /start-reload.sh, check the updated documentation. PR #6.

0.3.0

  • Set WORKERS_PER_CORE by default to 1, as it shows to have the best performance on benchmarks.
  • Make the default web concurrency, when WEB_CONCURRENCY is not set, to a minimum of 2 workers. This is to avoid bad performance and blocking applications (server application) on small machines (server machine/cloud/etc). This can be overridden using WEB_CONCURRENCY. This applies for example in the case where WORKERS_PER_CORE is set to 1 (the default) and the server has only 1 CPU core. PR #5.

0.2.0

  • Make /start.sh run independently, reading and generating used default environment variables. And remove /entrypoint.sh as it doesn't modify anything in the system, only reads environment variables. PR #4.

0.1.2

  • Whenever this image is built (and each of its tags/versions), trigger a build for the children images (FastAPI and Starlette).

0.1.0

  • Add support for /app/prestart.sh.

License

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

uvicorn-gunicorn-docker's People

Contributors

alejsdev avatar dependabot[bot] avatar estebanx64 avatar graue70 avatar jiyeonseo avatar mgfinch avatar pmav99 avatar tiangolo avatar wshayes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

uvicorn-gunicorn-docker's Issues

Log message are missing

Our Fast API app uses the python logger in a few places like this:

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

Which we set at the top of the file that uses that logger. It works very well when we run uvicorn locally on our machine.
We get all our logging exactly as we like it.

But

When we run our app in the docker container FROM tiangolo/uvicorn-gunicorn:python3.8-2020-12-19
We get no logging from our app. All our logging statements are completely missing.
All we get is this:

web_1  | ip_address:port - "POST /<endpoint> HTTP/1.1" 200
web_1  | ip_address:port - "POST /<endpoint> HTTP/1.1" 200

What are we missing?
I tried setting the env variable LOG_LEVEL to "debug" but my logging statements were still missing.

Custom gunicorn configuration file for logging

Hi,
I have a custom gunicorn file with:

...
access_log_format = '%({x-forwarded-for}i)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'
...

This custom gunicorn file was working perfectly with the corresponding wsgi image at https://github.com/tiangolo/meinheld-gunicorn-docker.

When I use the image at https://github.com/tiangolo/uvicorn-gunicorn-docker, I just changed my Django application MODULE_NAME from the wsgi to the asgi. But the logs are without the information requested on access_log_format.

I really don't understand, I am struggling many hours with this; is it something related to uvicorn logging that is not "passing back" to gunicorn?

how gunicorn + uvicorn + uvloop + httptools

question

i had `pip install gunicorn uvicorn uvloop httptools`, and how to use them all?

in uvicorn origin code

uvicorn.workers.UvicornWorker   :  CONFIG_KWARGS = {"loop": "auto", "http": "auto"}
uvicorn.workers.UvicornH11Worker   :  CONFIG_KWARGS = {"loop": "asyncio", "http": "h11"}

Deploying docker image with Nginx

The Gunicorn documentation says to have an Nginx in front of the Gunicorn.

How to change the uvicorn-gunicorn-docker to add also the Nginx to handle the requests ?

Run as non-root

There are advantages to being able to run docker images as non-root. It would be nice if that was a supported path here ;-)

PID 1 zombie reaping

Hi,

I think this is more a gunicorn problem here. Currently, gunicorn is running as PID 1 in the Docker image, and my question is I wonder if gunicorn will reap out orphaned child processes?

Thanks
Bin

multiprocessing.cpu_count() gives incorrect number of available cores when used within a docker container

As discussed here:

If your container has been allocated (say) a single core and 512 MB of RAM on a machine with 64 cores, multiprocessing.cpu_count() thinks it has 64 cores instead of just 1, and you end up spawning 129 workers. This is way more than the container has resources for, and ends up causing out of memory crashes.

I ran into this issue with too many workers being spawned in a docker container on kubernetes, and my application would be in a constant CrashLoopBackoff state.

Debug logs

{"loglevel": "info", "workers": 64, "bind": "0.0.0.0:80", "graceful_timeout": 120, "timeout": 120, "keepalive": 5, "errorlog": "-", "accesslog": "-", "workers_per_core": 1.0, "use_max_workers": null, "host": "0.0.0.0", "port": "80"}

Docker Image

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7

Relevant Line of Code

cores = multiprocessing.cpu_count()

Solution

I've modified the default gunicorn_config.py to assign cores to an optional AVAILABLE_CORES variable; This is set when I define the number of cpus for the container.

cores = os.getenv("AVAILABLE_CORES", multiprocessing.cpu_count())

Wrong ML prediction inside the docker

Hi,
I have trained distilbert model with ktrain and deployed it with this docker. I am getting a very weird result inside the docker. ml prediction is completely different from what I am getting out of this docker. I had also gone inside the docker with the docker shell. I loaded the model there and tested but the result was again wrong.

I don't understand how this could be possible? I am using this config inside and outside of the docker.

python 3.7
tensorflow 2.3
ktrain 0.25.2
transformers 4.3
pytorch 1.7

Any idea how this could be possible?

Adding --env-file to uvicorn development script

Hi,

Please consider adding environment file path to the configurable env vars

# uvicorn-gunicorn-docker/docker-images/start-reload.sh
...
...
...
# Start Uvicorn with live reload
exec uvicorn --reload --host $HOST......... --env-file $PATH_TO_THE_ENV_FILE

Thanks

python3.8-slim image: Time not Synced to Local Machine

I'm not sure if this is due to an incorrect configuration on my part but my docker containers using the python3.8-slim image don't sync to the time zone of my local machine. I believe my time zone is 4 hours ahead of what it should be.

RuntimeError: Task attached to a different loop

Yea, Hello everyone! Today, I tried to put my program into production, and this happened:
2021-08-17T14:35:18.959815442Z RuntimeError: Task <Task pending name='Task-7' coro=<RequestResponseCycle.run_asgi() running at /usr/local/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py:375> cb=[set.discard()]> got Future <Future pending cb=[_chain_future.<locals>._call_check_cancel() at /usr/local/lib/python3.8/asyncio/futures.py:360]> attached to a different loop . Yea, I don't know how to fix it. The line which produces the problem: if await col.find_one({"name": name}) is not None: (Which works great with uvicorn outside of the container.)
Edit:
I am using motor (MongoDB)

How to edit prestart.sh?

I appreciate that a lot of parameters are set for you, but how can I manually edit them? I would rather not have a massive line of -e flags in my docker run command.

Such as:

{
    "loglevel": loglevel,
    "workers": workers,
    "bind": bind,
    "graceful_timeout": graceful_timeout,
    "timeout": timeout,
    "keepalive": keepalive,
    "errorlog": errorlog,
    "accesslog": accesslog,
    # Additional, non-gunicorn variables
    "workers_per_core": workers_per_core,
    "use_max_workers": use_max_workers,
    "host": host,
    "port": port,
}

How to use "tiangolo/uvicorn-gunicorn-fastapi" under "conda" environment, and at the same time the command is "/bin/bash"?

Hi,
I used β€œFROM tiangolo/uvicorn-gunicorn-fastapi:python3.7” in the Dockerfile, however the command I got after docker running is "/start.sh". Due to my need is to call "tiangolo/fastapi" using "FROM continuumio/miniconda3", whether can I let the two different "FROM ..." ("FROM continuumio/miniconda3" and β€œFROM tiangolo/uvicorn-gunicorn-fastapi:python3.7”) be combined in the same Dcokerfile?
Could you pls give me some suggestions?
Note: My Dockerfile is below:
1
Moreover, the results of executing "docker ps" while using "FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7":
2
Another result of executing "docker ps" while using "FROM continuumio/miniconda3":
3

Updating python:3.8 base image does not update python interpreter version

Hi,

Please advise on this simple issue I'm having.

I've updated python:3.8 base image to the current version of python 3.8.6:

$ docker run -ti python:3.8 /bin/bash
root@5aa362248230:/# python
Python 3.8.6 (default, Sep 24 2020, 21:45:12)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

But I'm not succeeding in making your image see the change in the base image:

$ docker run -ti tiangolo/uvicorn-gunicorn python
Python 3.8.2 (default, Apr 23 2020, 14:22:33)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

I've tried removing the image and downloading it again:

$ docker run -ti tiangolo/uvicorn-gunicorn /bin/bash
Unable to find image 'tiangolo/uvicorn-gunicorn:latest' locally
latest: Pulling from tiangolo/uvicorn-gunicorn
90fe46dd8199: Pull complete
35a4f1977689: Pull complete
bbc37f14aded: Pull complete
74e27dc593d4: Pull complete
4352dcff7819: Pull complete
deb569b08de6: Pull complete
98fd06fa8c53: Pull complete
7b9cc4fdefe6: Pull complete
e8e1fd64f499: Pull complete
ea7ce61ac120: Pull complete
2d28617d3c82: Pull complete
f21b58a51d21: Pull complete
f48f85f101f4: Pull complete
ccf64c58a3ca: Pull complete
598614c5ce2f: Pull complete
fd65fe0e6e14: Pull complete
Digest: sha256:5981a4a455f50c8617892c847baefb553dd4ebfaa0dfb017f78708cb243f768e
Status: Downloaded newer image for tiangolo/uvicorn-gunicorn:latest
root@c5008c0c80ac:/app# python
Python 3.8.2 (default, Apr 23 2020, 14:22:33)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

How to make it upgrade the interpreter ?

Thanks for your attention.

Unix Socket Image available ?

Hey,

is there an image which works with unix sockets (I want to talk to my uvicorn container via nginx reverse proxy and socket) instead of TCP ?

Uvicorn not responding to requests

Hello, I am using your image for a Quart application that I want to deploy but every time I run it it just hangs and I get this output in the logs:

[2020-09-27 18:24:20 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2020-09-27 18:24:20 +0000] [1] [INFO] Listening at: http://0.0.0.0:80 (1)
[2020-09-27 18:24:20 +0000] [1] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2020-09-27 18:24:20 +0000] [7] [INFO] Booting worker with pid: 7
[2020-09-27 18:24:20 +0000] [8] [INFO] Booting worker with pid: 8
[2020-09-27 18:24:20 +0000] [9] [INFO] Booting worker with pid: 9
[2020-09-27 18:24:20 +0000] [10] [INFO] Booting worker with pid: 10
[2020-09-27 18:24:20 +0000] [11] [INFO] Booting worker with pid: 11
[2020-09-27 18:24:20 +0000] [12] [INFO] Booting worker with pid: 12
[2020-09-27 18:24:20 +0000] [13] [INFO] Booting worker with pid: 13
[2020-09-27 18:24:20 +0000] [14] [INFO] Booting worker with pid: 14
[2020-09-27 18:24:20 +0000] [10] [INFO] Started server process [10]
[2020-09-27 18:24:20 +0000] [10] [INFO] Waiting for application startup.
[2020-09-27 18:24:20 +0000] [7] [INFO] Started server process [7]
[2020-09-27 18:24:20 +0000] [7] [INFO] Waiting for application startup.
[2020-09-27 18:24:20 +0000] [11] [INFO] Started server process [11]
[2020-09-27 18:24:20 +0000] [11] [INFO] Waiting for application startup.
[2020-09-27 18:24:20 +0000] [8] [INFO] Started server process [8]
[2020-09-27 18:24:20 +0000] [12] [INFO] Started server process [12]
[2020-09-27 18:24:20 +0000] [12] [INFO] Waiting for application startup.
[2020-09-27 18:24:20 +0000] [8] [INFO] Waiting for application startup.
[2020-09-27 18:24:20 +0000] [13] [INFO] Started server process [13]
[2020-09-27 18:24:20 +0000] [13] [INFO] Waiting for application startup.
[2020-09-27 18:24:20 +0000] [9] [INFO] Started server process [9]
[2020-09-27 18:24:20 +0000] [9] [INFO] Waiting for application startup.
[2020-09-27 18:24:21 +0000] [14] [INFO] Started server process [14]
[2020-09-27 18:24:21 +0000] [14] [INFO] Waiting for application startup.
[2020-09-27 18:26:20 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:7)
[2020-09-27 18:26:20 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:8)
[2020-09-27 18:26:20 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:9)
[2020-09-27 18:26:20 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:10)
[2020-09-27 18:26:20 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:11)
[2020-09-27 18:26:20 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:12)
[2020-09-27 18:26:20 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:13)
[2020-09-27 18:26:20 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:14)
[2020-09-27 18:26:20 +0000] [15] [INFO] Booting worker with pid: 15
[2020-09-27 18:26:20 +0000] [16] [INFO] Booting worker with pid: 16
[2020-09-27 18:26:20 +0000] [17] [INFO] Booting worker with pid: 17

If I remove the startup function that connects to the database using the databases module it works fine. Any advice?

Thanks!

SIGINT takes long time to shut down

Using this image I have noticed that hitting ctrl+c when the application is running takes a long time for the process to actually halt, around 30s.

$ docker run --rm --volume ${PWD}:/app tiangolo/uvicorn-gunicorn:python3.7-alpine3.8
Checking for script in /app/prestart.sh
There is no script /app/prestart.sh
[2019-10-25 16:43:56 +0000] [6] [INFO] Starting gunicorn 19.9.0
[2019-10-25 16:43:56 +0000] [6] [INFO] Listening at: http://0.0.0.0:8080 (6)
[2019-10-25 16:43:56 +0000] [6] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2019-10-25 16:43:56 +0000] [8] [INFO] Booting worker with pid: 8
[2019-10-25 16:43:56 +0000] [9] [INFO] Booting worker with pid: 9
email-validator not installed, email fields will be treated as str.
To install, run: pip install email-validator
...
[2019-10-25 16:43:57 +0000] [15] [INFO] Started server process [15]
[2019-10-25 16:43:57 +0000] [15] [INFO] Waiting for application startup.
^C[2019-10-25 16:43:59 +0000] [6] [INFO] Handling signal: int
^C^C^C[2019-10-25 16:44:29 +0000] [6] [INFO] Shutting down: Master
{"loglevel": "info", "workers": 8, "bind": "0.0.0.0:8080", "workers_per_core": 1.0, "host": "0.0.0.0", "port": "8080"}

When running with the /start-reload.sh script, uvicorn recieves the signal, but never terminates. I have to run a docker killfrom another terminal.

$ docker run --rm --volume ${PWD}:/app tiangolo/uvicorn-gunicorn:python3.7-alpine3.8 /start-reload.sh
Checking for script in /app/prestart.sh
There is no script /app/prestart.sh
INFO: Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
INFO: Started reloader process [1]
email-validator not installed, email fields will be treated as str.
To install, run: pip install email-validator
INFO: Started server process [7]
INFO: Waiting for application startup.
^CINFO: Stopping reloader process [1]
^C^C^C^C^C^C^C^C^C^C

The issue is with the regular and alpine variants.

Image is not running

Im using the python3.7-alpine3.8 folder to build the container

docker build -t myimage ./
docker run --name mycontainer -p 80:80 myimage

': No such file or director

add a :latest-alpine tag

it would be nice to have a generic :latest-alpine or simply :alpine tag to pull the latest version of python/alpine without using the verbose :python3.7-alpine3.8 tag

About section :)

Pretty sure there should be Python 3 not 3.6

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

Cheers!

Updated Uvicorn to 0.14.0 to increase ws max_size to > 1mb, but doesn't work, & get "error walking file system: FileNotFoundError [Errno 2] No such file or directory: '/app/.venv/bin/python'"

I needed to update Uvicorn to 0.14.0 since this version permits websockets to send > 1mb of data in a message. However, since 0.12.0, Uvicorn no longer includes uvloop, websockets and httptools as default, which requires uvicorn[standard].

Updated in Poetry. However, when I then restart the Docker image, I get the following error scrolling off indefinitely:

error walking file system: FileNotFoundError [Errno 2] No such file or directory: '/app/.venv/bin/python'

These articles give me some indication it has something to do with Uvicorn and reload, but I'm a little lost as to how to fix it:

If I use /start.sh instead of /start-reload.sh the problem goes away, which then reveals that the ws max_size limit is still 1mb. Which may have something to do with gunicorn.

I'd like to edit /start.sh or /start-reload.sh to see if I can fix things there, but can't figure out how to do that (although, potentially, that involves forking the repo?).

I am now thoroughly lost as the errors cascade, and - before I bury myself any deeper - thought I'd ask for help here.

My starting point is the https://github.com/tiangolo/full-stack-fastapi-postgresql deployment.

Remove redundant syntax

little things.
I noticed you used virtual package in the alpine dockerfile.
As I know, once you use this feature, you don't have to list all packages in the apk del command.
apk del .build-deps should work perfect here.

https://github.com/tiangolo/uvicorn-gunicorn-docker/blob/master/docker-images/python3.7-alpine3.8.dockerfile#L7

more discussions:
https://stackoverflow.com/questions/46221063/what-is-build-deps-for-apk-add-virtual-command/46222036

Inpired a lot by your repo. good job man!

How to enable all debug which is available?

Hello.
Maybe, stupid question, but still:
how to enable all logs which is available?
I see INFO, CRITICAL and DEBUG messages, but do not see all access logs and error logs,
I added these environment variables:

LOG_LEVEL: "debug"
ACCESS_LOG: "-"
ERROR_LOG: "-"

[feat] support passing options to gunicorn

It would be nice to be able to pass options to gunicorn.

I myself need to start gunicorn with the --access-logformat ... option. And #14 could be solved by passing the --access-logfile /dev/null option.

Ideally it would behave like most official Docker images :

  • running the container without command would start gunicorn without additional options (exact same behavior as today)
  • running the container with a command would either :
    • run that command. i.e: docker run --rm -it tiangolo/uvicorn-gunicorn:python3.7 bash
    • add options to the gunicorn command. i.e.: docker run --rm -it tiangolo/uvicorn-gunicorn:python3.7 --proxy-headers <---- this is the new feature

This would require to alter the start.sh script. One could copy part of the official mongo entrypoint : https://github.com/docker-library/mongo/blob/2628a9b53013985d9aec377567f664b68b79e3d9/3.4/docker-entrypoint.sh

and modifying the Dockerfile so that it reads:

ENTRYPOINT ["/start.sh"]
CMD []

SSL?

Is there a recommended way to serve these requests over HTTPS?

Only prints `log_data` if `LOG_LEVEL` == `DEBUG`

I think that container only needs to print log_data from docker-image/gunicorn_conf.py if LOG_LEVEL is set up as DEBUG, otherwise there is no need for that.

Is there a way to supress that print that am I missing?

python3.7-slim

Is it possible to include a docker image with python3.7-slim-buster ?

Best Regards.

[QUESTION] Not understanding the web concurrency going on

First of all - I am not sure if this is this is the right place to ask. But you do have great documentation that brings production ready web applications available for people like me so I'm guessing there are others out there that are facing some of the same challenges like me.

Context

I'm using a FastAPI application to trigger an ETL process which involves spaCy models as well. It is POST only, i.e. the application do not need to return any answer, it is used for "task triggering". Some times the application receive up to 30 post requests within 100 ms but it shouldn't add any delay compared to the case where a single statement arrives. So now I'm trying to figure out how to handle this properly - first steps for me is to understand the basics but I've stumpled upon a mystery to me.

Example

I've made a simple application that sleeps for 3 seconds on any requests (and prints the time before and after the request)

Dockerfile

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7-alpine3.8
ENV WEB_CONCURRENCY=1
ENV LOG_LEVEL="warning"

WORKDIR /app
COPY main.py .

main.py

from time import sleep
import datetime
from fastapi import FastAPI
import logging

logging.basicConfig(level=logging.WARNING)

app = FastAPI()


def print_now(message: str):
    print(f"{datetime.datetime.now().strftime('%H:%M:%S')}: {message}")


def handle(i: int):
    print_now(f"Received {i}")
    sleep(3)
    print_now(f"Handled {i}")


@app.get("/")
def root(i: int):
    handle(i)

I've made a small test script that makes 100 requests at around the same time (each request made in a background thread)

requests.sh

#!/bin/bash
for i in {1..100}
do
   curl http://localhost/?i=${i} &
done

Results

The current settings are (i.e. 1 worker)

{"loglevel": "warning", "workers": 1, "bind": "0.0.0.0:80", "workers_per_core": 1.0, "host": "0.0.0.0", "port": "80"}

Running ./requests.sh then I see that my app handles max 30 requests at the same time (running on my macbook pro with 6 cores).

  1. Why 30!?
  2. If I change def root(i: int): to async def root(i: int): then my app handles 1 request per worker. Why is that different?
  3. If I change the application to perform the task in a new thread then all requests are handled right away (until the machine is out of resources I assume), i.e.
@app.get("/")
def root(i: int):
    t = threading.Thread(target=handle, args=(i,))
    t.daemon = True
    t.start()
  1. but I feel like this is bad practice?

I plan to have a load balancer in front but that doesn't change that the configurations on my containers should still be "right".

πŸ’₯I think one of the "issues" here is that FastAPI is making it extremely easy for a guy like me (who doesn't know too much about gunicorn configurations, concurrency, async etc.) to (almost) make production ready web applications. I thank you for that! On a final note; is FastAPI even the right tool for this "task triggering" where speed is everything (100 ms differences is big)?

Update PyYAML to >= 5.4 for CVE-2020-14343

Hello! I'm encountering a security failure for pipenv check using one of these base images.

Issue

  • IMAGE: tiangolo/uvicorn-gunicorn:python3.8-slim-2020-12-19
  • CVE-2020-14343
#9 11.44 Checking PEP 508 requirements...
#9 11.49 Passed!
#9 11.49 Checking installed package safety...
#9 13.31 39611: pyyaml <5.4 resolved (5.3.1 installed)!
#9 13.31 A vulnerability was discovered in the PyYAML library in versions before 5.4, where it is susceptible to arbitrary code execution when it processes untrusted YAML files through the full_load method or with the FullLoader loader. Applications that use the library to process untrusted input may be vulnerable to this flaw. This flaw allows an attacker to execute arbitrary code on the system by abusing the python/object/new constructor. This flaw is due to an incomplete fix for CVE-2020-1747. See CVE-2020-14343.

Reproduce

Simple Dockerfile:

FROM tiangolo/uvicorn-gunicorn:python3.8-slim-2020-12-19 as base
RUN pip freeze \
    && pip install pipenv \
    && pipenv check
docker build .

..........

#6 [base 2/2] RUN pip freeze
#6 sha256:e76efe7bb1013b8ea339937c42cd3938acaff5f50fc90d629df2396671b97ca4
#6 0.553 click==7.1.2
#6 0.553 gunicorn==20.0.4
#6 0.553 h11==0.11.0
#6 0.553 httptools==0.1.1
#6 0.553 python-dotenv==0.15.0
#6 0.553 PyYAML==5.3.1
#6 0.553 uvicorn==0.13.1
#6 0.553 uvloop==0.14.0
#6 0.553 watchgod==0.6
#6 0.553 websockets==8.1
#6 DONE 0.6s

Where PyYAML==5.3.1 must be updated.

Gist

https://gist.github.com/adveres/a1ddf55cd4491907431ae5f062ca38f2

Upgrade to Python 3.8.8 to fix CVE-2021-3177

Source: https://www.zdnet.com/article/python-programming-language-hurries-out-update-to-tackle-remote-code-vulnerability/

Hi, so we're using tiangolo/uvicorn-gunicorn:python3.8 for one of our services right now, and we'd like to make sure that they're all running Python 3.8.8 under the hood (as per the article above), but Poetry can't seem to pull anything greater than 3.8.6 when using either tiangolo/uvicorn-gunicorn:python3.8 or tiangolo/uvicorn-gunicorn:latest.

Am I just using the wrong command or does a new image need to be pushed to Docker Hub?

 ---> Running in 3dd3fb755f88
The currently activated Python version 3.8.6 is not supported by the project (3.8.8).
Trying to find and use a compatible version.

  NoCompatiblePythonVersionFound

  Poetry was unable to find a compatible version. If you have one, you can explicitly use it via the "env use" command.```

gunicorn: error: no such option: -k ... docker run exiting and booting

When I run my docker container, it constantly kills and boots the workers because of this gunicorn error. Any ideas?

{"loglevel": "info", "workers": 2, "bind": "0.0.0.0:80", "workers_per_core": 1.0, "host": "0.0.0.0", "port": "80"}
Usage: gunicorn [options]
gunicorn: error: no such option: -k
[2020-03-10 18:57:33 +0000] [12] [INFO] Worker exiting (pid: 12)
[2020-03-10 18:57:33 +0000] [10] [INFO] Worker exiting (pid: 10)
[2020-03-10 18:57:34 +0000] [22] [INFO] Booting worker with pid: 22
[2020-03-10 18:57:34 +0000] [24] [INFO] Booting worker with pid: 24

Dockerfile

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.6

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

WORKDIR /app
RUN pip install --upgrade -r requirements.txt

I've tried adding the latest gunicorn to the requirements so it installs over what comes from this repo.

Images are out of date

Could we have some sort of automatic build when we have bug-fix/security updates in python images?

Feature: Disable access log when using live-reload

When using gunicorn (via start.sh), one can disable the API access log via setting the corresponding environment variable to an empty value: ACCESS_LOG=. However, this is not possible when running the app with uvicorn and live-reload (via start-reload.sh).

But since you can pass the option --no-access-log to uvicorn (see docs), the script could be extended to disable the access log if the variable is set to an empty value.

Allow to disable access logs

It would be great to document and/or add the possibility to disable access logs on top of setting the log level.

My use case is that the api is deployed behind a proxy that already is responsible for logging the access logs, so it is redundant for me to have it here.

Python 3.9

As a developer, i would like to get a docker image based on python 3.9

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.