本文档介绍如何使用 Docker 构建、运行和部署 TodoIng 的 Golang 后端服务。
axiu/todoing-golinux/amd64, linux/arm64| 标签 | 描述 | 用途 |
|---|---|---|
latest | 最新稳定版本 | 生产环境推荐 |
v1.0.0 | 具体版本号 | 生产环境固定版本 |
dev | 开发分支版本 | 开发测试环境 |
YYYYMMDD-{sha} | 每日构建版本 | CI/CD 环境 |
# 启动 Golang 后端服务
docker run -d \
--name todoing-go-backend \
-p 5004:5004 \
-e MONGO_URI="mongodb://admin:password@localhost:27017/todoing?authSource=admin" \
-e JWT_SECRET="your_super_secret_jwt_key_change_in_production" \
axiu/todoing-go:latest
# 查看日志
docker logs -f todoing-go-backend
# 健康检查
curl http://localhost:5004/health
# 进入项目 docker 目录
cd /path/to/todoIng/docker
# 一键启动完整服务 (包含数据库)
./deploy.sh golang up
# 访问服务
echo "API: http://localhost:5004/api"
echo "Swagger: http://localhost:5004/swagger/"
# 进入 Golang 后端目录
cd backend-go
# 构建镜像
docker build -t todoing-go:local .
# 运行本地构建的镜像
docker run -d -p 5004:5004 \
-e MONGO_URI="mongodb://localhost:27017/todoing" \
-e JWT_SECRET="your_jwt_secret" \
todoing-go:local
# 在 docker 目录中本地构建
cd docker
# 设置环境变量使用本地构建
export GOLANG_BACKEND_IMAGE=""
# 或使用 --build 选项强制构建
./deploy.sh golang up --build
# 进入 Golang 后端目录
cd backend-go
# 使用 Make 构建 Docker 镜像
make docker-build
# 运行构建的镜像
make docker-run
# 查看所有 Docker 相关命令
make help | grep docker
# 数据库连接
MONGO_URI=mongodb://admin:password@localhost:27017/todoing?authSource=admin
# JWT 密钥 (生产环境必须修改)
JWT_SECRET=your_super_secret_jwt_key_change_in_production_min_32_chars
# 服务配置
PORT=5004 # 服务端口
GIN_MODE=release # Gin 模式 (debug/release)
LOG_LEVEL=info # 日志级别 (debug/info/warn/error)
# JWT 配置
JWT_EXPIRY=24h # JWT 过期时间
# Redis 配置 (可选)
REDIS_URL=redis://:password@localhost:6379
# 功能开关
DISABLE_REGISTRATION=false # 禁用注册功能
ENABLE_CAPTCHA=true # 启用验证码
ENABLE_EMAIL_VERIFICATION=false # 启用邮件验证
# 默认管理员账户
DEFAULT_USERNAME=admin
[email protected]
DEFAULT_PASSWORD=admin123
# 邮件配置 (如果启用邮件功能)
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
[email protected]
EMAIL_PASS=your-email-app-password
# CORS 配置
CORS_ORIGINS=http://localhost:3000,http://localhost:5173
# 监控配置
ENABLE_METRICS=true # 启用 Prometheus 指标
METRICS_PORT=9090 # 指标端口
# 阶段 1: 构建阶段
FROM golang:1.23-alpine AS builder
# - 安装构建依赖
# - 下载 Go 模块
# - 编译 Go 应用
# 阶段 2: 运行阶段
FROM alpine:3.18
# - 最小化运行环境
# - 非 root 用户
# - 安全配置
# 基础健康检查
curl http://localhost:5004/health
# 响应: {"status":"ok","timestamp":"2025-08-10T12:00:00Z"}
# 详细健康检查 (包含依赖检查)
curl http://localhost:5004/health/detailed
# 响应: {"status":"ok","dependencies":{"mongodb":"ok","redis":"ok"}}
# 就绪状态检查
curl http://localhost:5004/ready
# 响应: {"ready":true}
# 访问监控指标 (如果启用)
curl http://localhost:9090/metrics
# 常用指标
# - todoing_http_requests_total: HTTP 请求总数
# - todoing_http_request_duration_seconds: 请求耗时
# - todoing_database_connections: 数据库连接数
# - todoing_active_users: 活跃用户数
# 查看容器资源使用情况
docker stats todoing-go-backend
# 查看容器日志
docker logs -f todoing-go-backend
# 查看容器进程
docker top todoing-go-backend
# 进入容器调试
docker exec -it todoing-go-backend sh
# 查看启动日志
docker logs todoing-go-backend
# 检查环境变量
docker exec todoing-go-backend env | grep -E "(MONGO|JWT|PORT)"
# 检查端口占用
lsof -i :5004
# 测试数据库连接
docker exec todoing-go-backend sh -c "echo 'db.runCommand(\"ping\")' | mongosh $MONGO_URI"
# 检查网络连接
docker network ls
docker network inspect [network_name]
# 查看资源使用
docker stats todoing-go-backend
# 设置资源限制
docker run -d --name todoing-go-backend \
--memory=512m \
--cpus=1.0 \
-p 5004:5004 \
axiu/todoing-go:latest
# 启用调试模式
docker run -d \
-e GIN_MODE=debug \
-e LOG_LEVEL=debug \
-p 5004:5004 \
axiu/todoing-go:latest
# 启用开发模式 (包含调试端口)
docker run -d \
-p 5004:5004 \
-p 2345:2345 \
-e DEBUG=true \
axiu/todoing-go:dev
# 1. 使用固定版本标签
docker pull axiu/todoing-go:v1.0.0
# 2. 设置资源限制
docker run -d --name todoing-go-prod \
--memory=1g \
--cpus=2.0 \
--restart=unless-stopped \
-p 5004:5004 \
axiu/todoing-go:v1.0.0
# 3. 使用 Docker Compose 生产配置
cd docker
./deploy.sh prod up --profile replica
# 1. 非 root 用户运行 (镜像已配置)
docker run --user 1000:1000 axiu/todoing-go:latest
# 2. 只读文件系统
docker run --read-only \
--tmpfs /tmp \
--tmpfs /var/log \
axiu/todoing-go:latest
# 3. 限制网络访问
docker run --network custom-network axiu/todoing-go:latest
# 1. 使用 Docker Swarm
docker service create \
--name todoing-go-service \
--replicas 3 \
--publish 5004:5004 \
axiu/todoing-go:latest
# 2. 使用 Kubernetes
kubectl apply -f k8s/todoing-go-deployment.yaml
# 3. 使用负载均衡器
docker run -d --name nginx-lb \
-p 80:80 \
-v ./nginx.conf:/etc/nginx/nginx.conf \
nginx:alpine
# 1. 调整 Go 运行时参数
docker run -d \
-e GOGC=100 \
-e GOMAXPROCS=2 \
-e GOMEMLIMIT=1GiB \
axiu/todoing-go:latest
# 2. 优化垃圾回收
docker run -d \
-e GOGC=50 \
-e GODEBUG=madvdontneed=1 \
axiu/todoing-go:latest
# 设置数据库连接池参数
docker run -d \
-e MONGO_MAX_POOL_SIZE=100 \
-e MONGO_MIN_POOL_SIZE=10 \
-e MONGO_MAX_IDLE_TIME=300s \
axiu/todoing-go:latest
# 在 .github/workflows/docker-publish.yml 中
- name: Build and push Golang backend
uses: docker/build-push-action@v5
with:
context: ./backend-go
platforms: linux/amd64,linux/arm64
push: true
tags: axiu/todoing-go:latest,axiu/todoing-go:${{ github.ref_name }}
# 运行集成测试
docker run --rm \
-e TEST_MODE=true \
-e MONGO_URI="mongodb://mongo-test:27017/test" \
axiu/todoing-go:latest \
go test ./...
# 使用测试专用镜像
docker build --target test -t todoing-go-test .
docker run --rm todoing-go-test
git checkout -b feature/docker-improvementmake docker-build && make docker-test欢迎改进本文档,包括但不限于:
最后更新: 2025-08-10
Content type
Image
Digest
sha256:707e85b1a…
Size
17.8 MB
Last updated
about 1 year ago
docker pull axiu/todoing-go