Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ RUN apt-get update && apt-get install -y \
curl \
&& rm -rf /var/lib/apt/lists/*

# 复制依赖文件
COPY requirements.txt .
# 复制依赖文件并安装 Python 依赖
COPY pyproject.toml ./
COPY README.md ./
COPY src ./src

# 安装 Python 依赖
RUN pip install --no-cache-dir --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple && \
pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install --no-cache-dir . -i https://pypi.tuna.tsinghua.edu.cn/simple

# 复制项目文件
COPY . .

# 复制并设置启动脚本权限
COPY scripts/docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
COPY scripts/docker-entrypoint.sh /scripts/docker-entrypoint.sh
RUN chmod +x /scripts/docker-entrypoint.sh

# 创建必要的目录
RUN mkdir -p /app/logs /app/static /app/migrations
Expand All @@ -49,4 +50,4 @@ HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8000/docs || exit 1

# 启动命令
CMD ["/docker-entrypoint.sh"]
CMD ["/scripts/docker-entrypoint.sh"]
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,18 @@ uv run uvicorn src:app --reload --host 0.0.0.0 --port 8000
uv run uvicorn src:app --host 0.0.0.0 --port 8000 --workers 4
```

### 🐳 使用 Docker 运行项目

```bash
# 构建镜像(在项目根目录执行)
docker build -t fastapi-template .

# 启动容器并映射端口,可选加载环境变量
docker run --rm -p 8000:8000 --env-file .env fastapi-template
```

镜像启动后即可访问 http://localhost:8000/docs 验证服务是否正常,或使用 `curl http://localhost:8000/api/v1/base/health` 进行健康检查。

### 5. 访问服务

- **🌐 官网文档**: http://fastapi.infyai.cn/
Expand Down
4 changes: 4 additions & 0 deletions scripts/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
set -e

exec uvicorn src:app --host 0.0.0.0 --port 8000