Docker from the powershell, take two
Take one
Back in 2016, I posted a quick and dirty technique for parsing the output from the docker CLI utilities. I recently returned to this looking for a slightly more robust approach. Back then I'd also pointed out the existence of a PowerShell module from Microsoft that made things a bit easier, but this has now been deprecated with a recommendation to use the docker cli directly or to use Docker.DotNet. This latter is a full-blown API that might be really handy for some tasks, but not for what I had in mind. The Docker CLI, OTOH is the problem I'm trying to solve. Yes, sure, I might get further with spending more time figuring out filters and formatting, but the bottom line is that the PowerShell has made me lazy, and I plan to stay that way. A quick Google turns up any number of people hacking away at Bash to solve docker's CLI inadequacies. Whatever. This is Windows, and I expect to see a pipeline full of objects with properties. :-)
Take two
It turns out that a reasonably generic approach to parsing docker's output gives you exactly that: a pipeline full of objects. I've just added a couple of functions to my $profile, which means I can do something like
docker ps -a | parseColumns
and get the columns from ps in my pipeline as object properties. Here is the code:
function parseColumnsFromHeader ($line){
$cols = @()
# Lazily match chunks of text followed by at least two whitespace or a line end
$re = [regex]"((.*?)(\s{2,}|$))*"
$matches = $re.Match($line)
# Group 0 is the whole match, then you count left parens, so
# group 1 is ((.*?)(\s{2,}|$)),
foreach ($capture in $matches.Groups[1].Captures){
if ($capture.Length -gt 0) {
# The captures are therefore both the chunk of text and the following whitespace
# so the length is right, but we trim for the name
$col = @{
Name = $capture.Value.Trim()
Index = $capture.Index
Length = $capture.Length
}
$cols += New-Object PSObject -Property $col;
}
}
return $cols
}
filter parseColumns {
if (-not $headerDone) {
$cols = parseColumnsFromHeader $_;
$headerDone = $true;
} else {
$propertiesHash = [ordered]@{}
foreach($col in $cols) {
$propertiesHash.Add($col.Name, ([string]$_).Substring($col.Index, $col.Length))
}
New-Object PSObject -Property $propertiesHash
}
}
This works equally well for other docker commands, such as 'docker images'. The only proviso is that the output begins with a single line with column headers. As long as the headers themselves have at least two white space characters between them, and no more than one in the header text itself, it should be fine.
OK - it's fairly generic. Maybe it's better than my previous approach. That said, I'm irritated by the reliance on having to parse columns based on white space. This could break at any moment. Should I be looking for something better?
Not good enough
The thing is, I'm always on the lookout for techniques that will work everywhere. The reason I'm reasonably fluent in the vi editor today is that some years ago, I consciously chose to stop learning Emacs. This isn't a holy wars thing. Emacs was probably better. Maybe... but it wasn't everywhere. Certainly at the time, vi was available on every Unix machine in the world. (These days I have to type "apt-get update && apt-get install -y vim && vi foobar.txt" far more often than I'd like, but on those machines, there's no editor installed at all, and I understand why.)
One of the reasons I never really got along with the Powershell Module is that on any given day, I can't guarantee I'll be able to install modules on the system I'm working on. I probably can paste some code into my $profile, or perhaps even more commonly, grab a one-liner from this blog and paste it directly in my shell. But general hacks in your fingertips FTW.
A better way?
So maybe I just have to learn to love the lowest common denominator. So if I'm on a bare windows Machine doing Docker, can I get the pain threshold down far enough? Well just maybe!
If you look at the Docker CLI documentation, you'll see that pretty much every command takes a --format flag, which allows you to pass a GO template. If you want to output the results as json, it's fairly simple, and then of course, the built-in ConvertFrom-Json cmdlet will get you the rest of the way.
I'm reasonably sure that before too long I'll be typing this kind of thing instead of using the functions above:
docker ps -a --format '{{json .}}' | ConvertFrom-Json