Skip to content

Instantly share code, notes, and snippets.

@olljanat
Last active June 19, 2024 13:03
Show Gist options
  • 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"
@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-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

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