Build a Docker Dashboard with PowerShell Universal

Image Description

Daily PowerShell #18

Daily PowerShell Docker PowerShell Universal

November 3, 2021

quote Discuss this Article

In this post, we’ll look at how to build a Docker dashboard with PowerShell Universal.

Docker Desktop

We’ll be using Docker Desktop on Windows in this post. Docker Desktop provides a great interface for containerizing applications. It’s useful in development as well as deployment to ensure that you have isolated, repeatable environments. We even provide PowerShell Universal as a Docker image.

Once you have Docker Desktop running, you’ll have access to the docker command line tool to manage your Docker environment.

docker --version
Docker version 20.10.8, build 3967b7d

Integrating Docker with PowerShell

The docker command line tool returns stdout strings by default. You can change the format of the Docker output by using the --format parameter and specifying a Go template. The easiest way to integrate with PowerShell is to return the Docker output as JSON and then use ConvertTo-Json.

For example, to return all images as PowerShell PSCustomObjects, use the following.

docker image ls --format "{{json .}}" | ConvertFrom-Json | Format-Table

Containers CreatedAt                     CreatedSince  Digest ID           Repository                     SharedSize Size   Tag                           UniqueSize
---------- ---------                     ------------  ------ --           ----------                     ---------- ----   ---                           ----------
N/A        2021-10-20 13:25:50 -0600 MDT 13 days ago   <none> 374e376c25d4 azuresdk/autorest              N/A        238MB  latest                        N/A
N/A        2021-10-08 08:25:36 -0600 MDT 3 weeks ago   <none> 0a862dc7ed99 owasp/zap2docker-stable        N/A        1.71GB latest                        N/A
N/A        2021-10-02 15:46:50 -0600 MDT 4 weeks ago   <none> ee5bfd8960ab ironmansoftware/universal      N/A        757MB  2.3.2-ubunutu-18.04-ps-7.1.4  N/A
N/A        2021-10-02 15:46:50 -0600 MDT 4 weeks ago   <none> ee5bfd8960ab ironmansoftware/universal      N/A        757MB  latest                        N/A
N/A        2021-09-17 09:16:16 -0600 MDT 6 weeks ago          40bd2c207cdf ironmansoftware/universal      N/A        754MB  <none>                        N/A
N/A        2021-08-09 23:05:16 -0600 MDT 2 months ago         236e5fcd27d1 ironmansoftware/universal      N/A        695MB  <none>                        N/A
N/A        2021-08-09 23:04:09 -0600 MDT 2 months ago  <none> 78fc0ed5b396 ironmansoftware/universal      N/A        719MB  2.2.1-ubunutu-18.04-ps-7.1.2  N/A
N/A        2021-06-03 10:09:48 -0600 MDT 5 months ago  <none> 273e64b77c98 universal                      N/A        876MB  latest                        N/A
N/A        2021-06-03 10:05:04 -0600 MDT 5 months ago  <none> afb49237b478 <none>                         N/A        852MB  <none>                        N/A
N/A        2021-06-01 22:13:47 -0600 MDT 5 months ago         07316af2d12f ironmansoftware/universal      N/A        851MB  <none>                        N/A
N/A        2021-05-26 10:44:30 -0600 MDT 5 months ago  <none> 82c9f30a63de mcr.microsoft.com/powershell   N/A        291MB  lts-ubuntu-18.04              N/A
N/A        2021-05-17 06:33:15 -0600 MDT 5 months ago         bb1e31317f90 ironmansoftware/universal      N/A        878MB  <none>                        N/A
N/A        2021-02-19 17:18:04 -0700 MST 8 months ago  <none> 3fc91f7b6ea3 ironmansoftware/universal      N/A        917MB  1.5.13-ubunutu-18.04-ps-7.1.2 N/A
N/A        2021-02-19 17:08:44 -0700 MST 8 months ago  <none> ca88ddc60155 <none>                         N/A        918MB  <none>                        N/A
N/A        2021-02-19 17:02:38 -0700 MST 8 months ago  <none> 6d92c5633b00 <none>                         N/A        1.11GB <none>                        N/A
N/A        2021-02-19 16:55:27 -0700 MST 8 months ago  <none> 0ffeef9f2786 <none>                         N/A        1.08GB <none>                        N/A
N/A        2021-02-19 08:51:37 -0700 MST 8 months ago  <none> b0d7382eb3ea <none>                         N/A        539MB  <none>                        N/A

Creating Scripts in PowerShell Universal

Next, we’ll create scripts in PowerShell Universal to return all kinds of information about our Docker environment. Each script will call a different Docker command and return information as PSCustomObjects.

To create a script in PowerShell Universal, click Automation \ Scripts \ Create New Script.

Docker Containers

The following script lists all Docker containers as PSCustomObjects.

 docker ps --format "{{json .}}" | ConvertFrom-jSon

Docker Contexts

The following script lists all Docker contexts as PSCustomObjects.

 docker context list --format "{{json .}}"  | ConvertFrom-Json

Docker Disk Size

The following script lists the Docker disk size usage as a PSCustomObject.

 docker system df --format "{{json .}}" | ConvertFrom-Json

Docker Images

The following script lists all the Docker images as a PSCustomObject.

docker image ls --format "{{json .}}" | ConvertFrom-Json

Docker Networks

The following script lists all the Docker networks as a PSCustomObject.

docker network ls --format "{{json .}}" | ConvertFrom-Json

Docker System Info

The following script lists the Docker system info as a PSCustomObject.

docker system info --format "{{json .}}" | ConvertFrom-Json

Docker Volumes

The following script lists all the Docker volumes as a PSCustomObject.

docker volume ls --format "{{json .}}" | ConvertFrom-Json

Once all scripts are created, you will have to run them all at least once to view the data in the next step.

Creating a PowerShell Universal Page

Next, we’ll create pages for each one of the scripts. The pages will display the output from the last job run in a table.

Click User Interfaces \ Pages \ Create New Page to create a new page. You can start with Images.

Once the page has been created, you can edit the page by click View and then clicking Edit.

Once in edit mode, click Toolbox, Data Display and Table. Select the Images.ps1 script data source.

After selecting the data source and clicking Ok, you can resize the component and view the table data. Save the data and the page will reload with the table data displayed.

You can repeat this process for each one of the scripts.

To customize the icons for the page, when in edit mode, click Properties and select the Icon.

Once you’ve configured each page, you can click through the navigation to view all your Docker data.

Customizing Columns

Some tables, the System Info in particular, include many columns and you may want to reduce the amount of data shown. When editing the table, you can customize the column name and properties.

PowerShell Universal Docker Template

This post has been exported as a PowerShell Universal Docker Template. You can download it from our template library.