-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdate-DockerImages.ps1
More file actions
332 lines (278 loc) · 11.1 KB
/
Update-DockerImages.ps1
File metadata and controls
332 lines (278 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
function Update-DockerImages
{
<#
.SYNOPSIS
Pulls the latest versions of all local Docker images from their remote registries.
.DESCRIPTION
Inspects all locally available Docker images and attempts to pull the latest version
for each image that has a remote registry reference (i.e., excludes locally-built images
without a repository or those tagged as '<none>').
Images are identified by their Repository:Tag combination. Each eligible image is pulled
using 'docker pull', and results are reported as a summary object. Use -Verbose to see
detailed progress for each image.
Cross-platform compatible with PowerShell 5.1+ on Windows, macOS, and Linux. Requires
Docker CLI to be installed and available in PATH.
.PARAMETER Filter
An optional wildcard pattern to filter which images to update. Only images whose
Repository:Tag matches the pattern will be pulled. Supports standard PowerShell
wildcard characters (*, ?, []).
.PARAMETER ExcludeFilter
An optional wildcard pattern to exclude images from updating. Images whose
Repository:Tag matches this pattern will be skipped.
.PARAMETER PruneDanglingImages
When specified, runs 'docker image prune --force' after pulls complete to remove
dangling images.
.EXAMPLE
PS > Update-DockerImages
Pulls the latest version of every local Docker image that has a remote registry reference.
.EXAMPLE
PS > Update-DockerImages -Verbose
Pulls the latest images with detailed progress output for each image.
.EXAMPLE
PS > Update-DockerImages -Filter 'mcr.microsoft.com/*'
Only updates images from the Microsoft Container Registry.
.EXAMPLE
PS > Update-DockerImages -ExcludeFilter '*dev*'
Updates all images except those with 'dev' in their name.
.EXAMPLE
PS > Update-DockerImages -PruneDanglingImages
Updates eligible images, then prunes dangling Docker images.
.OUTPUTS
[PSCustomObject]
Returns an object with summary information:
- TotalImages : Total number of local images found
- Eligible : Number of images eligible for pulling (with remote registry)
- Updated : Number of images successfully pulled
- Skipped : Number of images skipped (no repository or filtered out)
- Failed : Number of images that failed to pull
- Results : Array of per-image result objects with Image, Status, and Message
- DanglingPruneRequested : $true when -PruneDanglingImages is specified
- DanglingPruneSucceeded : $true when dangling image prune completed successfully
- DanglingPruneError : Error text when dangling image prune fails, otherwise $null
.NOTES
- Requires Docker CLI in PATH
- Images tagged as '<none>' are automatically skipped
- Locally-built images without a registry push will attempt a pull and may fail gracefully
- Use -PruneDanglingImages to remove dangling images after updating
Author: Jon LaBelle
License: MIT
Source: https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/Developer/Update-DockerImages.ps1
.LINK
https://docs.docker.com/reference/cli/docker/image/pull/
.LINK
https://docs.docker.com/reference/cli/docker/image/ls/
.LINK
https://docs.docker.com/reference/cli/docker/image/prune/
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')]
[CmdletBinding(SupportsShouldProcess)]
[OutputType([PSCustomObject])]
param(
[Parameter()]
[String]$Filter,
[Parameter()]
[Alias('Exclude')]
[String]$ExcludeFilter,
[Parameter()]
[Alias('Prune')]
[Switch]$PruneDanglingImages
)
begin
{
Write-Verbose 'Starting Docker image update'
$dockerCommand = Get-Command -Name 'docker' -ErrorAction SilentlyContinue
if (-not $dockerCommand)
{
throw 'Docker is not installed or not available in PATH. Please install Docker and try again.'
}
Write-Verbose "Docker found at: $($dockerCommand.Source)"
}
process
{
# Get all local images in JSON format for reliable parsing
$imageJson = & docker image ls --format '{{json .}}' 2>&1
if ($LASTEXITCODE -ne 0)
{
throw "Failed to list Docker images: $imageJson"
}
$images = @()
foreach ($line in $imageJson)
{
$trimmed = "$line".Trim()
if ($trimmed -and $trimmed.StartsWith('{'))
{
try
{
$images += $trimmed | ConvertFrom-Json
}
catch
{
Write-Verbose "Skipping unparseable line: $trimmed"
}
}
}
Write-Verbose "Found $($images.Count) local Docker image(s)"
# Deduplicate by Repository:Tag so we don't pull the same image twice
$seen = @{}
$uniqueImages = @()
foreach ($img in $images)
{
$repo = $img.Repository
$tag = $img.Tag
# Skip images with no repository or tag
if ([string]::IsNullOrWhiteSpace($repo) -or $repo -eq '<none>')
{
continue
}
if ([string]::IsNullOrWhiteSpace($tag) -or $tag -eq '<none>')
{
continue
}
$imageRef = '{0}:{1}' -f $repo, $tag
if (-not $seen.ContainsKey($imageRef))
{
$seen[$imageRef] = $true
$uniqueImages += [PSCustomObject]@{
Repository = $repo
Tag = $tag
ImageRef = $imageRef
}
}
}
Write-Verbose "$($uniqueImages.Count) unique image(s) with valid repository and tag"
# Apply filters
$eligibleImages = $uniqueImages
if ($Filter)
{
$eligibleImages = @($eligibleImages | Where-Object { $_.ImageRef -like $Filter })
Write-Verbose "$($eligibleImages.Count) image(s) matching filter '$Filter'"
}
if ($ExcludeFilter)
{
$eligibleImages = @($eligibleImages | Where-Object { $_.ImageRef -notlike $ExcludeFilter })
Write-Verbose "$($eligibleImages.Count) image(s) after exclude filter '$ExcludeFilter'"
}
$totalImages = $images.Count
$skippedCount = $totalImages - $eligibleImages.Count
$updatedCount = 0
$failedCount = 0
$results = @()
$danglingPruneSucceeded = $false
$danglingPruneError = $null
if ($eligibleImages.Count -eq 0)
{
Write-Verbose 'No eligible images to update'
}
$processedImages = 0
foreach ($img in $eligibleImages)
{
$imageRef = $img.ImageRef
$processedImages++
Write-Host "Pulling image [$processedImages/$($eligibleImages.Count)]: $imageRef"
if (-not $PSCmdlet.ShouldProcess($imageRef, 'Pull latest image'))
{
$skippedCount++
$results += [PSCustomObject]@{
Image = $imageRef
Status = 'Skipped'
Message = 'Skipped by -WhatIf'
}
continue
}
Write-Verbose "Pulling $imageRef..."
try
{
$pullOutput = & docker pull $imageRef 2>&1
if ($LASTEXITCODE -eq 0)
{
$statusMessage = 'Updated'
# Check if the image was already up to date
$outputText = $pullOutput -join "`n"
if ($outputText -match 'Image is up to date|Already exists')
{
$statusMessage = 'Already up to date'
}
$updatedCount++
$results += [PSCustomObject]@{
Image = $imageRef
Status = 'Success'
Message = $statusMessage
}
Write-Verbose "$imageRef : $statusMessage"
}
else
{
$failedCount++
$errorMessage = ($pullOutput | Where-Object { $_ }) -join ' '
$results += [PSCustomObject]@{
Image = $imageRef
Status = 'Failed'
Message = $errorMessage
}
Write-Warning "Failed to pull ${imageRef}: $errorMessage"
}
}
catch
{
$failedCount++
$results += [PSCustomObject]@{
Image = $imageRef
Status = 'Failed'
Message = $_.Exception.Message
}
Write-Warning "Error pulling ${imageRef}: $($_.Exception.Message)"
}
}
if ($PruneDanglingImages)
{
if ($PSCmdlet.ShouldProcess('Dangling Docker images', 'Prune'))
{
Write-Host 'Pruning dangling Docker images...'
Write-Verbose 'Pruning dangling Docker images...'
try
{
$pruneOutput = & docker image prune --force 2>&1
if ($LASTEXITCODE -eq 0)
{
$danglingPruneSucceeded = $true
Write-Verbose 'Dangling Docker image prune completed'
}
else
{
$danglingPruneError = ($pruneOutput | Where-Object { $_ }) -join ' '
if (-not $danglingPruneError)
{
$danglingPruneError = 'docker image prune failed with an unknown error.'
}
Write-Warning "Failed to prune dangling images: $danglingPruneError"
}
}
catch
{
$danglingPruneError = $_.Exception.Message
Write-Warning "Error pruning dangling images: $danglingPruneError"
}
}
else
{
Write-Verbose 'Skipping dangling Docker image prune due to -WhatIf'
}
}
}
end
{
$summary = [PSCustomObject]@{
TotalImages = $totalImages
Eligible = $eligibleImages.Count
Updated = $updatedCount
Skipped = $skippedCount
Failed = $failedCount
Results = $results
DanglingPruneRequested = $PruneDanglingImages.IsPresent
DanglingPruneSucceeded = $danglingPruneSucceeded
DanglingPruneError = $danglingPruneError
}
Write-Verbose "Update complete: $updatedCount updated, $failedCount failed, $skippedCount skipped"
$summary
}
}