What ports am I using?

I've lost track of which ports are in use on my server. I'm not too worried about the ones used internally on the docker networks, but the ones that might interact or overlap with access to my services are a worry.

This isn't the shortest, nor the prettiest solution & it's Linux only, but you're welcome to it. I only need it occasionally so there's no real effort spare now that it works. It strips out the ip4 lines from the netstat output, removes the start & end of the lines, leaving only the port numbers, then sorts the result as numbers so you can see where you are.

 netstat -lnt | grep 0.0.0.0: | sed 's/\([^:]*\)\(.*\)/\2/;s/://;s/\(0.0.0.0:\)\(.*\)/\1/;s/\(  \)\(.*\)/\1/' | sort -n

Which docker containers are using which ports?

That first command shows what's being used on the server, some (or more) of those ports are being used by docker containers. (Internal ports in the docker networks aren't exposed elsewhere). You can list the ports being used by the docker containers using this

docker container ls --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}" -a

Of course, you can pipe this to grep to just see containers with ports that are exposed (I've dropped the container ID from the table from here on)

 docker container ls --format "table {{.Names}}\t{{.Ports}}" -a | grep '\->'

Or if you know which port is the problem you can check which container is using it (server port 2368 in this example)

docker container ls --format "table {{.Names}}\t{{.Ports}}" -a | grep '2368->'