gongxiangp2/install-docker.ps1

191 lines
7.1 KiB
PowerShell
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.

# Docker Desktop 安装前检查脚本
# 检查系统是否满足 Docker Desktop 安装要求
param(
[switch]$InstallWSL,
[switch]$Help
)
function Write-Success {
param([string]$Message)
Write-Host "✅ $Message" -ForegroundColor Green
}
function Write-Error-Custom {
param([string]$Message)
Write-Host "❌ $Message" -ForegroundColor Red
}
function Write-Info {
param([string]$Message)
Write-Host "ℹ️ $Message" -ForegroundColor Cyan
}
function Write-Warning-Custom {
param([string]$Message)
Write-Host "⚠️ $Message" -ForegroundColor Yellow
}
function Show-Help {
Write-Host @"
Docker Desktop 安装前检查脚本
用法:
.\install-docker.ps1 # 检查系统要求
.\install-docker.ps1 -InstallWSL # 检查并安装 WSL 2(需要管理员权限)
功能:
- 检查 Windows 版本
- 检查 WSL 2 状态
- 检查虚拟化支持
- 提供安装指导
"@
}
function Main {
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Docker Desktop 安装前检查" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
if ($Help) {
Show-Help
return
}
$allChecksPassed = $true
# 检查 1:Windows 版本
Write-Host "📋 检查 1:Windows 版本" -ForegroundColor Magenta
$osVersion = [System.Environment]::OSVersion.Version
$osInfo = Get-CimInstance Win32_OperatingSystem
Write-Info "操作系统:$($osInfo.Caption)"
Write-Info "版本:$($osVersion.Major).$($osVersion.Minor) Build $($osInfo.BuildNumber)"
if ($osVersion.Major -ge 10 -and $osVersion.Build -ge 19041) {
Write-Success "Windows 版本满足要求(Windows 10 2004 或更高)"
} else {
Write-Error-Custom "Windows 版本可能不满足要求(需要 Windows 10 2004 或更高)"
$allChecksPassed = $false
}
Write-Host ""
# 检查 2:WSL 状态
Write-Host "📋 检查 2:WSL 状态" -ForegroundColor Magenta
try {
$wslStatus = wsl --status 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Info "WSL 输出:"
Write-Host $wslStatus
# 检查 WSL 版本
$wslList = wsl --list --verbose 2>&1
if ($wslList -match "2.0") {
Write-Success "WSL 2 已安装"
} else {
Write-Warning-Custom "WSL 已安装,但可能不是 WSL 2"
Write-Info "建议运行:wsl --set-default-version 2"
}
} else {
Write-Warning-Custom "WSL 未安装或未正确配置"
Write-Info "需要安装 WSL 2 才能使用 Docker Desktop"
if ($InstallWSL) {
Write-Info "正在安装 WSL 2..."
# 检查是否有管理员权限
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-Error-Custom "安装 WSL 需要管理员权限"
Write-Info "请以管理员身份运行 PowerShell,然后执行:"
Write-Host " wsl --install" -ForegroundColor Yellow
Write-Host " wsl --set-default-version 2" -ForegroundColor Yellow
$allChecksPassed = $false
} else {
Write-Info "以管理员身份安装 WSL..."
wsl --install
if ($LASTEXITCODE -eq 0) {
Write-Success "WSL 安装命令已执行,请重启计算机后继续"
Write-Info "重启后运行:wsl --set-default-version 2"
} else {
Write-Error-Custom "WSL 安装失败"
$allChecksPassed = $false
}
}
} else {
Write-Info "要安装 WSL 2,请以管理员身份运行:"
Write-Host " wsl --install" -ForegroundColor Yellow
Write-Host " 或运行此脚本:.\install-docker.ps1 -InstallWSL" -ForegroundColor Yellow
$allChecksPassed = $false
}
}
} catch {
Write-Warning-Custom "无法检查 WSL 状态:$_"
$allChecksPassed = $false
}
Write-Host ""
# 检查 3:虚拟化支持
Write-Host "📋 检查 3:虚拟化支持" -ForegroundColor Magenta
try {
$hyperV = Get-CimInstance -ClassName Win32_ComputerSystem -Property HypervisorPresent
if ($hyperV.HypervisorPresent) {
Write-Success "检测到虚拟化支持(Hyper-V)"
} else {
Write-Warning-Custom "未检测到虚拟化支持"
Write-Info "请确保在 BIOS/UEFI 中启用了虚拟化功能(VT-x 或 AMD-V)"
}
} catch {
Write-Warning-Custom "无法检查虚拟化状态"
}
Write-Host ""
# 检查 4:内存
Write-Host "📋 检查 4:系统内存" -ForegroundColor Magenta
try {
$memory = Get-CimInstance Win32_ComputerSystem
$totalMemoryGB = [Math]::Round($memory.TotalPhysicalMemory / 1GB, 2)
Write-Info "总内存:$totalMemoryGB GB"
if ($totalMemoryGB -ge 8) {
Write-Success "内存充足(推荐 8GB 或更高)"
} elseif ($totalMemoryGB -ge 4) {
Write-Warning-Custom "内存可能不足(推荐 8GB,当前 $totalMemoryGB GB)"
} else {
Write-Error-Custom "内存不足(需要至少 4GB,当前 $totalMemoryGB GB)"
$allChecksPassed = $false
}
} catch {
Write-Warning-Custom "无法检查内存信息"
}
Write-Host ""
# 总结
Write-Host "========================================" -ForegroundColor Cyan
if ($allChecksPassed) {
Write-Success "系统检查通过!可以安装 Docker Desktop"
Write-Host ""
Write-Info "下一步:"
Write-Host " 1. 下载 Docker Desktop:" -ForegroundColor Yellow
Write-Host " https://www.docker.com/products/docker-desktop/" -ForegroundColor Cyan
Write-Host " 2. 运行安装程序并按照提示完成安装" -ForegroundColor Yellow
Write-Host " 3. 安装完成后重启计算机" -ForegroundColor Yellow
Write-Host " 4. 启动 Docker Desktop 并验证安装" -ForegroundColor Yellow
} else {
Write-Warning-Custom "系统检查未完全通过,请先解决上述问题"
Write-Host ""
Write-Info "常见问题解决:"
Write-Host " - WSL 未安装:以管理员身份运行 'wsl --install'" -ForegroundColor Yellow
Write-Host " - 虚拟化未启用:在 BIOS/UEFI 中启用虚拟化功能" -ForegroundColor Yellow
Write-Host " - 内存不足:关闭其他程序或增加内存" -ForegroundColor Yellow
}
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Info "详细安装指南请查看:Docker安装指南.md"
}
# 执行主函数
Main