FastAPI?
这是一个现代而快速的Python Web框架。
它最近似乎很流行,因此我将其用于商业。
官方文件
https://github.com/tiangolo/fastapi
环境因成员而异,因此介绍不能顺利进行,但是
这是一条备忘录,因为现在每个人都可以使用Docker共享相同的工作环境。
创建的文件
我们将按照官方程序进行。
如果文件结构最终像这样,可以。
超级简单。
Dockerfile
安装官方文档"安装"部分中描述的模块。
它在构建时会自行运行,因此很方便,因为您无需手动搜索所需的模块。
Docker文件
1 2 3 | FROM python:3.7 WORKDIR /var/www/html RUN pip install fastapi uvicorn |
main.py
这也是参考Example的项目编写的。
这是一个API,可在访问根目录时返回Hello World对象。
main.py
1 2 3 4 5 6 7 | from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} |
docker-compose.yaml
它充当
使用Dockerfile安装Python模块后,
请参阅"运行它"项,然后单击命令以启动Fast API服务器。
在这里,它设置为从端口9004开始。
docker-compose.yaml
1 2 3 4 5 6 7 8 9 10 11 | version: '3' services: app: container_name: FastAPI build: ./docker volumes: - ./src:/var/www/html ports: - "9004:9004" command: uvicorn main:app --reload --host 0.0.0.0 --port 9004 |
操作检查
使用
docker-compose启动并构建容器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | $ docker-compose up --build Creating network "docker_fastapi_default" with the default driver Building app Step 1/3 : FROM python:3.7 ---> 879165535a54 Step 2/3 : WORKDIR /var/www/html ---> Using cache ---> 31d5c58e6177 Step 3/3 : RUN pip install fastapi uvicorn ---> Using cache ---> 430430eecf7f Successfully built 430430eecf7f Successfully tagged docker_fastapi_app:latest Creating FastAPI ... done Attaching to FastAPI FastAPI | INFO: Uvicorn running on http://0.0.0.0:9004 (Press CTRL+C to quit) FastAPI | INFO: Started reloader process [1] FastAPI | INFO: Started server process [7] FastAPI | INFO: Waiting for application startup. FastAPI | INFO: Application startup complete. |
已安装必需的模块,并且FastAPI服务器已启动。
让我们使用
curl命令访问它。
1 2 3 | $ curl http://localhost:9004 {"Hello":"World"} |
概要
您可以使用FastAPI轻松设置API服务器!
能够使用Docker
与少量文件共享同一环境真是太好了
Python也可以执行shell脚本,因此
实现用于自动部署的命令并执行诸如" pseudo Jenkins"之类的操作很方便。
不要说您应该使用Jenkins del>
参考网址
- GitHub | FastAPI
- Docker Hub | python