DevOps
阿里云 ECS 2C2G GoCode MVP Docker 部署指南
在一台 2 核 2 GB 的阿里云 ECS 上,用 Docker Compose 部署 GoCode site-astro(门户 + 知识库 + 搜索)并绑定 gocodestudio.cn 域名。
阿里云 ECS 2C2G GoCode MVP Docker 部署指南
文档目标
- 使用
site-astro 构建统一的门户 + 知识库站点。 - 在阿里云 ECS(2 vCPU / 2 GB RAM)上通过 Docker Compose 发布。
- 通过
gocodestudio.cn 和 /wiki 提供访问。 - 保留 Pagefind 搜索、RSS、Sitemap、robots.txt 和健康检查。
- Waline 评论不属于 MVP,上线后按需恢复。
整体方案
| 维度 | 说明 |
|---|
| 前端应用 | @gocode/site-astro(Astro 5 + Tailwind) |
| 内容来源 | gocode-context/wiki/**/* |
| 搜索 | Pagefind 在镜像构建阶段生成 dist/pagefind |
| 容器入口 | 仓库根目录 Dockerfile + docker-compose.yml |
| 运行服务 | 容器内 Nginx 监听 8080 |
| 域名与 HTTPS | 系统 Nginx 监听 80/443,反向代理到 127.0.0.1:8080 |
准备工作
- 在阿里云控制台为
gocodestudio.cn 添加 A 记录,指向 ECS 公网 IP。 - 安全组放行 22、80、443 端口。
- 确认服务器能拉取 Git 仓库。
- 确认发布命令在 monorepo 根目录执行,而不是
gocode-apps/site-astro/。
步骤 1:初始化 ECS
sudo apt update && sudo apt upgrade -y
sudo apt install -y git curl ufw ca-certificates
sudo timedatectl set-timezone Asia/Shanghai
if [ ! -f /swapfile ]; then
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
fi
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw --force enable
步骤 2:安装 Docker
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo tee /etc/apt/keyrings/docker.asc >/dev/null
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list >/dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo systemctl enable --now docker
步骤 3:拉取仓库
sudo mkdir -p /srv/gocode
sudo chown "$USER":"$USER" /srv/gocode
cd /srv/gocode
git clone https://github.com/YOUR_USERNAME/gocode-monorepo.git
cd gocode-monorepo
步骤 4:构建并启动容器
docker compose up -d --build
docker compose ps
默认端口是 8080。如果服务器上已有服务占用 8080,可以改为:
GOCODE_SITE_PORT=18080 docker compose up -d --build
步骤 5:验证容器
curl -I http://127.0.0.1:8080/
curl -I http://127.0.0.1:8080/wiki
curl -I http://127.0.0.1:8080/wiki/react-tutorial
curl -I http://127.0.0.1:8080/pagefind/pagefind.js
curl -I http://127.0.0.1:8080/sitemap-index.xml
curl -I http://127.0.0.1:8080/robots.txt
curl -I http://127.0.0.1:8080/healthz
步骤 6:配置系统 Nginx 和 HTTPS
sudo apt install -y nginx certbot python3-certbot-nginx
sudo tee /etc/nginx/sites-available/gocodestudio.cn >/dev/null <<'EOF'
server {
listen 80;
listen [::]:80;
server_name gocodestudio.cn www.gocodestudio.cn;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
EOF
sudo ln -sf /etc/nginx/sites-available/gocodestudio.cn /etc/nginx/sites-enabled/gocodestudio.cn
sudo nginx -t
sudo systemctl reload nginx
sudo certbot --nginx -d gocodestudio.cn -d www.gocodestudio.cn
sudo certbot renew --dry-run
步骤 7:上线验收
https://gocodestudio.cn/ 首页可访问。https://gocodestudio.cn/wiki 知识库可访问。- 至少 3 篇文章详情页可访问。
- 搜索框能返回结果,点击结果不出现 404。
/pagefind/pagefind.js、/sitemap-index.xml、/robots.txt、/healthz 均返回 200。
后续更新
cd /srv/gocode/gocode-monorepo
git pull
docker compose up -d --build
docker compose ps
回滚
cd /srv/gocode/gocode-monorepo
git log --oneline -5
git checkout <上一版已验证提交>
docker compose up -d --build
docker compose ps
常见问题
| 现象 | 处理办法 |
|---|
| 构建时报 wiki 内容不存在 | 确保 Docker 构建在 monorepo 根目录执行,且 gocode-context/wiki 已随仓库拉取。 |
| 2G 机器构建时 OOM | 确认 swap 已启用,必要时临时停止其他占内存进程。 |
| 搜索文件 404 | 重新执行 docker compose up -d --build,确认镜像构建阶段生成 Pagefind。 |
| 域名访问失败 | 检查 DNS、安全组、UFW、系统 Nginx 和容器健康状态。 |
| HTTPS 失败 | 确认 80 端口可访问,再执行 sudo certbot renew --dry-run。 |
关联文档