-
-
Save olljanat/340b4033eb24d8d33ec75f2c3c3b6b3d to your computer and use it in GitHub Desktop.
param ( | |
[switch]$RenameOrphanLayers | |
) | |
If ($RenameOrphanLayers) { | |
Write-Warning "$($env:COMPUTERNAME) -RenameOrphanLayers option enabled, will rename all orphan layers" | |
} | |
# Get known layers on Docker images | |
[array]$ImageDetails += docker images -q | ForEach { docker inspect $_ | ConvertFrom-Json } | |
ForEach ($Image in $ImageDetails) { | |
$ImageLayer = $Image.GraphDriver.Data.dir | |
[array]$ImageLayers += $ImageLayer | |
$LayerChain = Get-Content "$ImageLayer\layerchain.json" | |
If ($LayerChainFileContent -ne "null") { | |
[array]$ImageParentLayers += $LayerChain | ConvertFrom-Json | |
} | |
} | |
# Get known layes on Docker containers | |
[array]$ContainerDetails = docker ps -a -q | ForEach { docker inspect $_ | ConvertFrom-Json} | |
ForEach ($Container in $ContainerDetails) { | |
[array]$ContainerLayers += $Container.GraphDriver.Data.dir | |
} | |
# Get layers on disk | |
$LayersOnDisk = (Get-ChildItem -Path C:\ProgramData\Docker\windowsfilter -Directory).FullName | |
$ImageLayers += $ImageParentLayers | |
$UniqueImageLayers = $ImageLayers | Select-Object -Unique | |
[array]$KnownLayers = $UniqueImageLayers | |
$KnownLayers += $ContainerLayers | |
# Find orphan layers | |
$OrphanLayersTotal = 0 | |
ForEach ($Layer in $LayersOnDisk) { | |
If ($KnownLayers -notcontains $Layer) { | |
[array]$OrphanLayer += $Layer | |
$LayerSize = (Get-ChildItem -Path $Layer -Recurse -ErrorAction:SilentlyContinue | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum | |
$OrphanLayersTotal += $LayerSize | |
Write-Warning "$($env:COMPUTERNAME) - Found orphan layer: $($Layer -Replace '\r\n','') with size: $(($LayerSize -Replace '\r\n','') / 1MB) MB" | |
If (($RenameOrphanLayers) -and ($Layer -notlike "*-removing")) { | |
$LayerNewPath = $Layer + "-removing" | |
Rename-Item -Path $Layer -NewName $LayerNewPath | |
} | |
} | |
} | |
Write-Host "$($env:COMPUTERNAME) - Layers on disk: $($LayersOnDisk.count)" | |
Write-Host "$($env:COMPUTERNAME) - Image layers: $($UniqueImageLayers.count)" | |
Write-Host "$($env:COMPUTERNAME) - Container layers: $($ContainerLayers.count)" | |
$OrphanLayersTotalMB = $OrphanLayersTotal / 1MB | |
Write-Warning "$($env:COMPUTERNAME) - Found $($OrphanLayer.count) orphan layers with total size $OrphanLayersTotalMB MB" |
Hi @oljanat,
would it be possible for you to reopen this topic?
docker/for-win#745
I have a big volume for Docker (on Windows Server 2019), and today it was totally full. Would like to address the original issue, since it's stíll there...
@bctails I do not have rights to open those issues. Technically this bug have been fixed already long time ago but unfortunately it looks that Mirantis deal messed up Docker release process and they have not been able release new version. I also proposed that this would be released as part of hotfix version but it was denied (you can find discussion from docker-archive/engine#268 )
Anyway, you can use my custom build which contains those fixes and which we have been running even on production environment > 8 months now without issues https://github.com/olljanat/moby/releases/tag/19.03.5-olljanat2
@olljanat thank you for this script, very useful.
It's probably obvious but took me a while to realise that I wasn't in Windows containers mode which is necessary for this to work properly (in Linux containers mode commands like
docker images
only list Linux images, whereas files inwindowsfilter
are exclusively related to Windows container images).So, if anyone else is in Linux containers mode, or gets an error from this script such as
Get-Content : Cannot find path 'C:\layerchain.json' because it does not exist.
, first right-click on the Docker tray icon and choose Switch to Windows containers... before trying again.@olljanat might it be worth a comment at the top saying it only works in Windows containers mode? (or even better to detect if Docker is in linux mode & warn, e.g. checking the
OSType:
indocker info
output)Of course, after switching to Windows containers mode you may find (as I did) that the
windowsfilter
space usage is now explained bydocker system df
(i.e. nothing is orphaned at all) or that it is now cleaned up bydocker system prune
because these commands can now act on Windows containers.