FastAPI Docker 项目教程

FastAPI Docker 项目教程

uvicorn-gunicorn-fastapi-dockerDocker image with Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python with performance auto-tuning. Optionally with Alpine Linux.项目地址:https://gitcode.com/gh_mirrors/uv/uvicorn-gunicorn-fastapi-docker

项目的目录结构及介绍

FastAPI Docker 项目的目录结构如下:

uvicorn-gunicorn-fastapi-docker/
├── docker-images/
│   └── python3.11/
│       └── Dockerfile
├── .github/
├── .gitignore
├── LICENSE
├── README.md
├── docker-compose.yml
└── requirements.txt

目录结构介绍

  • docker-images/: 包含不同 Python 版本的 Dockerfile。
  • python3.11/: 特定 Python 版本的 Dockerfile。
  • .github/: GitHub 相关配置文件。
  • .gitignore: Git 忽略文件配置。
  • LICENSE: 项目许可证。
  • README.md: 项目说明文档。
  • docker-compose.yml: Docker Compose 配置文件。
  • requirements.txt: Python 依赖包列表。

项目的启动文件介绍

项目的启动文件主要是 Dockerfile 和 docker-compose.yml。

Dockerfile

Dockerfile 内容如下:

FROM python:3.11

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"]

docker-compose.yml

docker-compose.yml 内容如下:

version: '3.7'

services:
  web:
    build: .
    ports:
      - "80:80"
    environment:
      - WORKER_CLASS=uvicorn.workers.UvicornH11Worker
      - TIMEOUT=120
      - KEEP_ALIVE=2
      - GRACEFUL_TIMEOUT=120

项目的配置文件介绍

项目的配置文件主要是 Dockerfile 和 docker-compose.yml 中的环境变量配置。

环境变量配置

  • WORKER_CLASS: 指定 Uvicorn 的工作类。
  • TIMEOUT: 设置 Gunicorn 的超时时间。
  • KEEP_ALIVE: 设置 Keep-Alive 连接的等待时间。
  • GRACEFUL_TIMEOUT: 设置优雅重启的超时时间。

通过这些配置,可以灵活地调整 FastAPI 应用的运行参数,以适应不同的生产环境需求。

uvicorn-gunicorn-fastapi-dockerDocker image with Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python with performance auto-tuning. Optionally with Alpine Linux.项目地址:https://gitcode.com/gh_mirrors/uv/uvicorn-gunicorn-fastapi-docker

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

平均冠Zachary

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值