FastAPI容器使用说明翻译

官方说明:https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker/blob/master/README.md

有多个环境变量可供设置,如果不指定,则会使用默认值。

Python “module” (文件) Gunicorn, this module would contain the actual application in a variable.

默认为:

  • app.main——如果主文件路径为 /app/app/main.py 或者
  • main——如果主文件路径为 /app/main.py

打个比方, 如果主文件路径为 /app/custom_app/custom_main.py, 那么你需要设置成这样:

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

默认是:

  • app

举个例子, 如果你的主文件是这样的:

1
2
3
4
5
6
7
from fastapi import FastAPI

api = FastAPI()

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

在上面的例子中,FastAPI()对象的变量名为api ,那么你需要这样设置:

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

默认为:

  • /app/gunicorn_conf.py 如果在该路径存在则使用此处的
  • /app/app/gunicorn_conf.py 如果在该路径存在则使用此处的
  • /gunicorn_conf.py (容器作者在此处放入了默认配置文件)

你可以这样设置:

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

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.

默认为:

  • 1

你可以这样设置:

1
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:

1
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: 默认为, 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.

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.

默认为 it’s not set, meaning that it’s unlimited.

你可以这样设置:

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

Override the automatic definition of number of workers.

默认为:

  • 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, 默认为 it will be set to 2.

你可以这样设置:

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

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

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

It is the host inside of the container.

因此, 如果你设置成 127.0.0.1, 那么将只能在容器中访问FastAPI服务.

默认为:

  • 0.0.0.0

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.

默认为:

  • 80

你可以这样设置:

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

The actual host and port passed to Gunicorn.

默认为, 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

你可以这样设置:

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

The log level for Gunicorn.

One of:

  • debug
  • info
  • warning
  • error
  • critical

默认为info.

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

你可以这样设置:

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

The class to be used by Gunicorn for the workers.

默认为, 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.

你可以这样设置:

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

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

Read more about it in the Gunicorn docs: timeout.

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.

你可以这样设置:

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

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

Read more about it in the Gunicorn docs: keepalive.

你可以这样设置:

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

Timeout for graceful workers restart.

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

默认为, set to 120.

你可以这样设置:

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

如果你想禁用访问日志,将其设置为空值即可:

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

如果你想禁用错误日志,将其设置为空值即可:

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