File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ FROM mcr.microsoft.com/windows/servercore:ltsc2016
2+ COPY check-baseimage.ps1 check-baseimage.ps1
3+ RUN powershell -File check-baseimage.ps1
Original file line number Diff line number Diff line change 1+ # Check base image for an update
2+
3+ Once you push a Windows image it might get out of date.
4+ Let's hack a small script to check if the current image is out of date
5+ and there is a newer one in MCR, the Microsoft Container Registry.
6+
7+ ## Example
8+
9+ ```
10+ $ docker build -t stefanscherer/check-baseimage .
11+ Sending build context to Docker daemon 4.608kB
12+ Step 1/3 : FROM mcr.microsoft.com/windows/servercore:ltsc2016
13+ ---> ea9f7aa13d03
14+ Step 2/3 : COPY check-baseimage.ps1 check-baseimage.ps1
15+ ---> 719d742e96ec
16+ Step 3/3 : RUN powershell -File check-baseimage.ps1
17+ ---> Running in 003723fac24c
18+ Base image Update Build Revision 2724, Hub Update Build Revision 3204
19+ C:\check-baseimage.ps1 : Your base image has update revision 2724, but should be
20+ updated to 3204!
21+ At C:\check-baseimage.ps1:15 char:3
22+ + Write-Error "Your base image has update revision $currUBR, but should be ...
23+ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24+ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorExcep
25+ tion
26+ + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorExceptio
27+ n,check-baseimage.ps1
28+
29+ The command 'cmd /S /C powershell -File check-baseimage.ps1' returned a non-zero code: 1
30+ ```
Original file line number Diff line number Diff line change 1+ $ErrorActionPreference = ' Stop'
2+
3+ # fetch the current version number from base image
4+ $current = (Get-ItemProperty " HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" )
5+ $currUBR = $current.UBR
6+
7+ # fetch the maximum version number from MCR by filtering and sorting the JSON result
8+ $prefix = " $ ( $current.CurrentMajorVersionNumber ) .$ ( $current.CurrentMinorVersionNumber ) .$ ( $current.CurrentBuildNumber ) ."
9+ $json = $ (Invoke-WebRequest - UseBasicParsing https:// mcr.microsoft.com / v2/ windows/ servercore/ tags/ list | ConvertFrom-Json )
10+ $hubUBR = ($json.tags | Where-Object - FilterScript { $_.StartsWith ($prefix ) -and $_ -Match " ^\d+\.\d+\.\d+\.\d+$" } | % {[System.Version ]$_ }| sort)[-1 ].Revision
11+
12+ Write-Output " Base image Update Build Revision $currUBR , Hub Update Build Revision $hubUBR "
13+
14+ if ($currUBR -Lt $hubUBR ) {
15+ Write-Error " Your base image has update revision $currUBR , but should be updated to $hubUBR !"
16+ } else {
17+ Write-Output " Your base image seems up to date."
18+ }
You can’t perform that action at this time.
0 commit comments