Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active January 25, 2024 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/ab6ec7d2504f6493eec1187ef13f59c7 to your computer and use it in GitHub Desktop.
Save guitarrapc/ab6ec7d2504f6493eec1187ef13f59c7 to your computer and use it in GitHub Desktop.
Get docker memory status for Windows running Docker Desktop for Windows.
PS> powershell.exe -Command ./Get-DockerMemoryStatus.ps1
7069/24832 (28%)

Sample

$ docker stats --no-stream --format "{{.MemUsage}}"
32.75MiB / 23.72GiB
164.2MiB / 23.72GiB
604KiB / 23.72GiB
938.4MiB / 23.72GiB
951.9MiB / 23.72GiB
741.7MiB / 23.72GiB
68.89MiB / 23.72GiB
68.97MiB / 23.72GiB
68.96MiB / 23.72GiB
68.96MiB / 23.72GiB
1.358GiB / 23.72GiB
17.81MiB / 23.72GiB
34.6MiB / 23.72GiB
85.61MiB / 23.72GiB
21.16MiB / 23.72GiB
262.9MiB / 23.72GiB
493.7MiB / 23.72GiB
381.1MiB / 23.72GiB
401MiB / 23.72GiB
7.91MiB / 23.72GiB
452.8MiB / 23.72GiB
445.1MiB / 23.72GiB
8.066MiB / 23.72GiB
$reservedMemoryMB = Get-Content -Raw -Path "${env:APPDATA}/Docker/settings.json" | ConvertFrom-Json | Select-Object -ExpandProperty memoryMiB
$dockerStats = docker stats --no-stream --format "{{.MemUsage}}"
$totalMemoryKB = 0
foreach ($line in $dockerStats) {
$memoryUsage = $line.Split('/')[0].Trim()
$value = [double]($memoryUsage -replace '[^\d.]')
if ($memoryUsage -match "GiB") {
$totalMemoryKB += $value * 1024 * 1024
} elseif ($memoryUsage -match "MiB") {
$totalMemoryKB += $value * 1024
} elseif ($memoryUsage -match "KiB") {
$totalMemoryKB += $value
}
}
[int]$totalMemoryMB = $totalMemoryKB / 1024
echo "$totalMemoryMB/$reservedMemoryMB ($([int]($totalMemoryMB/$reservedMemoryMB * 100))%)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment