快速部署命令

常用部署操作

# 完整部署(推送代码 + 服务器部署)
powershell -ExecutionPolicy Bypass -File .\deploy\scripts\deploy.ps1 -Action app
 
# 本地测试部署
powershell -ExecutionPolicy Bypass -File .\deploy\scripts\deploy.ps1 -Action local
 
# 查看日志
powershell -ExecutionPolicy Bypass -File .\deploy\scripts\deploy.ps1 -Action logs
 
# 查看状态
powershell -ExecutionPolicy Bypass -File .\deploy\scripts\deploy.ps1 -Action status
 
# 重启服务
powershell -ExecutionPolicy Bypass -File .\deploy\scripts\deploy.ps1 -Action restart
 
# 停止服务
powershell -ExecutionPolicy Bypass -File .\deploy\scripts\deploy.ps1 -Action stop

服务器直接操作

# SSH 登录
ssh root@$SERVER_IP
 
# 查看容器状态
docker ps | grep digital-garden
 
# 查看日志
docker compose logs -f digital-garden
 
# 重启容器
docker compose restart
 
# 停止并删除容器
docker compose down
 
# 重新构建并启动
docker compose up -d --build
 
# 查看资源使用
docker stats digital-garden

首次部署检查清单

1. 服务器 SSH 密钥(GitHub 访问)

# 生成密钥
ssh root@$SERVER_IP "ssh-keygen -t ed25519 -C 'server-name' -f ~/.ssh/id_ed25519 -N ''"
 
# 获取公钥
ssh root@$SERVER_IP "cat ~/.ssh/id_ed25519.pub"
 
# 添加到 GitHub: Settings > SSH and GPG keys
 
# 测试连接
ssh root@$SERVER_IP "ssh -T git@github.com"

2. Git Remote 配置

# 检查当前 URL
ssh root@$SERVER_IP "cd /root/digital-garden && git remote -v"
 
# 改为 SSH(如果是 HTTPS)
ssh root@$SERVER_IP "cd /root/digital-garden && git remote set-url origin git@github.com:username/repo.git"

3. Docker 镜像源(国内服务器)

# 配置镜像源
ssh root@$SERVER_IP 'cat > /etc/docker/daemon.json << EOF
{
  "registry-mirrors": [
    "https://dockerproxy.com",
    "https://docker.m.daocloud.io"
  ]
}
EOF'
 
# 重启 Docker
ssh root@$SERVER_IP "systemctl restart docker"
 
# 验证
ssh root@$SERVER_IP "docker info | grep -A 3 'Registry Mirrors'"

4. 本地配置文件

创建 deploy/server-config.txt:

ServerHost=$SERVER_IP
ServerUser=root
ServerPath=/root/digital-garden

故障排查速查表

问题:SSH 连接失败

# 测试连接
ssh root@$SERVER_IP
 
# 检查密钥
ssh -v root@121.4.26.74
 
# 添加密钥到 agent
ssh-add ~/.ssh/id_rsa

问题:Git Pull 失败

# 检查 GitHub SSH 连接
ssh root@$SERVER_IP "ssh -T git@github.com"
 
# 强制重置服务器代码
ssh root@$SERVER_IP "cd /root/digital-garden && git reset --hard origin/main && git clean -fd"
 
# 检查 remote URL
ssh root@$SERVER_IP "cd /root/digital-garden && git remote -v"

问题:Docker 构建失败

# 查看详细构建日志
ssh root@$SERVER_IP "cd /root/digital-garden && docker compose build --no-cache"
 
# 检查磁盘空间
ssh root@$SERVER_IP "df -h"
 
# 清理 Docker 缓存
ssh root@$SERVER_IP "docker system prune -a"

问题:容器无法启动

# 查看容器日志
docker compose logs digital-garden
 
# 查看所有容器
docker ps -a
 
# 进入容器调试
docker exec -it digital-garden sh

问题:端口被占用

# 查看端口占用
ssh root@$SERVER_IP "netstat -tulpn | grep 8080"
 
# 修改 docker-compose.yml 中的端口映射
ports:
  - "8081:80"  # 改为其他端口

重要命令备忘

Docker Compose v2 语法

# V1 (旧版,已废弃)
docker-compose up -d
 
# V2 (新版,使用这个)
docker compose up -d

PowerShell 中的 CRLF 处理

# 清理行结束符
$script = $script -replace "`r`n", "`n"
$script = $script -replace "`r", "`n"

Dockerfile Alpine 兼容性

# 安装 GNU coreutils 支持 env -S
RUN apk add --no-cache coreutils

网站访问地址

监控命令

实时监控容器

# 查看资源使用
docker stats
 
# 查看容器事件
docker events
 
# 查看实时日志
docker compose logs -f --tail=100

健康检查

# 检查容器健康状态
docker ps --format "table {{.Names}}\t{{.Status}}"
 
# 测试网站响应
curl -I http://$SERVER_IP:8080
 
# 检查 Nginx 配置
docker exec digital-garden nginx -t

回滚方案

快速回滚到上一版本

# 1. SSH 到服务器
ssh root@$SERVER_IP
 
# 2. 进入项目目录
cd /root/digital-garden
 
# 3. 查看提交历史
git log --oneline -n 10
 
# 4. 回滚到指定提交
git reset --hard <commit-hash>
 
# 5. 重新部署
docker compose down
docker compose up -d --build

紧急停止服务

# 本地执行
powershell -ExecutionPolicy Bypass -File .\deploy\scripts\deploy.ps1 -Action stop
 
# 或直接 SSH 执行
ssh root@$SERVER_IP "cd /root/digital-garden && docker compose down"

性能优化建议

Docker 构建优化

# 使用 .dockerignore 排除不必要的文件
.git
node_modules
.env
*.md
 
# 利用构建缓存
# 先复制 package.json,再复制其他文件
COPY package*.json ./
RUN npm ci
COPY . .

镜像体积优化

# 查看镜像大小
docker images | grep digital-garden
 
# 多阶段构建减小体积
# 在 Dockerfile 中只复制必要的文件到最终镜像

安全建议

  1. 不要在代码中存储敏感信息

    • 使用环境变量
    • 使用 .env 文件(不提交到 Git)
  2. 定期更新依赖

    npm audit
    npm audit fix
  3. 使用最小权限原则

    • 容器内使用非 root 用户
    • 限制容器资源使用
  4. 启用 HTTPS

    • 使用 Nginx 配置 SSL
    • 使用 Let’s Encrypt 免费证书

相关文档