Usefull Docker Scripts

Here are a couple of user docker powershell utility scripts

ConvertFrom-Docker.psm1 – this will take the output from something like

docker ps

and convert it to something like this

docker ps | ConvertFrom-Docker

ContainerId : fbf1955f00a9
Image       : portainer/portainer
Created     : About an hour ago
Status      : Up About an hour
Ports       : 0.0.0.0:9000->9000/tcp
Name        : portainer

Here is the code

function PascalName($name){
    $parts = $name.Split(" ")
    for($i = 0 ; $i -lt $parts.Length ; $i++){
        $parts[$i] = [char]::ToUpper($parts[$i][0]) + $parts[$i].SubString(1).ToLower();
    }
    $parts -join ""
}
function GetHeaderBreak($headerRow, $startPoint=0){
    $i = $startPoint
    while( $i + 1  -lt $headerRow.Length)
    {
        if ($headerRow[$i] -eq ' ' -and $headerRow[$i+1] -eq ' '){
            return $i
            break
        }
        $i += 1
    }
    return -1
}
function GetHeaderNonBreak($headerRow, $startPoint=0){
    $i = $startPoint
    while( $i + 1  -lt $headerRow.Length)
    {
        if ($headerRow[$i] -ne ' '){
            return $i
            break
        }
        $i += 1
    }
    return -1
}
function GetColumnInfo($headerRow){
    $lastIndex = 0
    $i = 0
    while ($i -lt $headerRow.Length){
        $i = GetHeaderBreak $headerRow $lastIndex
        if ($i -lt 0){
            $name = $headerRow.Substring($lastIndex)
            New-Object PSObject -Property @{ HeaderName = $name; Name = PascalName $name; Start=$lastIndex; End=-1}
            break
        } else {
            $name = $headerRow.Substring($lastIndex, $i-$lastIndex)
            $temp = $lastIndex
            $lastIndex = GetHeaderNonBreak $headerRow $i
            New-Object PSObject -Property @{ HeaderName = $name; Name = PascalName $name; Start=$temp; End=$lastIndex}
       }
    }
}
function ParseRow($row, $columnInfo) {
    $values = @{}
    $columnInfo | ForEach-Object {
        if ($_.End -lt 0) {
            $len = $row.Length - $_.Start
        } else {
            $len = $_.End - $_.Start
        }
        $values[$_.Name] = $row.SubString($_.Start, $len).Trim()
    }
    New-Object PSObject -Property $values
}

function ConvertFrom-Docker(){
    begin{
        $positions = $null;
    }
    process {
        if($positions -eq $null) {
            # header row => determine column positions
            $positions  = GetColumnInfo -headerRow $_  #-propertyNames $propertyNames
        } else {
            # data row => output!
            ParseRow -row $_ -columnInfo $positions
        }
    }
    end {
    }
}
  
Export-ModuleMember -Function * -Alias *
# e.g. :
# docker --tls ps -a --no-trunc | ConvertFrom-Docker | ft

.\Docker-Utils.psm1 – this is a set of utility functions and aliases that make life a littler easier.

docke-ps-ext # retrieve docket ps info with IP address

ContainerId : fbf1955f00a9
Image : portainer/portainer
Created : About an hour ago
Status : Up About an hour
Ports : 0.0.0.0:9000->9000/tcp
Name : portainerIPAddress : 172.22.232.8

Here is the code

function Docket-Get-IP() { param ([string] $containerId) return docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $containerId }

function Docker-List-IPAddress { 
  docker ps | ConvertFrom-Docker | % { 	
    $dc = New-Object PSObject -Property @{ ContainerId=""; Image=""; Created=""; Status=""; Ports=""; Name=""; IPAddress="" }
    
    $dc.ContainerId = $_.ContainerId
    $dc.Image = $_.Image
    $dc.Created = $_.Created
    $dc.Status = $_.Status
    $dc.Ports = $_.Ports
    $dc.Name = $_.Names
    $dc.IPAddress = (Docket-Get-IP($_.ContainerId))
    
    $dc | Select ContainerId, Image, Created, Status, Ports, Name, IPAddress
  }
}

Remove-Item docker-ips -ErrorAction SilentlyContinue
Set-Alias -Scope Global -Name docker-ps-ext -Value Docker-List-IPAddress