brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: docker port shows which host ports a container publishes

Docker
~/tips/tip-docker-port-published-ports

docker port mycontainer

Prints the host address and port mapped to each published port of a container.

After a while I forget which container sits behind which port on the host. Instead of scrolling through docker ps output, I ask the container itself.

docker port mycontainer
docker port mycontainer 80

Without a port the command lists every published mapping, one per line, such as 80/tcp -> 0.0.0.0:8080. With a container port as an argument it prints only that mapping. When you let Docker pick a random host port with -p 80, this is the quickest way to find out which one it took.

For Compose the same idea is docker compose port db 5432, which asks for the mapping of a service instead of a container name.

Look at the address part. 0.0.0.0 means the port is reachable on every interface of the host. If the service should only be used locally, publish it as 127.0.0.1:8080:80 and run the command again to confirm it changed.

The caveat is scope: docker port only knows ports that were published with -p or -P. A port that is merely declared with EXPOSE is documentation and does not appear. Containers on --network host publish nothing either, so the output is empty even though the service is listening.

Further reading: Port publishing and mapping

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link