FastAPI是一种现代,快速(高性能)的Web框架,用于基于标准Python类型提示使用Python 3.6+构建API,对飚go和nodejs。
FastAPI github地址:https://github.com/tiangolo/fastapi
Uvicorn
Uvicorn是一款闪电般的“ ASGI”服务器。
它在单个过程中运行异步Python Web代码。
Gunicorn
您可以使用Gunicorn管理Uvicorn和运行多个这些并发进程。
这样,您将获得最佳的并发性和并行性。
FastAPI
FastAPI是一种现代,快速(高性能)的Web框架,用于使用Python 3.6+构建API。
步骤一:创建dockfile
#Dockerfile
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
COPY ./app /app
dockerfile的意思就是把当前目录(./app)复制到 /app文件夹中。
步骤二:创建FastAPI文件
mkdir app
vim main.py
main.py文件内容如下:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
目录结构如下:
├── app
│ └── main.py
└── Dockerfile
步骤三:创建镜像
docker build -t fastapi_image ./
Sending build context to Docker daemon 3.584kB
Step 1/2 : FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
---> 23579a0a7c50
Step 2/2 : COPY ./app /app
---> Using cache
---> 8c41afea5ee1
Successfully built 8c41afea5ee1
Successfully tagged fastapi_image:latest
查看创建的image
docker image ls|grep fastapi_image
步骤四:启动应用
docker run -d --name fastapi_container -p 10001:80 fastapi_image
15984271b4792db1025224da534be5951307b8433dc8e41babbcba073a2baa24
查看刚才启动的应用
docker container ls |grep fastapi
15984271b479 fastapi_image "/start.sh" About a minute ago Up About a minute 0.0.0.0:10001->80/tcp fastapi_container
步骤五:访问应用
通过浏览器 http://ip地址:10001
或者命令行:curl http://127.0.0.1:10001
{"Hello":"World"}
http://127.0.0.1/docs #API文档
http://127.0.0.1/redoc #备用API文档
下面我们来分析下镜像的构建过程:
docker image ls |grep fastapi
docker history fastapi_image:latest
[root@kube-test-master ~]# docker history fastapi_image:latest
IMAGE CREATED CREATED BY SIZE COMMENT
8c41afea5ee1 2 days ago /bin/sh -c #(nop) COPY dir:d1727655ba287a3f2… 223B
23579a0a7c50 5 weeks ago /bin/sh -c #(nop) COPY dir:81482727cef4cc213… 292B
由以上信息可以知道workdir 是/app,应用启动脚本start.sh,重启脚本start-reload.sh
进入容器进一步验证
docker container ls |grep fastapi
15984271b479 fastapi_image "/start.sh" 34 minutes ago Up 34 minutes 0.0.0.0:10001->80/tcp fastapi_container
docker exec -it 15984271b479 /bin/bash
root@15984271b479:/app# ls
__pycache__ main.py prestart.sh
root@15984271b479:/app# cd ..
root@15984271b479:/# ls
app boot etc home lib64 mnt proc run srv start.sh tmp var
bin dev gunicorn_conf.py lib media opt root sbin start-reload.sh sys usr
root@15984271b479:/#
start.sh start-reload.sh位于根目录
root@15984271b479:/# cat start.sh
#! /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}
export WORKER_CLASS=${WORKER_CLASS:-"uvicorn.workers.UvicornWorker"}
# If there's a prestart.sh script in the /app directory or other path specified, run it before starting
PRE_START_PATH=${PRE_START_PATH:-/app/prestart.sh}
echo "Checking for script in $PRE_START_PATH"
if [ -f $PRE_START_PATH ] ; then
echo "Running script $PRE_START_PATH"
. "$PRE_START_PATH"
else
echo "There is no script $PRE_START_PATH"
fi
# Start Gunicorn
exec gunicorn -k "$WORKER_CLASS" -c "$GUNICORN_CONF" "$APP_MODULE"
gunicorn_conf.py
root@15984271b479:/# cat gunicorn_conf.py
import json
import multiprocessing
import os
workers_per_core_str = os.getenv("WORKERS_PER_CORE", "1")
max_workers_str = os.getenv("MAX_WORKERS")
use_max_workers = None
if max_workers_str:
use_max_workers = int(max_workers_str)
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 = max(int(default_web_concurrency), 2)
if use_max_workers:
web_concurrency = min(web_concurrency, use_max_workers)
accesslog_var = os.getenv("ACCESS_LOG", "-")
use_accesslog = accesslog_var or None
errorlog_var = os.getenv("ERROR_LOG", "-")
use_errorlog = errorlog_var or None
graceful_timeout_str = os.getenv("GRACEFUL_TIMEOUT", "120")
timeout_str = os.getenv("TIMEOUT", "120")
keepalive_str = os.getenv("KEEP_ALIVE", "5")
# Gunicorn config variables
loglevel = use_loglevel
workers = web_concurrency
bind = use_bind
errorlog = use_errorlog
worker_tmp_dir = "/dev/shm"
accesslog = use_accesslog
graceful_timeout = int(graceful_timeout_str)
timeout = int(timeout_str)
keepalive = int(keepalive_str)
# For debugging and testing
log_data = {
"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,
}
print(json.dumps(log_data))
其中有两个变量 ACCESS_LOG ERROR_LOG是日志输出的路径,如果没定义默认不输出日志。
如果是正式环境,建议设置路径。具体见官方文档,并使用 -v参数映射到宿主机目录。
docker container logs fastapi_container 查看容器logs
docker container inspect fastapi_container
可以获取容器的详细信息
其中id 15984271b4792db1025224da534be5951307b8433dc8e41babbcba073a2baa24
存放路径位置
/var/lib/docker/containers/15984271b4792db1025224da534be5951307b8433dc8e41babbcba073a2baa24
{"log":"{\"loglevel\": \"info\", \"workers\": 2, \"bind\": \"0.0.0.0:80\", \"graceful_timeout\": 120, \"timeout\": 120, \"keepa
live\": 5, \"errorlog\": \"-\", \"accesslog\": \"-\", \"workers_per_core\": 1.0, \"use_max_workers\": null, \"host\": \"0.0.0.0
\", \"port\": \"80\"}\n","stream":"stdout","time":"2020-06-08T01:58:54.535459288Z"}
{"log":"192.168.101.202:62512 - \"GET / HTTP/1.1\" 200\n","stream":"stdout","time":"2020-06-08T01:58:54.535495087Z"}
{"log":"192.168.101.202:62512 - \"GET /favicon.ico HTTP/1.1\" 404\n","stream":"stdout","time":"2020-06-08T01:58:55.271215897Z"}
{"log":"172.17.0.1:41038 - \"GET / HTTP/1.1\" 200\n","stream":"stdout","time":"2020-06-08T02:00:02.409539216Z"}
{"log":"{\"loglevel\": \"info\", \"workers\": 2, \"bind\": \"0.0.0.0:80\", \"graceful_timeout\": 120, \"timeout\": 120, \"keepa
live\": 5, \"errorlog\": \"-\", \"accesslog\": \"-\", \"workers_per_core\": 1.0, \"use_max_workers\": null, \"host\": \"0.0.0.0
\", \"port\": \"80\"}\n","stream":"stdout","time":"2020-06-08T02:00:07.276382848Z"}
{"log":"172.17.0.1:41074 - \"GET / HTTP/1.1\" 200\n","stream":"stdout","time":"2020-06-08T02:00:07.276421756Z"}