I'm trying to create a more streamlined version of the "docker ps" output for my terminal, especially since I often work with limited width. I've managed to set up an alias that shows the container names, images, statuses, and ports, but the ports section is cluttered with IP addresses. Currently, the output looks like this: `0.0.0.0:34400->34400/tcp, :::34400->34400/tcp`. Since I don't use IPv6 and all my ports are forwarded for any IP, this extra information just takes up space. What I'm hoping for is a cleaner output, like just `34400->34400/tcp`. The Docker formatting options seem pretty limited and I don't see an easy way to manipulate this directly. Is there any way to achieve the desired output using the format switch, or will I need to resort to using regex or something like that?
3 Answers
You might want to consider using `sed` to clean up the output. You can pipe the output of your `docker ps` command into `sed` to remove the unwanted IP addresses. For instance, this alias should help:
```bash
alias dps='docker ps --format "table {{.Names}}t{{.Image}}t{{.Status}}t{{.Ports}}" | ( read -r; printf "%sn" "$REPLY"; sort -k1 ) | sed -E "s/([0-9]{1,3}.){3}[0-9]{1,3}:|::://g"'
```
Give this a try!
Check out this script I wrote. It’s not exactly what you’re looking for, but it shows used ports system-wide. You could adapt it to just list the ports for each container. Here’s the link: https://github.com/telnetdoogie/synology-scripts/blob/main/showports.sh
Another option is to just use the `awk` command instead of regex. Depending on how your output is structured, you could try something simpler like filtering out the parts you don't want. But really, the `sed` solution is probably your best bet.
Thanks for the suggestion! I'll consider both `sed` and `awk` to see which one works better for my setup.
Thanks, I totally spaced on using sed! I'm still learning the ins and outs of Linux.