Skip to content

Commit dab8867

Browse files
committed
fix(pwsh): 改进 Docker 镜像清理脚本的错误处理和输出格式
在镜像删除失败时,若因被停止容器引用,则提示用户使用 -Force 或先清理停止容器。 统一管道中 Select-Object 的缩进格式以提升代码可读性。
1 parent c011d62 commit dab8867

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

scripts/pwsh/devops/Clean-DockerImages.ps1

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ function Invoke-DockerAction {
195195
if ($AllowFailure) {
196196
Write-Warning ("命令执行失败(已忽略): " + $commandText)
197197
Write-Warning $_.Exception.Message
198+
if ($Action -eq '删除 Docker 镜像' -and -not $Force -and $_.Exception.Message -match 'must be forced|being used by stopped container') {
199+
Write-Warning '检测到镜像被已停止容器引用。可使用 -Force 重试,或先执行 docker container prune -f 清理停止容器。'
200+
}
198201
return $false
199202
}
200203

@@ -370,11 +373,11 @@ function Get-DockerImages {
370373

371374
if (-not $imageMap.ContainsKey($normalizedId)) {
372375
$imageMap[$normalizedId] = [PSCustomObject]@{
373-
Id = $normalizedId
374-
References = New-Object 'System.Collections.Generic.List[object]'
375-
IsDangling = $false
376-
CreatedUtc = $null
377-
SizeBytes = 0L
376+
Id = $normalizedId
377+
References = New-Object 'System.Collections.Generic.List[object]'
378+
IsDangling = $false
379+
CreatedUtc = $null
380+
SizeBytes = 0L
378381
}
379382
}
380383

@@ -416,7 +419,7 @@ function Get-RunningImageSet {
416419
$running = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
417420
$containerIds = @(
418421
@(Invoke-DockerRead -Arguments @('ps', '-q', '--no-trunc') -AllowFailure) |
419-
Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
422+
Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
420423
)
421424

422425
foreach ($containerId in $containerIds) {
@@ -555,13 +558,13 @@ if ($candidates.Count -gt 0) {
555558
Write-Output ''
556559
Write-Output '候选镜像预览(前 20 条):'
557560
$preview = $candidates |
558-
Select-Object -First 20 |
559-
Select-Object `
560-
@{ Name = 'ImageId'; Expression = { $_.Id.Substring(0, [Math]::Min(12, $_.Id.Length)) } }, `
561+
Select-Object -First 20 |
562+
Select-Object `
563+
@{ Name = 'ImageId'; Expression = { $_.Id.Substring(0, [Math]::Min(12, $_.Id.Length)) } }, `
561564
Reason, `
562-
@{ Name = 'AgeHours'; Expression = { if ($null -eq $_.AgeHours) { '-' } else { $_.AgeHours } } }, `
563-
@{ Name = 'Size'; Expression = { Format-Bytes -Bytes $_.SizeBytes } }, `
564-
@{ Name = 'Refs'; Expression = { ($_.RepoTags | Select-Object -First 2) -join ', ' } }
565+
@{ Name = 'AgeHours'; Expression = { if ($null -eq $_.AgeHours) { '-' } else { $_.AgeHours } } }, `
566+
@{ Name = 'Size'; Expression = { Format-Bytes -Bytes $_.SizeBytes } }, `
567+
@{ Name = 'Refs'; Expression = { ($_.RepoTags | Select-Object -First 2) -join ', ' } }
565568

566569
$tableText = ($preview | Format-Table -AutoSize | Out-String -Width 220).TrimEnd()
567570
Write-Output $tableText

0 commit comments

Comments
 (0)