4.7 KiB
4.7 KiB
Registry 部署快速指南
快速开始(阿里云 ACR)
1. 准备工作(5 分钟)
1.1 创建阿里云账号
- 访问 https://www.aliyun.com
- 注册/登录账号
1.2 创建容器镜像服务实例
- 进入「容器镜像服务 ACR」:https://cr.console.aliyun.com
- 选择「个人版」(免费)
- 选择地域(建议选择与服务器相同的地域,如:华东1-杭州)
- 创建实例
1.3 创建命名空间和仓库
- 创建命名空间(如:
youyidian) - 创建镜像仓库(如:
3d-material) - 选择仓库类型:私有
1.4 获取登录凭证
- 在仓库页面,点击「访问凭证」
- 设置固定密码(或使用临时密码)
- 记录以下信息:
- 登录地址:
registry.cn-hangzhou.aliyuncs.com(根据地域不同) - 用户名:通常是阿里云账号
- 密码:刚才设置的密码
- 登录地址:
2. 配置部署脚本(2 分钟)
2.1 设置环境变量(推荐)
创建 .env.registry 文件(不提交到 Git):
# 阿里云容器镜像服务配置
ALIYUN_REGISTRY_HOST=registry.cn-hangzhou.aliyuncs.com
ALIYUN_REGISTRY_NAMESPACE=youyidian
ALIYUN_REGISTRY_IMAGE=3d-material
ALIYUN_REGISTRY_USER=your-username
ALIYUN_REGISTRY_PASSWORD=your-password
2.2 使用部署脚本
# 加载环境变量
Get-Content .env.registry | ForEach-Object {
if ($_ -match '^([^=]+)=(.*)$') {
$env:($matches[1]) = $matches[2]
}
}
# 执行部署
.\一键镜像部署.ps1 `
-Server 45.249.208.118 `
-Username root `
-Password "your-ssh-password" `
-RegistryHost $env:ALIYUN_REGISTRY_HOST `
-RegistryNamespace $env:ALIYUN_REGISTRY_NAMESPACE `
-RegistryImage $env:ALIYUN_REGISTRY_IMAGE `
-RegistryUser $env:ALIYUN_REGISTRY_USER `
-RegistryPassword $env:ALIYUN_REGISTRY_PASSWORD `
-Tag "v1.0.0"
3. 首次部署(3 分钟)
# 第一次部署需要构建和推送,耗时稍长
.\一键镜像部署.ps1 `
-Server 45.249.208.118 `
-Username root `
-Password "your-ssh-password" `
-RegistryHost "registry.cn-hangzhou.aliyuncs.com" `
-RegistryNamespace "youyidian" `
-RegistryImage "3d-material" `
-RegistryUser "your-username" `
-RegistryPassword "your-password" `
-Tag "latest"
4. 后续部署(1-2 分钟)
后续部署会更快,因为:
- 构建时使用缓存(如果依赖未变化)
- 只推送变更的层
- 服务器端只拉取变更的层
使用 Docker Hub(备选方案)
如果不想使用阿里云,可以使用 Docker Hub:
1. 注册 Docker Hub 账号
访问 https://hub.docker.com 注册
2. 创建仓库
在 Docker Hub 创建仓库:your-username/flnxi-material
3. 部署
.\一键镜像部署.ps1 `
-Server 45.249.208.118 `
-Username root `
-Password "your-ssh-password" `
-RegistryHost "docker.io" `
-RegistryNamespace "your-username" `
-RegistryImage "flnxi-material" `
-RegistryUser "your-username" `
-RegistryPassword "your-dockerhub-password" `
-Tag "latest"
性能对比
当前方案(tar 上传)
- 构建:3-5 分钟(本地)
- 打包:30 秒
- 上传:1-2 分钟(120MB)
- 加载:30 秒
- 总计:5-8 分钟
Registry 方案
- 构建:3-5 分钟(本地,使用缓存)
- 推送:30 秒-1 分钟(只推送变更层)
- 拉取:30 秒-1 分钟(服务器端,只拉取变更层)
- 总计:4-7 分钟(首次),2-3 分钟(后续)
优势
- ✅ 上传更快:只推送变更的层,不是整个镜像
- ✅ 部署更快:服务器端只拉取变更的层
- ✅ 版本管理:可以保留多个版本,方便回滚
- ✅ 安全性:私有仓库,代码不会泄露
- ✅ 可扩展:可以轻松部署到多台服务器
注意事项
- 首次部署:需要完整构建和推送,耗时稍长
- 网络要求:需要能访问 Registry(阿里云国内访问快)
- 认证安全:不要将密码提交到 Git,使用环境变量
- 版本管理:建议使用有意义的 tag(如:
v1.0.0、latest)
故障排查
问题 1:登录失败
Error: unauthorized: authentication required
解决:检查用户名和密码是否正确
问题 2:推送失败
Error: denied: requested access to the resource is denied
解决:检查命名空间和仓库名称是否正确,是否有推送权限
问题 3:服务器拉取失败
Error: pull access denied
解决:确保服务器已登录 Registry,使用正确的用户名和密码
下一步
部署成功后,可以:
- 设置自动部署(CI/CD)
- 配置多环境部署(开发/测试/生产)
- 使用版本标签管理不同版本