I’m a fan of docker, especially with my brand new macbook - spinning up loads of containers is always fun 😀. However when listing the containers, things get a bit unclear. Check the following output:
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
d52c11b67cfe docker.elastic.co/elasticsearch/elasticsearch:7.13.2 "/bin/tini -- /usr/l…" 2 hours ago Up 2 hours
0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp vb-bo_elasticsearch_1
76fee05889f0 logstash:7.13.2 "/usr/local/bin/dock…" 2 hours ago Up 50 minutes
0.0.0.0:5044->5044/tcp, :::5044->5044/tcp, 0.0.0.0:9600->9600/tcp, :::9600->9600/tcp vb-bo_logstash_1
e18d279ed438 mysql/mysql-server:latest "/entrypoint.sh mysq…" 2 hours ago Up 2 hours (healthy)
0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060-33061/tcp vb-bo_mysql_1
See what I mean? Columnnames floating to the next line, a lot of white space making it not so readable.
Recently I got more invested with AWK, and wanted to give this a try and I came up with this little gem.
{print $2, $5}
END{
print "\n",NR-1, "up and running"
}
This actually means: print the second and fifth column (Mind you: this is not zero-index based). Also you need a special delimiter, so tying everthing together you get:
docker ps | awk -F '( )+' '{print $2, $5} END{print "\n",NR-1, "up and running"}'
Now you get this cleaner output with the final line being the summary of the number of running containers
IMAGE STATUS
docker.elastic.co/elasticsearch/elasticsearch:7.13.2 Up 2 hours
logstash:7.13.2 Up 58 minutes
mysql/mysql-server:latest Up 2 hours (healthy)
3 up and running
Of course, if you don’t want to dip your toe in AWK, docker ps
now also supports the --format
switch.
docker ps --format "table {{.Names}} {{.Status}}"