Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active November 10, 2023 15:51
Show Gist options
  • Save jdhitsolutions/ba49375590597030c49f9f6e815dbdc1 to your computer and use it in GitHub Desktop.
Save jdhitsolutions/ba49375590597030c49f9f6e815dbdc1 to your computer and use it in GitHub Desktop.
A Powershell function that wraps Docker command line output to display disk usage. Save the files without the numeric prefix.
#requires -version 5.1
Function Get-DockerDiskUsage {
[cmdletbinding()]
[alias("gddu")]
[OutputType("Get-DockerDiskUsage.DockerDiskUsage")]
Param(
[Parameter(Position = 0, HelpMessage = "Get specific usage types. The default is All")]
[ValidateSet("Images","Containers","Volumes","Cache")]
[string[]]$Type
)
Begin {
Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)"
#define a class
Class DockerDiskUsage {
[string]$Type
[int]$Total
[int]$Active
[double]$Size
[double]$Reclaim
[int]$ReclaimPercent
#these properties are hidden but they can be used in format.ps1xml files or Select-Object expressions
hidden [string]$SizeUnit
hidden [string]$ReclaimUnit
#methods are hidden because they are only used internally
hidden [string] GetUnit($value) {
Write-Verbose "[CLASS] Getting unit string from $value"
[regex]$unitrx = "[KMGT]?B"
$r = $unitrx.match($value).value
write-Verbose "[CLASS] Returning $r"
return $r
}
hidden [double] GetValue($value) {
Write-Verbose "[CLASS] Getting numeric value from $value"
[regex]$szrx = "\d+(\.\d+)?"
$r = $szrx.match($value).value
Write-Verbose "[CLASS] Returning $r"
return $r
}
hidden [int] GetUnitValue([string]$SizeUnit) {
Write-Verbose "[CLASS] Getting unit value from $SizeUnit"
$unit = Switch ($sizeUnit) {
"TB" { 1TB; break}
"GB" { 1GB; break}
"MB" { 1MB; break}
"KB" { 1KB; break}
default { 1}
}
Write-Verbose "[CLASS] Returning $unit"
return $unit
}
#the constructor
DockerDiskUsage ($Item) {
Write-Verbose "[CLASS] Invoking constructor for $($item.type)"
$this.Type = $item.Type
$this.Total = $item.totalCount
$this.Active = $item.active
$this.SizeUnit = $this.getunit($item.size)
$this.reclaimUnit = $this.GetUnit($item.reclaimable)
$this.Size = $this.getValue($item.size) * $this.GetUnitValue($this.SizeUnit)
$this.Reclaim = $this.GetValue($item.reclaimable.split()[0]) * $this.GetUnitValue($this.ReclaimUnit)
$this.ReclaimPercent = $this.GetValue($item.reclaimable.split()[1])
}
} #close class definition
} #begin
Process {
Write-Verbose "[PROCESS] Getting Docker Disk Usage"
$data = docker system df --format "{{json .}}" | ConvertFrom-Json
if ($data) {
foreach ($item in $data) {
$item | Out-String | Write-Verbose
#filter on type if specified via the Type parameter
if ( (-not $Type) -OR ( $item.type -match $($type -join "|"))){
New-Object -TypeName DockerDiskUsage -ArgumentList $item
}
} #foreach item
}
else {
Write-Warning "Failed to get any Docker disk usage data"
}
} #process
End {
Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)"
} #end
}
$format = "C:\scripts\dockerusage.format.ps1xml"
if (Test-Path -path $format) {
Update-FormatData $format
}
<?xml version="1.0" encoding="UTF-8"?>
<!--
format type data generated 04/05/2019 11:19:27
by BOVINE320\Jeff
-->
<Configuration>
<ViewDefinitions>
<View>
<!--Created 04/05/2019 11:19:27 by BOVINE320\Jeff-->
<Name>default</Name>
<ViewSelectedBy>
<TypeName>Get-DockerDiskUsage.DockerDiskUsage</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Type</Label>
<Width>15</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Total</Label>
<Width>7</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Active</Label>
<Width>7</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Size</Label>
<Width>17</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Reclaim</Label>
<Width>14</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>ReclaimPercent</Label>
<Width>14</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Type</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Total</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Active</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Size</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Reclaim</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>ReclaimPercent</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<!--Created 04/05/2019 13:41:17 by BOVINE320\Jeff-->
<Name>docker</Name>
<ViewSelectedBy>
<TypeName>Get-DockerDiskUsage.DockerDiskUsage</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Type</Label>
<Width>15</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Total</Label>
<Width>7</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Active</Label>
<Width>7</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Size</Label>
<Width>11</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Reclaimable</Label>
<Width>16</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Type</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Total</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Active</PropertyName>
</TableColumnItem>
<TableColumnItem>
<Scriptblock>"{0}{1}" -f $($_.size/$_.GetUnitValue($_.sizeunit)),$_.sizeunit</Scriptblock>
</TableColumnItem>
<TableColumnItem>
<Scriptblock>"{0}{1} ({2}%)" -f $($_.reclaim/$_.GetUnitValue($_.reclaimunit)),$_.reclaimunit,($_.ReclaimPercent)</Scriptblock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
@jdhitsolutions
Copy link
Author

Save the two files to your computer. Modify the $format variable at the end of the script file to reflect where you saved the ps1xml file. Dot source the script file.

PS C:\> . c:\scripts\get-dockerdiskusage.ps1

Then you can run it.

PS C:\> get-dockerdiskusage

Type              Total  Active              Size        Reclaim ReclaimPercent
----              -----  ------              ----        ------- --------------
Images                3       2    1663226085.376    204891750.4             12
Containers            2       1       363226726.4              0              0
Local Volumes         0       0                 0              0              0
Build Cache           0       0                 0              0              0

PS C:\> get-dockerdiskusage | format-table -view docker

Type            Total   Active         Size      Reclaimable
----            -----   ------         ----      -----------
Images          3       2           1.549GB    195.4MB (12%)
Containers      2       1           346.4MB          0B (0%)
Local Volumes   0       0                0B          0B (0%)
Build Cache     0       0                0B          0B (0%)

PS C:\>

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