> For the complete documentation index, see [llms.txt](https://close.gitbook.io/yun-wei-bi-ji/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://close.gitbook.io/yun-wei-bi-ji/python/fastapi/fastapi-docker-fang-shi.md).

# FastAPI Docker 方式

* Dockfile

```python
FROM python:3.9.9-alpine

RUN pip install fastapi uvicorn aiofiles fastapi-async-sqlalchemy python-multipart

EXPOSE 80

COPY . .

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]
```

* 打包

```bash
docker build -t myfastapi .
```

* 部署

```bash
docker run -d --name myfastapi -p 80:80 myfastapi
```

* 保存镜像

```bash
docker save -o myfastapi.tar myfastapi
```

* 其他机器部署

  ```bash
  docker load < myfastapi.tar
  ```
