Skip to content

Commit af70022

Browse files
committed
perf(docker): 优化Windows下Docker构建性能
通过为hugo-build服务添加匿名卷挂载/resources和/public目录,避免跨OS文件系统同步大量小文件导致的性能问题。构建时间从超过60秒降至13秒内。
1 parent f0b0f14 commit af70022

8 files changed

Lines changed: 91 additions & 2 deletions

File tree

Makefile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: dev build clean stop optimize-images analyze-performance analyze-content analyze-content-ai full-build full-build-ai validate-architecture generate-covers generate-ai-covers test-covers generate-covers-for-directory ingest-data help
1+
.PHONY: dev build clean stop optimize-images analyze-performance build-measure analyze-content analyze-content-ai full-build full-build-ai validate-architecture generate-covers generate-ai-covers test-covers generate-covers-for-directory ingest-data help
22

33
# Shell 设置
44
# 让每个配方(target)的所有命令在同一个 shell 中执行,确保 .env 中的导出变量可在后续命令中生效
@@ -28,6 +28,15 @@ build:
2828
@echo "🔨 执行Hugo生产构建..."
2929
docker-compose run --rm hugo-build
3030

31+
# 测量构建时间
32+
build-measure:
33+
@echo "⏱️ 测量Hugo构建时间..."
34+
@start_time=$$(date +%s); \
35+
$(MAKE) build; \
36+
end_time=$$(date +%s); \
37+
duration=$$((end_time - start_time)); \
38+
echo "✅ 构建完成,耗时: $${duration} 秒"
39+
3140
# 优化图片
3241
optimize-images:
3342
@echo "🖼️ 优化图片资源..."
@@ -288,6 +297,7 @@ help:
288297
@echo ""
289298
@echo "Build Commands:"
290299
@echo " make build Execute production build"
300+
@echo " make build-measure Measure build time"
291301
@echo " make full-build Full build process (validate + optimize + analyze)"
292302
@echo " make full-build-ai 🤖 AI-enhanced full build process"
293303
@echo " make clean Clean build files"
@@ -348,3 +358,4 @@ CF_VECTOR_INDEX ?= blog-index
348358
vectorize-create-category-index:
349359
@echo "Creating filterable metadata index 'category' on $(CF_VECTOR_INDEX)"
350360
@npx wrangler vectorize create-metadata-index $(CF_VECTOR_INDEX) --property-name=category --type=string
361+

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,24 @@ docker build -t ai-blog .
107107
docker run -p 1313:1313 ai-blog
108108
```
109109

110+
### 使用 Make 运行与构建(推荐)
111+
112+
为确保一致的开发体验并触发必要的验证钩子,建议通过 `make` 管理 Hugo:
113+
114+
```bash
115+
# 启动开发服务器(如已在运行,先执行 make stop)
116+
make dev
117+
118+
# 停止已启动的 Hugo 服务
119+
make stop
120+
121+
# 生产构建(用于部署,包含最完整输出)
122+
make build
123+
124+
# 查看所有可用命令
125+
make help
126+
```
127+
110128
## 📁 项目结构
111129

112130
```text
@@ -335,3 +353,4 @@ This project is licensed under the [MIT License](LICENSE), free to use and distr
335353
**Content Categories**: Paper Reviews, Technical Analysis, Open Source Projects, Industry News, Product Reviews, Celebrity Interviews
336354

337355
⭐ If this project helps you, please give it a Star for support!
356+

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ services:
1919
image: hugomods/hugo:exts-0.152.2
2020
volumes:
2121
- .:/src
22+
- /src/resources
23+
- /src/public
2224
working_dir: /src
2325
command: build --minify
2426
profiles:
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Docker on Windows 构建性能优化经验 (2025-12-07)
2+
3+
## 问题描述
4+
5+
在 Windows 11 + WSL2/Git Bash 环境下,执行 `make build` (基于 Docker) 极慢,经常超时 (>60s)。
6+
而同样的 `hugo --minify` 命令在 GitHub Actions Linux 环境下只需几秒钟。
7+
8+
## 根本原因
9+
10+
经过排查,瓶颈在于 **Docker 的文件系统挂载(Bind Mount)机制**
11+
12+
1. **原始配置**
13+
`docker-compose.yml` 中的 `hugo-build` 服务仅挂载了根目录 `.:/src`
14+
```yaml
15+
services:
16+
hugo-build:
17+
volumes:
18+
- .:/src
19+
```
20+
21+
2. **性能杀手**:
22+
Hugo 构建时会生成大量静态文件(`/public` 目录,包含数千个文件)和资源文件(`/resources`)。
23+
由于 `.:/src` 是一个绑定挂载(Bind Mount),Docker 容器内的每一次写操作都需要同步回 Windows 宿主机的文件系统。
24+
在跨操作系统的文件系统(Windows NTFS <-> Linux Container)之间同步大量小文件(I/O 密集型操作)会导致极大的性能开销。
25+
26+
## 解决方案
27+
28+
使用 **匿名卷(Anonymous Volumes)** 来隔离高频写入目录,避免回写到宿主机(如果不需要在宿主机上保留这些构建产物,或者只需要最终结果)。
29+
30+
在 `docker-compose.yml` 中,为 `hugo-build` 服务添加对 `/src/resources` 和 `/src/public` 的挂载:
31+
32+
```yaml
33+
services:
34+
hugo-build:
35+
volumes:
36+
- .:/src
37+
- /src/resources # 匿名卷,不回写宿主机
38+
- /src/public # 匿名卷,不回写宿主机
39+
```
40+
41+
## 结果对比
42+
43+
* **优化前**:构建耗时 > 60s(甚至触发超时),系统卡顿。
44+
* **优化后**:构建耗时 < 13s。
45+
46+
## 经验教训
47+
48+
1. **I/O 敏感性**:Docker on Windows (WSL2 backend) 对跨 OS 的文件系统绑定挂载非常敏感,尤其是涉及大量小文件的读写。
49+
2. **构建隔离**:对于中间产物(如 `resources`)或非必须持久化到开发机源码目录的产物(如 `public`,如果是为了部署通常在 CI/CD 中生成),应尽量使用 Docker 内部卷或匿名卷。
50+
3. **开发 vs 生产**:开发服务 (`hugo server`) 通常使用内存缓存,对 I/O 不那么敏感,但在 Windows 上也建议对 `resources` 和 `public` 使用匿名卷以提升性能。
51+
52+
## 相关命令
53+
54+
* `make build`: 生产构建(已优化)。
55+
* `make build-measure`: 测量当前构建耗时。

docs/issues/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
这里记录开发过程中遇到的一些疑难杂症,和解决方案,以便于日后学习。

scripts/validate-architecture.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,4 @@ else
158158
echo " Read ARCHITECTURE.md for guidelines."
159159
exit 1
160160
fi
161+
54.7 KB
Loading

tools/performance-monitor/reports/performance-report.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Hugo博客性能分析报告
22

3-
生成时间: 2025-10-31 23:35:02
3+
生成时间: 2025-12-07 15:47:58
44

55
## 📊 总体统计
66

0 commit comments

Comments
 (0)