Files
AIRegulation-Deployment/scripts/00_install_docker_windows.ps1
2026-04-23 09:58:47 +08:00

106 lines
4.7 KiB
PowerShell
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ══════════════════════════════════════════════════
# 00_install_docker_windows.ps1
# Windows 11 安装 Docker Desktop + WSL2 配置
# 用法:以管理员身份运行 PowerShell执行
# .\scripts\00_install_docker_windows.ps1
# ══════════════════════════════════════════════════
#Requires -RunAsAdministrator
$ErrorActionPreference = "Stop"
function Write-Info { Write-Host "[INFO] $args" -ForegroundColor Cyan }
function Write-Ok { Write-Host "[OK] $args" -ForegroundColor Green }
function Write-Warn { Write-Host "[WARN] $args" -ForegroundColor Yellow }
function Write-Err { Write-Host "[ERR] $args" -ForegroundColor Red; exit 1 }
Write-Info "============================================"
Write-Info "AI合规智能中枢 — Windows Docker 环境安装"
Write-Info "============================================"
# ── Step 1启用 WSL2 ──────────────────────────
Write-Info "Step 1/4检查并启用 WSL2..."
$wslFeature = Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
$vmFeature = Get-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
if ($wslFeature.State -ne "Enabled") {
Write-Info "启用 WSL 功能..."
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux -NoRestart
}
if ($vmFeature.State -ne "Enabled") {
Write-Info "启用虚拟机平台..."
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -NoRestart
}
# 更新 WSL 内核
Write-Info "更新 WSL2 内核..."
wsl --update
wsl --set-default-version 2
Write-Ok "WSL2 配置完成"
# ── Step 2安装 Ubuntu WSL 发行版 ─────────────
Write-Info "Step 2/4检查 Ubuntu WSL..."
$wslList = wsl --list --quiet 2>$null
if ($wslList -notmatch "Ubuntu") {
Write-Info "安装 Ubuntu 22.04..."
wsl --install -d Ubuntu-22.04
Write-Ok "Ubuntu 22.04 安装完成(首次运行需要设置用户名和密码)"
} else {
Write-Ok "Ubuntu WSL 已安装"
wsl --list --verbose
}
# ── Step 3安装 Docker Desktop ────────────────
Write-Info "Step 3/4检查 Docker Desktop..."
$dockerCmd = Get-Command docker -ErrorAction SilentlyContinue
if ($dockerCmd) {
Write-Ok "Docker 已安装:$(docker --version)"
} else {
# 尝试用 winget 安装
$winget = Get-Command winget -ErrorAction SilentlyContinue
if ($winget) {
Write-Info "通过 winget 安装 Docker Desktop..."
winget install -e --id Docker.DockerDesktop --accept-package-agreements --accept-source-agreements
Write-Ok "Docker Desktop 安装完成"
} else {
Write-Warn "未找到 winget请手动安装 Docker Desktop"
Write-Warn "下载地址https://www.docker.com/products/docker-desktop/"
Write-Warn "安装时勾选Use WSL 2 instead of Hyper-V"
Start-Process "https://www.docker.com/products/docker-desktop/"
Read-Host "安装完成后按 Enter 继续"
}
}
# ── Step 4配置 Docker Desktop WSL 集成 ───────
Write-Info "Step 4/4提示 Docker Desktop 配置..."
Write-Warn ""
Write-Warn "请确认 Docker Desktop 已进行以下配置:"
Write-Warn " 1. Settings → General → 勾选 'Use WSL 2 based engine'"
Write-Warn " 2. Settings → Resources → WSL Integration → 开启 Ubuntu-22.04"
Write-Warn " 3. 如有 NVIDIA GPU"
Write-Warn " Settings → General → 勾选 'Use GPU with WSL 2'"
Write-Warn ""
# ── 验证 ───────────────────────────────────────
Write-Info "验证安装..."
try {
$dockerVer = docker --version
$composeVer = docker compose version
Write-Ok "Docker: $dockerVer"
Write-Ok "Compose: $composeVer"
} catch {
Write-Warn "Docker 命令不可用,可能需要重启后再验证"
Write-Warn "重启后运行docker run hello-world"
}
Write-Host ""
Write-Host "============================================" -ForegroundColor Green
Write-Host " 安装完成!" -ForegroundColor Green
Write-Host "============================================" -ForegroundColor Green
Write-Host ""
Write-Host "后续步骤(在 WSL2 Ubuntu 中执行):" -ForegroundColor Yellow
Write-Host " 1. 打开 Ubuntu WSL 终端"
Write-Host " 2. cd /mnt/c/Projects/AIProjects/AIRegulations/Depolyment"
Write-Host " 3. bash scripts/01_setup_project.sh"
Write-Host ""
Write-Host "如需重启系统请现在重启,然后继续操作。" -ForegroundColor Yellow