Working with docker output in PowerShell


Just a quick post today 🙂

These days I find myself working in both PowerShell and bash, and when I’m in PowerShell, I like to have objects that I can work with. With the Azure CLI I can use the --json switch and then use something like jq to process the output, or in PowerShell I can use ConvertFrom-Json to parse the JSON into objects that can be piped/filtered/projected in the normal way.

When working with the docker command line, things are slightly different. The query commands (e.g. ps, images) support filtering but only the ps command seems to support formatting.

To enable me to work with docker more easily in PowerShell, I wrote a little helper: ConvertFrom-Docker

Installing ConvertFrom-Docker

To get started, grab the script from https://gist.github.com/stuartleeks/4661c3c05678eec822bc and save as ConvertFromDocker.ps1

Then you need to dot source it, simply because I’ve not found time to wrap it up in a module yet – I’ll update this post if I do 🙂

. ./ConvertFromDocker.ps1

Using ConvertFrom-Docker

With this done you can run the following command:

docker ps --no-trunc | ConvertFrom-Docker

The output will be similar to

Image       : containersched
Ports       : 0.0.0.0:5123->5000/tcp
Names       : desperate_jang
Command     : "dnx -p project.json web"
Created     : 12 days ago
ContainerId : 9fb9d10f6a6d6423ff3b3a7734375726cc79706038942201442e636b50fa7979
Status      : Up 12 days

Image       : stuartleeks/vorlon
Ports       : 0.0.0.0:1337->1337/tcp
Names       : stoic_yalow
Command     : "node /vorlon/bin/vorlon"
Created     : 2 weeks ago
ContainerId : 5e64f5a8accabd039b24bcfec6b89127f46c14eb16e2d165461071f1e557151c
Status      : Up 2 weeks

Image       : jwilder/nginx-proxy
Ports       : 0.0.0.0:80->80/tcp, 443/tcp
Names       : angry_visvesvaraya
Command     : "/app/docker-entrypoint.sh forego start -r"
Created     : 2 weeks ago
ContainerId : 7fb4709668946f5bdc915874b9537b5cc52ccaf7fa2c74586b93af279db6512b
Status      : Up 2 weeks

This works with docker ps, docker images, etc and uses the first line of output from the command to determine the property names for the object 🙂

Enjoy!

Comments (0)

Skip to main content