Skip to content

Instantly share code, notes, and snippets.

@theit8514
Created May 10, 2018 14:52
Show Gist options
  • Select an option

  • Save theit8514/2221c2ec7a65d3d5c9e68ca635a35af2 to your computer and use it in GitHub Desktop.

Select an option

Save theit8514/2221c2ec7a65d3d5c9e68ca635a35af2 to your computer and use it in GitHub Desktop.
Docker status report script
#!/bin/bash
# This script will run a 'docker ps' comamnd, passing through any arguments, and then run a 'docker inspect' on
# each container to get the bound port information. Exposed but unbound ports are not shown (unlike 'docker ps').
# It also filters out the host '0.0.0.0' since this is the assumed default value for '-p'. It makes the output much cleaner.
# If the docker container is configured with --net that will also be displayed.
# Requires the 'column' command. On my Synology, I get that with `sudo opkg install column`.
#NAMES IMAGE STATUS PORTS
list_names=('NAMES' '-----')
list_image=('IMAGE' '-----')
list_status=('STATUS' '------')
list_ports=('PORTS' '-----')
while IFS=$'\t' read -r id names image status ; do
list_names+=("$names")
list_image+=("$image")
list_status+=("$status")
ports=$(docker inspect --format='{{if ne .HostConfig.NetworkMode "default"}}--net={{.HostConfig.NetworkMode}}{{end}}{{range $p, $conf := .NetworkSettings.Ports}}{{ if $conf }}, {{if ne (index $conf 0).HostIp "0.0.0.0"}}{{(index $conf 0).HostIp}}:{{end}}{{(index $conf 0).HostPort}}:{{$p}}{{end}}{{end}}' $id | sed 's/^, //')
list_ports+=("$ports")
done < <(docker ps --format '{{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Status}}' $@)
# get length of an array
tLen=${#list_names[@]}
# loop through all the items in the array and output the contents in a table
for (( i=0; i<${tLen}; i++ ))
do
echo ${list_names[$i]} $'\x1d' ${list_image[$i]} $'\x1d' ${list_status[$i]} $'\x1d' "${list_ports[$i]}"
done | column -t -s$'\x1d'
$ docker-status
NAMES IMAGE STATUS PORTS
----- ----- ------ -----
homeassistant homeassistant/home-assistant Up 2 days --net=host
unbound unbound-custom Up 2 weeks 53:53/tcp, 53:53/udp
mqtt 2bytes/mqtt:1.0.2 Up 4 weeks --net=mosquittodockermaster_default, 1883:1883/tcp, 2883:2883/tcp, 127.0.0.1:3883:3883/tcp
$ docker-status -a
NAMES IMAGE STATUS PORTS
----- ----- ------ -----
elated_noyce ubuntu Created
homeassistant homeassistant/home-assistant Up 2 days --net=host
unbound unbound-custom Up 2 weeks 53:53/tcp, 53:53/udp
mqtt 2bytes/mqtt:1.0.2 Up 4 weeks --net=mosquittodockermaster_default, 1883:1883/tcp, 2883:2883/tcp, 127.0.0.1:3883:3883/tcp
$ docker start elated_noyce
elated_noyce
$ docker-status -a
NAMES IMAGE STATUS PORTS
----- ----- ------ -----
elated_noyce ubuntu Exited (0) 2 seconds ago
homeassistant homeassistant/home-assistant Up 2 days --net=host
unbound unbound-custom Up 2 weeks 53:53/tcp, 53:53/udp
mqtt 2bytes/mqtt:1.0.2 Up 4 weeks --net=mosquittodockermaster_default, 1883:1883/tcp, 2883:2883/tcp, 127.0.0.1:3883:3883/tcp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment