添加docker相关

This commit is contained in:
Connor
2026-01-12 22:21:46 +08:00
parent 9d29a774cb
commit 92012699a2
5 changed files with 292 additions and 1 deletions

60
.dockerignore Normal file
View File

@@ -0,0 +1,60 @@
# Git
.git
.gitignore
.gitattributes
# IDE
.idea
.vscode
*.swp
*.swo
*~
# OS
.DS_Store
Thumbs.db
# 构建产物
dist/
build/
*.exe
*.dll
*.so
*.dylib
huobao-drama
backend
# 依赖
node_modules/
vendor/
# 数据文件
data/
*.db
*.log
# 临时文件
tmp/
temp/
# 前端构建缓存
web/.vite/
web/dist/
# 测试
*.test
coverage/
# 文档
README.md
drama.png
*.md
# Docker
Dockerfile*
docker-compose*.yml
.dockerignore
# CI/CD
.github/
.gitlab-ci.yml

4
.gitignore vendored
View File

@@ -59,3 +59,7 @@ web/.env.local
# Config file (use config.example.yaml as template)
configs/config.yaml
# Docker publish documentation (optional)
DOCKER_PUBLISH.md
build.sh

97
Dockerfile Normal file
View File

@@ -0,0 +1,97 @@
# 多阶段构建 Dockerfile for Huobao Drama
# ==================== 阶段1: 构建前端 ====================
FROM node:20-alpine AS frontend-builder
WORKDIR /app/web
# 复制前端依赖文件
COPY web/package*.json ./
# 安装前端依赖(包括 devDependencies构建需要
RUN npm ci
# 复制前端源码
COPY web/ ./
# 构建前端
RUN npm run build
# ==================== 阶段2: 构建后端 ====================
FROM golang:1.23-alpine AS backend-builder
# 安装必要的构建工具(包括 gcc、musl-dev 和 sqlite-dev 用于 CGO
RUN apk add --no-cache \
git \
ca-certificates \
tzdata \
gcc \
musl-dev \
sqlite-dev
WORKDIR /app
# 复制 Go 模块文件
COPY go.mod go.sum ./
# 下载依赖
RUN go mod download
# 复制后端源码
COPY . .
# 复制前端构建产物
COPY --from=frontend-builder /app/web/dist ./web/dist
# 构建后端可执行文件(启用 CGO 以支持 go-sqlite3
RUN CGO_ENABLED=1 go build -ldflags="-w -s" -o huobao-drama .
# ==================== 阶段3: 运行时镜像 ====================
FROM alpine:latest
# 安装运行时依赖
RUN apk add --no-cache \
ca-certificates \
tzdata \
ffmpeg \
sqlite-libs \
&& rm -rf /var/cache/apk/*
# 设置时区
ENV TZ=Asia/Shanghai
# 创建非 root 用户
RUN addgroup -g 1000 app && \
adduser -D -u 1000 -G app app
WORKDIR /app
# 从构建阶段复制可执行文件
COPY --from=backend-builder /app/huobao-drama .
# 复制前端构建产物
COPY --from=frontend-builder /app/web/dist ./web/dist
# 复制配置文件模板并创建默认配置
COPY configs/config.example.yaml ./configs/
RUN cp ./configs/config.example.yaml ./configs/config.yaml
# 复制数据库迁移文件
COPY migrations ./migrations/
# 创建数据目录
RUN mkdir -p /app/data/storage && \
chown -R app:app /app
# 切换到非 root 用户
USER app
# 暴露端口
EXPOSE 5678
# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:5678/health || exit 1
# 启动应用
CMD ["./huobao-drama"]

103
README.md
View File

@@ -339,7 +339,108 @@ go run main.go
## 📦 部署指南
### 🏭 生产环境部署
### 🐳 Docker 部署(推荐)
使用 Docker 部署是最简单快捷的方式,已内置默认配置,开箱即用。
#### 快速体验(推荐新手)
```bash
# 从 Docker Hub 拉取并运行
docker run -d \
--name huobao-drama \
-p 5678:5678 \
--restart unless-stopped \
huobao/huobao-drama:latest
```
访问: `http://localhost:5678` 即可开始使用!
#### 方式一:使用 Docker Compose
```bash
# 启动服务(使用内置配置)
docker-compose up -d
# 查看日志
docker-compose logs -f
# 停止服务
docker-compose down
```
**自定义配置**(可选):
```bash
# 1. 取消 docker-compose.yml 中配置文件挂载的注释
# 2. 复制并修改配置文件
cp configs/config.example.yaml configs/config.yaml
vim configs/config.yaml
# 3. 重启服务
docker-compose up -d
```
#### 方式二:使用 Docker 命令
**基础启动**(使用内置配置):
```bash
docker run -d \
--name huobao-drama \
-p 5678:5678 \
-v $(pwd)/data:/app/data \
--restart unless-stopped \
huobao/huobao-drama:latest
```
**自定义配置启动**
```bash
# 挂载自定义配置文件
docker run -d \
--name huobao-drama \
-p 5678:5678 \
-v $(pwd)/data:/app/data \
-v $(pwd)/configs/config.yaml:/app/configs/config.yaml:ro \
--restart unless-stopped \
huobao/huobao-drama:latest
```
**查看日志**
```bash
docker logs -f huobao-drama
```
#### 方式三:本地构建镜像
```bash
# 1. 构建镜像
docker build -t huobao-drama:latest .
# 2. 运行容器
docker run -d \
--name huobao-drama \
-p 5678:5678 \
-v $(pwd)/data:/app/data \
--restart unless-stopped \
huobao-drama:latest
```
#### 镜像仓库
**Docker Hub**(国际):
```bash
docker pull huobao/huobao-drama:latest
```
**Docker 部署优势:**
- ✅ 开箱即用,内置默认配置
- ✅ 环境一致性,避免依赖问题
- ✅ 一键启动,无需安装 Go、Node.js、FFmpeg
- ✅ 易于迁移和扩展
- ✅ 自动健康检查和重启
---
### 🏭 传统部署方式
#### 1. 编译构建

29
docker-compose.yml Normal file
View File

@@ -0,0 +1,29 @@
services:
huobao-drama:
image: huobao-drama:latest
container_name: huobao-drama
build:
context: .
dockerfile: Dockerfile
ports:
- "5678:5678"
volumes:
# 持久化数据目录
- ./data:/app/data
# 挂载配置文件(可选,如需自定义配置请取消注释)
# - ./configs/config.yaml:/app/configs/config.yaml:ro
environment:
- TZ=Asia/Shanghai
restart: unless-stopped
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:5678/health"]
interval: 30s
timeout: 3s
retries: 3
start_period: 10s
networks:
- huobao-network
networks:
huobao-network:
driver: bridge