Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Extending PowerShell and Docker Containers

Posted on April 2, 2019April 2, 2019

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?

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.

Getting docker 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.

Docker container folders

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

Docker container mounts

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.

Geting Docker container size with PowerShell

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.

Default formatted view of Docker containers

But I also added a 2nd table view called stats.

Using the Stats alternate view

Now I have very useful PowerShell command for at least getting Docker container information.

Getting all 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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d