Skip to content

Instantly share code, notes, and snippets.

@GottZ
Last active Sep 22, 2020
Embed
What would you like to do?
/etc/profile.d/dps.sh
# Docker PS prettifier
# revision 6
# https://gist.github.com/GottZ/4a6c2af314d73cd8b71d
dps() {
docker ps $@ --format "table{{ .Image }}\\t{{ .Names }}\\t{{ .Status }}\\t{{ .Ports }}" | awk '
NR % 2 == 0 {
printf "\033[0m";
}
NR % 2 == 1 {
printf "\033[1m";
}
NR == 1 {
PORTSPOS = index($0, "PORTS");
PORTS = "PORTS";
PORTSPADDING = "\n";
for(n = 1; n < PORTSPOS; n++)
PORTSPADDING = PORTSPADDING " ";
}
NR > 1 {
PORTS = substr($0, PORTSPOS);
gsub(/, /, PORTSPADDING, PORTS);
}
{
printf "%s%s\n", substr($0, 0, PORTSPOS - 1), PORTS;
}
END {
printf "\033[0m";
}
'
}
dpsa() { dps -a $@; }
@GottZ
Copy link
Author

GottZ commented Oct 23, 2015

thats actually my first awk script.. so ye.. if you have some feedback.. i'd be happy 😄

its linked to this issue: moby/moby#7477

@aanm
Copy link

aanm commented Oct 24, 2015

@GottZ remove sudo. You can run sudo usermod -aG docker $your_user so you don't have to run sudo with docker

@GottZ
Copy link
Author

GottZ commented Oct 24, 2015

i wrote this on the fly and didn't think about that. thanks!

@chiefy
Copy link

chiefy commented Jun 2, 2016

adding this to my dotfiles, thanks @GottZ

@ko-lem
Copy link

ko-lem commented Oct 24, 2016

Just found this. I have something similar where my focus is just to reduce the columns and sort the results by name.

  docker ps -a --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}" \
     | awk 'NR == 1; NR > 1 { print $0 | "sort" }'

Recently had encountered multiple port bindings for a container so just piped my result to your awk script. Thanks!

@andyheath
Copy link

andyheath commented Jan 20, 2018

Following on from @lemonkoala's lead I wrote a small bash script to do the messy stuff. It takes a list of options (e.g. -iIcClmnNprSshL), constructs the go string and invokes the docker ps with it. In case its useful to anyone its here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment