Skip to content

Instantly share code, notes, and snippets.

@olljanat
Last active September 6, 2023 17:54
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save olljanat/340b4033eb24d8d33ec75f2c3c3b6b3d to your computer and use it in GitHub Desktop.
Save olljanat/340b4033eb24d8d33ec75f2c3c3b6b3d to your computer and use it in GitHub Desktop.
Find Windows containers orphan layers
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"
@sparrowt
Copy link

sparrowt commented Jan 30, 2019

@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 in windowsfilter 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: in docker info output)

Of course, after switching to Windows containers mode you may find (as I did) that the windowsfilter space usage is now explained by docker system df (i.e. nothing is orphaned at all) or that it is now cleaned up by docker system prune because these commands can now act on Windows containers.

@bctails
Copy link

bctails commented Aug 7, 2020

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...

@olljanat
Copy link
Author

olljanat commented Aug 8, 2020

@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/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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment