A customised docker ps with some extra information. Some techniques are used: (1) use #xxx
as inline comments (2) use sed to filter lines (3) use awk to add new columns (4) use docker ps -format to get ps info we need (5) pass a bash variable to awk with -v option (6) force awk and column to use 'tab' as seperator instead of 'space'
#!/bin/bash
# Get ip address of this machine
ipAddress="$(/sbin/ip -o -4 addr list eth0 2> /dev/null | awk '{print $4}' | cut -d/ -f1)"
if [ "$ipAddress"=="" ]; then
ipAddress="$(/sbin/ip -o -4 addr list enp0s3 2> /dev/null | awk '{print $4}' | cut -d/ -f1)"
fi
# List containers
(
echo 'ID Image Created Status Ports Names';
docker ps --format '{{.ID}}\t{{.Image}}\t{{.CreatedAt}}\t{{.Status}}\t{{.Ports}}\t{{.Names}}'
) \
| sed -n '1p;/xb/p;/xrs/p' `#filter relavent lines` \
| sed -E 's/80\/tcp(, )/..., /g' `#hide port 80 info` \
| sed -E "s/443\/tcp(, )/..., /g" `#hide port 443 info` \
| sed -E "s/(..., )+/..., /g" `#merge multiple...` \
| sed -E "s/ \+0800 CST//g" `#remove time zone info` \
| awk -F '\t' -v ipAddress="$ipAddress" 'BEGIN {
}
{
if (NR==1) {
print $0 "\tServiceUrl";
} else {
printf $0;
where = match($5, /[0-9]+->5000\/tcp/);
if (where) {
wholePortExp = substr($5, RSTART, RLENGTH);
where = match(wholePortExp, /^[0-9]+/);
if (where) {
externalPort = substr(wholePortExp, RSTART, RLENGTH);
}
}
if (externalPort) {
printf("\thttp://%s:%s/api/v1.0\n", ipAddress, externalPort);
}
}
}' \
| column -t -s $'\t'