-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
90 lines (62 loc) · 2.44 KB
/
Dockerfile
File metadata and controls
90 lines (62 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# ================================
# 博客全栈 Dockerfile
# 包含:Astro 前端(SSR) + FastAPI 后端 + Vue 管理后台
# ================================
# === 阶段1:构建前端 ===
FROM node:20-alpine AS frontend-builder
WORKDIR /app/client
# 复制前端依赖文件
COPY client/package.json client/pnpm-lock.yaml ./
# 安装 pnpm 和依赖
RUN npm install -g pnpm && pnpm install --frozen-lockfile
# 复制前端源码并构建
COPY client/ ./
# 设置构建时的 API 地址
ENV API_URL=http://localhost:8000
RUN pnpm build
# === 阶段2:构建管理后台 ===
FROM node:20-alpine AS admin-builder
WORKDIR /app/admin
# 复制管理后台依赖文件
COPY server/admin/package.json server/admin/package-lock.json ./
# 安装依赖
RUN npm ci
# 复制管理后台源码并构建
COPY server/admin/ ./
RUN npm run build
# === 阶段3:最终运行镜像 ===
FROM node:20-alpine
WORKDIR /app
# 安装 Python 和必要的系统依赖
RUN apk add --no-cache python3 py3-pip
# 复制 Python 依赖文件并安装
COPY server/requirements.txt ./
RUN pip3 install --no-cache-dir --break-system-packages -r requirements.txt
# 复制后端源码
COPY server/app ./app
# 从构建阶段复制前端构建产物(SSR 模式)
COPY --from=frontend-builder /app/client/dist ./client/dist
COPY --from=frontend-builder /app/client/node_modules ./client/node_modules
COPY --from=frontend-builder /app/client/package.json ./client/package.json
# 从构建阶段复制管理后台构建产物
COPY --from=admin-builder /app/static/admin ./static/admin
# 复制前端静态资源(Banner 图片等)
COPY client/public/assets ./client/public/assets
COPY client/public/images ./client/public/images
# 复制前端内容文件(用于关于页面编辑)
COPY client/src/content ./client/src/content
# 创建数据目录(用于 SQLite 和上传文件)
RUN mkdir -p /app/data /app/uploads/photos/thumbnails
# 创建启动脚本(避免 Windows CRLF 问题)
RUN printf '#!/bin/sh\necho "Starting Astris Blog..."\npython3 -m uvicorn app.main:app --host 0.0.0.0 --port 8000 &\nsleep 2\ncd /app/client/dist/server && node entry.mjs\n' > /app/start.sh && chmod +x /app/start.sh
# 设置环境变量
ENV PYTHONPATH=/app
ENV DATABASE_URL=sqlite+aiosqlite:///./data/blog.db
ENV DEBUG=false
ENV NODE_ENV=production
ENV HOST=0.0.0.0
ENV PORT=4321
# 暴露端口(4321: Astro前端, 8000: FastAPI后端)
EXPOSE 4321 8000
# 启动命令
CMD ["/bin/sh", "/app/start.sh"]