Modules/businessdev.ALbuild.Containers/Private/Get-BcImageSizeBytes.ps1
|
function Get-BcImageSizeBytes { <# .SYNOPSIS Size of a local Docker image in bytes, or 0 when it cannot be read. .DESCRIPTION Note this is the image's TOTAL size as Docker reports it, which includes the layers shared with the generic base image. Several version images on one host therefore cost far less in total than the sum of these numbers - the base is stored once. Cache budgeting should account for that rather than multiplying this figure by the number of cached versions. .PARAMETER ImageName The image tag or id. .PARAMETER DockerExecutable Docker executable. .OUTPUTS System.Int64 #> [CmdletBinding()] [OutputType([long])] param( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $ImageName, [string] $DockerExecutable = 'docker' ) try { $result = Invoke-BcDocker -DockerExecutable $DockerExecutable -Quiet -PassThru -SuccessExitCodes @(0, 1) ` -Arguments @('image', 'inspect', '--format', '{{.Size}}', $ImageName) if ($result.ExitCode -ne 0) { return [long]0 } $value = "$($result.StdOut)".Trim() $parsed = [long]0 if ([long]::TryParse($value, [ref]$parsed)) { return $parsed } return [long]0 } catch { return [long]0 } } |