Files
blog-press/docs/Web/FastAPI/FastAPI-Docker.md
2026-05-20 11:26:38 +08:00

33 lines
1.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: FastAPI Docker离线部署
date: 2026-01-06
---
# 一、安装依赖
  通过pip将requirements.txt中的依赖包下载到本地
```sh
pip download -r requirements.txt -d ./packages --only-binary=:all: --platform manylinux2014_x86_64 -i https://pypi.tuna.tsinghua.edu.cn/simple
```
::: tip
`--only-binary=:all:`表示只下载预编译的二进制包
`--platform manylinux2014_x86_64`指定目标平台为 Linux
`-i`指定pip镜像源源
还可以加上`--python-version 310`指定python版本
:::
  执行命令后,会在./packages中下载相应的二进制包。
# 二、Dockfile
```dockerfile
FROM python:3.10-slim
WORKDIR /app
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' > /etc/timezone
COPY ./packages /app/packages
COPY requirements.txt /app/
RUN pip install --no-cache-dir --no-index --find-links=/app/packages -r requirements.txt
COPY . /app/
EXPOSE 8095
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8095"]
```