no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | ms_windows_ms_sql:windows_docker_purge_orphan_layers [2021/12/25 09:51] (current) – created admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | Если директория **C: | ||
+ | docker system prune -a -f | ||
+ | не помогает освобить место, то нужно почистить orphan (сиротские) слои, не принадлежащие никаким образам. | ||
+ | < | ||
+ | param ( | ||
+ | [switch]$RenameOrphanLayers, | ||
+ | [switch]$CalcSize | ||
+ | ) | ||
+ | |||
+ | If ($RenameOrphanLayers) { | ||
+ | Write-Warning " | ||
+ | } | ||
+ | |||
+ | # 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 " | ||
+ | If ($LayerChainFileContent -ne " | ||
+ | [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: | ||
+ | $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 | ||
+ | If ($CalcSize) { | ||
+ | $LayerSize = (Get-ChildItem -Path $Layer -Recurse -ErrorAction: | ||
+ | $OrphanLayersTotal += $LayerSize | ||
+ | Write-Warning " | ||
+ | } | ||
+ | else { | ||
+ | Write-Warning " | ||
+ | } | ||
+ | |||
+ | If (($RenameOrphanLayers) -and ($Layer -notlike " | ||
+ | GrantFullAccessToAdmins $Layer | ||
+ | $LayerNewPath = $Layer + " | ||
+ | Rename-Item -Path $Layer -NewName $LayerNewPath | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | Write-Host " | ||
+ | Write-Host " | ||
+ | Write-Host " | ||
+ | If ($CalcSize) { | ||
+ | $OrphanLayersTotalMB = $OrphanLayersTotal / 1MB | ||
+ | Write-Warning " | ||
+ | } | ||
+ | else { | ||
+ | Write-Warning " | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | function GrantFullAccessToAdmins ($Path) { | ||
+ | $NewAcl = Get-Acl -Path $Path | ||
+ | # Set properties | ||
+ | $identity = " | ||
+ | $fileSystemRights = " | ||
+ | #$type = " | ||
+ | # Create new rule | ||
+ | $fileSystemAccessRuleArgumentList = $identity, $fileSystemRights, | ||
+ | $fileSystemAccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $fileSystemAccessRuleArgumentList | ||
+ | # Apply new rule | ||
+ | $NewAcl.SetAccessRule($fileSystemAccessRule) | ||
+ | Set-Acl -Path $Path -AclObject $NewAcl | ||
+ | } | ||
+ | |||
+ | # Purge untagged images | ||
+ | if ( $(docker images -f " | ||
+ | |||
+ | GrantFullAccessToAdmins " | ||
+ | |||
+ | Find-OrphanDockerLayers -RenameOrphanLayers | ||
+ | |||
+ | # Due to short docker service shutdown timeout I will restart it untill all labeled layers be purged. | ||
+ | while (Get-ChildItem -Path C: | ||
+ | Stop-Service *docker* | ||
+ | Restart-Service *docker* | ||
+ | } | ||
+ | </ |