Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Alternative Docker PS

I find that the output of docker ps is too wide, and will always wrap on my screen. It looks like I'm not the only one. Unfortunately, using docker ps | less -S doesn't work for me either when using byobu, so I came up with my own script to get the output in a veritcal, YAML based format. It produces a result like below:

Steps

Use the following script to output information about your running containers. You can pass the --full optional parameter to get all the details, instead of a shorter version.

#!/usr/bin/env php
<?php

class Container
{
    private $m_command;
    private $m_createdAt;
    private $m_id;
    private $m_image;
    private $m_labels;
    private $m_localVolumes;
    private $m_mounts;
    private $m_names;
    private $m_networks;
    private $m_ports;
    private $m_runningFor;
    private $m_size;
    private $m_status;
    private $m_verbose;


    public function __construct(array $arrayForm, bool $verbose)
    {
        $this->m_command = $arrayForm['Command'];
        $this->m_createdAt = $arrayForm['CreatedAt'];
        $this->m_id = $arrayForm['ID'];
        $this->m_image = $arrayForm['Image'];
        $this->m_labels = $arrayForm['Labels'];
        $this->m_localVolumes = $arrayForm['LocalVolumes'];
        $this->m_mounts = $arrayForm['Mounts'];
        $this->m_names = $arrayForm['Names'];
        $this->m_networks = $arrayForm['Networks'];
        $this->m_ports = $arrayForm['Ports'];
        $this->m_runningFor = $arrayForm['RunningFor'];
        $this->m_size = $arrayForm['Size'];
        $this->m_status = $arrayForm['Status'];
        $this->m_verbose = $verbose;
    }


    public function __toString()
    {
        $stringForm = "";
        $red = "\033[31m";
        $lightRed = "\033[91m";
        $noColor = "\033[0m";
        $green = "\033[32m";
        $lightGreen = "\033[92m";
        $lightYellow = "\033[93m";
        $lightBlue = "\033[94m";
        $lightCyan = "\033[96m";
        $lightMagenta = "\033[95m";

        $darkGrey = "\033[90m";

        $cyan = "\033[36m";
        $blue = "\033[34m";
        $magenta = "\033[35m";
        $yellow = "\033[33m";
        $lightGrey = "\033[37m";
        $white = "\033[97m";
        $dim = "\033[2m";
        $resetDim = "\033[22m";

        if ($this->m_verbose === false)
        {
            $shortId = substr($this->m_id, 0, 12);

            $stringForm =
                "{$lightGrey}Image{$darkGrey}: {$lightGreen}{$this->m_image}{$noColor}" . PHP_EOL .
                "{$lightGrey}Name{$darkGrey}:{$lightMagenta} {$this->m_names}{$noColor}" . PHP_EOL .
                "{$lightGrey}ID{$darkGrey}: {$lightCyan}{$shortId}{$noColor}" . PHP_EOL .
                "{$lightGrey}Mounts{$darkGrey}: {$lightBlue}{$this->m_mounts}{$noColor}" . PHP_EOL .
                "{$lightGrey}Ports{$darkGrey}: {$cyan}{$this->m_ports}{$noColor}" . PHP_EOL .
                "{$lightGrey}Status{$darkGrey}: {$white}{$this->m_status}{$noColor}" . PHP_EOL;
        }
        else
        {
            $stringForm =
                "{$lightGrey}Image{$darkGrey}: {$lightGreen}{$this->m_image}{$noColor}" . PHP_EOL .
                "{$lightGrey}Name{$darkGrey}:{$lightMagenta} {$this->m_names}{$noColor}" . PHP_EOL .
                "{$lightGrey}ID{$darkGrey}: {$lightCyan}{$this->m_id}{$noColor}" . PHP_EOL .
                "{$lightGrey}Command{$darkGrey}: {$blue}{$this->m_command}" . PHP_EOL .
                "{$lightGrey}CreatedAt{$darkGrey}: {$green}{$this->m_createdAt}" . PHP_EOL .
                "{$lightGrey}Mounts{$darkGrey}: {$lightBlue}{$this->m_mounts}{$noColor}" . PHP_EOL .
                "{$lightGrey}Ports{$darkGrey}: {$cyan}{$this->m_ports}{$noColor}" . PHP_EOL .
                "{$lightGrey}Size{$darkGrey}: {$dim}{$white}{$this->m_size}{$resetDim}" . PHP_EOL .
                "{$lightGrey}Status{$darkGrey}: {$white}{$this->m_status}{$noColor}" . PHP_EOL .
                "{$lightGrey}LocalVolumes{$darkGrey}: {$dim}{$lightYellow}{$this->m_localVolumes}{$resetDim}{$noColor}" . PHP_EOL .
                "{$lightGrey}Labels{$darkGrey}: {$dim}{$white}{$this->m_labels}{$resetDim}{$noColor}" . PHP_EOL;
        }

        return $stringForm;
    }
}


$fullVersion = (isset($argv[1]) && strtolower($argv[1]) === "--full");

$info = shell_exec("docker ps --no-trunc --format '{{ json .}}'");
$lines = array_filter(explode(PHP_EOL, $info));
$containers = [];

foreach ($lines as $line)
{
    $containerArray = json_decode($line, true);
    $containers[] = new Container($containerArray, $fullVersion);
}

$string = PHP_EOL . implode(PHP_EOL, $containers);
echo $string;

Easy Install

wget https://files.programster.org/public/dockerps \
  && sudo mv dockerps /usr/bin/dockerps \
  && sudo chmod +x /usr/bin/dockerps

Combining with Less Command

If you wan't to pipe to less, then be sure to use -r parameter in order to gracefully handle the colors. E.g.

dockerps | less -r
Last updated: 5th September 2020
First published: 24th August 2020

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch