I've been continuing to tinker with my PowerShell command for getting information about Docker containers. The Docker CLI is fine, but it is very difficult to work with the output or do much with it. That's why I prefer to have objects in a PowerShell pipeline. One of the Docker container elements that I ignored until now was the container size. The whole beauty of containers is that they are small by design so why not get that information as well?
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Container Size
You can use the Docker CLI to display a container size.
From what I can tell, this is the size of writable layers in the container. For me this always seems to be 0 bytes. Still, eventually I might have a container with an actual value so I need some code to extract it.
$name = "cow1" $sz = docker ps -asf "name=$name" --format "{{json .}}" | Convertfrom-json | Select -expandproperty size [regex]$rx = "\d+" $szbytes = $rx.match($sz).value
Using the JSON trick I discussed last time I can extract the Size property which in my case will be 0B. I use a regular expression pattern to get just the numeric portion of this string, or 0.
Get Docker Files
Each container also includes a set of files, such as logs, which are stored in the Docker program location. Using a little PowerShell processing, I can get that location from Docker system information.
$dockpath = ((docker system info | select-string "docker root dir") -split ": ")[1]
In Windows, this should be C:\ProgramData\Docker. Each container has its own directory.
The folder name is the container ID which I already know how to get. From there it is a simple matter of measuring the total size of all files.
$name = "cow1" $ID = (docker inspect $name --format "{{json .ID}}") -replace '"','' $stat = Get-Childitem (join-path $dockpath "containers\$ID") -file -recurse | Measure-Object -Property length -sum
$Stat gives me a sum value of 11977.
Include Docker Container Volumes
Finally, a container might have an associated volume for persistent storage.
$c = docker inspect cow1 | Convertfrom-json
Again, this is pretty easy to measure.
$m = $c.mounts | foreach { get-childitem -path $_.source -file -recurse } | measure-object length -sum
I can then add all the values together to get a pretty close approximation of the container's size.
Updated PowerShell Function
With this in mind, I updated the Get-DockerContainer function.
I updated the class to include size.
PowerShell Formatting
The function includes code to use a format.ps1xml file which I've also put up on Github.
The default view is still a table.
But I also added a 2nd table view called stats.
Now I have very useful PowerShell command for at least getting Docker container information.
Because it is in PowerShell, I can export it, dump it to a database, create an HTML report or anything else you can think of in PowerShell. As I said, the Docker CLI is helpful, but it isn't PowerShell useful.