buildDockerImage_u24_docker.ps1 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # Cognio 远程构建 Docker 镜像脚本
  2. [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
  3. # ============================================================
  4. # 配置区
  5. # ============================================================
  6. # 远程服务器配置
  7. $remoteUser = "root"
  8. $remoteHost = "192.168.1.99"
  9. $remotePort = "22"
  10. # Git 仓库配置
  11. $repoUrl = "http://ds:39418/AI/Cognio"
  12. $repoDir = "Cognio"
  13. # Docker 镜像配置
  14. $imageName = "amwpfiqvy/cognio"
  15. # Docker 镜像加速源(随机选择一个,不带 https:// 前缀)
  16. $dockerMirrors = @(
  17. "docker.m.daocloud.io"
  18. "docker.1panel.live"
  19. "hub.rat.dev"
  20. )
  21. $dockerMirror = $dockerMirrors | Get-Random
  22. # 构建时代理配置(用于 poetry/pip 下载依赖)
  23. $buildProxy = "http://192.168.1.222:7890"
  24. # tmux 会话名
  25. $tmuxSession = "dbx"
  26. # ============================================================
  27. # 主程序
  28. # ============================================================
  29. Write-Host ""
  30. Write-Host "========================================" -ForegroundColor Cyan
  31. Write-Host " Cognio 远程构建 Docker 镜像" -ForegroundColor Cyan
  32. Write-Host "========================================" -ForegroundColor Cyan
  33. Write-Host ""
  34. # Docker 镜像标签:提示用户输入(默认包含 latest,-l 排除 latest)
  35. $imageTagInput = Read-Host "请输入额外的 Docker 镜像标签 (多个用空格分隔,-l=排除latest,留空则仅推送 latest)"
  36. $inputTags = @()
  37. $excludeLatest = $false
  38. if ($imageTagInput -ne "") {
  39. $inputTags = $imageTagInput -split '\s+' | Where-Object { $_ -ne "" }
  40. if ($inputTags -contains "-l") {
  41. $excludeLatest = $true
  42. $inputTags = $inputTags | Where-Object { $_ -ne "-l" }
  43. }
  44. }
  45. # 构建最终标签列表
  46. if ($excludeLatest) {
  47. $imageTags = $inputTags
  48. } else {
  49. $imageTags = @("latest") + $inputTags | Select-Object -Unique
  50. }
  51. # 构建 -t 参数列表
  52. $tagParams = ($imageTags | ForEach-Object { "-t ${imageName}:$_" }) -join " "
  53. Write-Host "镜像标签: $($imageTags -join ', ')" -ForegroundColor Yellow
  54. Write-Host "镜像加速: $dockerMirror" -ForegroundColor Yellow
  55. Write-Host ""
  56. # 支持命令行参数覆盖默认配置
  57. if ($args.Count -ge 1) { $remoteUser = $args[0] }
  58. if ($args.Count -ge 2) { $remoteHost = $args[1] }
  59. if ($args.Count -ge 3) { $remotePort = $args[2] }
  60. Write-Host "连接到: $remoteUser@$remoteHost`:$remotePort" -ForegroundColor Yellow
  61. Write-Host ""
  62. # ============================================================
  63. # 构建远程执行的 Shell 脚本
  64. # ============================================================
  65. # 生成远程脚本内容(写入临时文件执行,避免引号嵌套问题)
  66. $scriptContent = @"
  67. #!/bin/bash
  68. cd ~
  69. if [ -d "$repoDir" ]; then
  70. echo "目录已存在,还原本地修改并拉取最新代码..."
  71. cd $repoDir
  72. git checkout .
  73. git pull
  74. else
  75. echo "目录不存在,正在克隆仓库..."
  76. git clone $repoUrl
  77. cd $repoDir
  78. fi
  79. echo "修改 Dockerfile 使用镜像加速..."
  80. sed -i '1s|^FROM python|FROM \$\{DOCKER_MIRROR\}/library/python|' Dockerfile
  81. sed -i '1i ARG DOCKER_MIRROR=docker.io' Dockerfile
  82. echo "添加 apt 国内镜像源..."
  83. sed -i '/^FROM/a RUN sed -i "s|http://deb.debian.org|https://mirrors.aliyun.com|g" /etc/apt/sources.list.d/debian.sources 2>/dev/null || sed -i "s|http://deb.debian.org|https://mirrors.aliyun.com|g" /etc/apt/sources.list 2>/dev/null || true' Dockerfile
  84. echo "配置 pip 国内镜像源..."
  85. sed -i 's|pip install|pip install -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com|g' Dockerfile
  86. echo "配置 Poetry 国内镜像源(环境变量)..."
  87. sed -i '/^FROM/a ENV PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/ PIP_TRUSTED_HOST=mirrors.aliyun.com' Dockerfile
  88. echo "配置 pip 只使用预编译包(避免 triton 编译问题)..."
  89. sed -i '/^FROM/a ENV PIP_ONLY_BINARY=:all:' Dockerfile
  90. echo "设置代理环境变量(用于推送镜像)..."
  91. export HTTP_PROXY=$buildProxy
  92. export HTTPS_PROXY=$buildProxy
  93. echo "配置 Docker daemon 代理(用于 buildx 推送)..."
  94. sudo mkdir -p /etc/systemd/system/docker.service.d
  95. cat << PROXYEOF | sudo tee /etc/systemd/system/docker.service.d/proxy.conf > /dev/null
  96. [Service]
  97. Environment="HTTP_PROXY=$buildProxy"
  98. Environment="HTTPS_PROXY=$buildProxy"
  99. Environment="NO_PROXY=localhost,127.0.0.1,mirrors.aliyun.com"
  100. PROXYEOF
  101. sudo systemctl daemon-reload
  102. sudo systemctl restart docker
  103. sleep 3
  104. echo "临时禁用 IPv6(避免推送超时)..."
  105. sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
  106. sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1
  107. echo "创建带代理的 buildx builder..."
  108. docker buildx rm mybuilder 2>/dev/null || true
  109. docker buildx create --name mybuilder --driver docker-container \
  110. --driver-opt env.HTTP_PROXY=$buildProxy \
  111. --driver-opt env.HTTPS_PROXY=$buildProxy \
  112. --use
  113. docker buildx inspect mybuilder --bootstrap
  114. echo "验证 builder 状态..."
  115. docker buildx ls
  116. echo "正在构建并推送 Docker 镜像..."
  117. echo "使用镜像加速: $dockerMirror"
  118. echo "使用代理: $buildProxy"
  119. docker buildx build --builder mybuilder \
  120. --platform linux/amd64,linux/arm64 \
  121. --build-arg DOCKER_MIRROR=$dockerMirror \
  122. --build-arg HTTP_PROXY=$buildProxy \
  123. --build-arg HTTPS_PROXY=$buildProxy \
  124. $tagParams \
  125. --cache-from type=local,src=`$HOME/.buildx-cache \
  126. --cache-to type=local,dest=`$HOME/.buildx-cache,mode=max \
  127. --push \
  128. .
  129. echo "恢复 IPv6..."
  130. sudo sysctl -w net.ipv6.conf.all.disable_ipv6=0
  131. sudo sysctl -w net.ipv6.conf.default.disable_ipv6=0
  132. "@
  133. # 将脚本内容转为 base64,避免特殊字符问题
  134. $scriptBase64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($scriptContent))
  135. # tmux 命令:解码并执行脚本
  136. $remoteCmd = "tmux has-session -t $tmuxSession 2>/dev/null || tmux new-session -d -s $tmuxSession; " +
  137. "tmux send-keys -t $tmuxSession 'echo $scriptBase64 | base64 -d > /tmp/build_cognio.sh && bash /tmp/build_cognio.sh' Enter"
  138. # ============================================================
  139. # 执行远程命令
  140. # ============================================================
  141. Write-Host "正在发送命令到 tmux 会话: $tmuxSession" -ForegroundColor Yellow
  142. Write-Host ""
  143. & ssh -p $remotePort "$remoteUser@$remoteHost" $remoteCmd
  144. if ($LASTEXITCODE -eq 0) {
  145. Write-Host ""
  146. Write-Host "========================================" -ForegroundColor Green
  147. Write-Host " 命令已发送到 tmux 会话: $tmuxSession" -ForegroundColor Green
  148. Write-Host "========================================" -ForegroundColor Green
  149. Write-Host ""
  150. Write-Host "推送目标:" -ForegroundColor Yellow
  151. foreach ($tag in $imageTags) {
  152. Write-Host " - ${imageName}:${tag}" -ForegroundColor Cyan
  153. }
  154. Write-Host ""
  155. Write-Host "查看构建进度:" -ForegroundColor Yellow
  156. Write-Host " ssh -t -p $remotePort $remoteUser@$remoteHost `"tmux attach -t $tmuxSession`"" -ForegroundColor Cyan
  157. } else {
  158. Write-Host ""
  159. Write-Host "部署失败,请检查错误信息" -ForegroundColor Red
  160. }
  161. Write-Host ""
  162. pause