Skip to content

Commit d5c37fa

Browse files
authored
Merge pull request #8 from JiayuXu0/codex/add-docker-entrypoint.sh-and-update-dockerfile
Update Docker build to use entrypoint script and pyproject installation
2 parents 73adea2 + e9ed399 commit d5c37fa

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

Dockerfile

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ RUN apt-get update && apt-get install -y \
2121
curl \
2222
&& rm -rf /var/lib/apt/lists/*
2323

24-
# 复制依赖文件
25-
COPY requirements.txt .
24+
# 复制依赖文件并安装 Python 依赖
25+
COPY pyproject.toml ./
26+
COPY README.md ./
27+
COPY src ./src
2628

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

3132
# 复制项目文件
3233
COPY . .
3334

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

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

5152
# 启动命令
52-
CMD ["/docker-entrypoint.sh"]
53+
CMD ["/scripts/docker-entrypoint.sh"]

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,18 @@ uv run uvicorn src:app --reload --host 0.0.0.0 --port 8000
267267
uv run uvicorn src:app --host 0.0.0.0 --port 8000 --workers 4
268268
```
269269

270+
### 🐳 使用 Docker 运行项目
271+
272+
```bash
273+
# 构建镜像(在项目根目录执行)
274+
docker build -t fastapi-template .
275+
276+
# 启动容器并映射端口,可选加载环境变量
277+
docker run --rm -p 8000:8000 --env-file .env fastapi-template
278+
```
279+
280+
镜像启动后即可访问 http://localhost:8000/docs 验证服务是否正常,或使用 `curl http://localhost:8000/api/v1/base/health` 进行健康检查。
281+
270282
### 5. 访问服务
271283

272284
- **🌐 官网文档**: http://fastapi.infyai.cn/

scripts/docker-entrypoint.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
set -e
3+
4+
exec uvicorn src:app --host 0.0.0.0 --port 8000

0 commit comments

Comments
 (0)