Skip to content

DreamTo/interface_test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI Docker 项目

这是一个使用FastAPI构建的简单API服务,支持Docker容器化部署。

🚀 快速开始

1. 构建镜像

docker build -t fastapi-service .

2. 运行容器

# 使用docker运行
docker run -d -p 8000:8000 --name fastapi-service fastapi-service

# 或者使用docker-compose(推荐)
docker compose up -d

3. 测试服务

# 健康检查
curl http://localhost:8000/health

# 测试公共接口
curl -X POST http://localhost:8000/api/public \
  -H 'Content-Type: application/json' \
  -d '{"message":"测试消息"}'

# 测试受保护接口(需要token)
curl -X POST http://localhost:8000/api/protected \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer your-token-here' \
  -d '{"message":"测试消息"}'

📋 接口说明

接口 方法 描述 认证
/health GET 健康检查 不需要
/api/public POST 公共接口 不需要
/api/protected POST 受保护接口 需要Bearer Token

🔧 Docker命令

# 构建镜像
docker build -t fastapi-service .

# 运行容器
docker run -d -p 8000:8000 --name fastapi-service fastapi-service

# 查看日志
docker logs fastapi-service

# 停止容器
docker stop fastapi-service

# 删除容器
docker rm fastapi-service

# 删除镜像
docker rmi fastapi-service

📊 容器信息

  • 基础镜像: python:3.11-slim
  • 工作目录: /app
  • 暴露端口: 8000
  • 健康检查: 每30秒检查一次 /health 端点
  • 重启策略: unless-stopped

🔍 查看容器状态

# 查看运行中的容器
docker ps

# 查看镜像
docker images

# 进入容器内部
docker exec -it fastapi-service /bin/bash

# 查看容器IP地址
docker inspect fastapi-service | grep IPAddress

🌐 访问API文档

容器运行后,可以通过以下地址访问交互式API文档:

About

测试接口的调用

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors