How to find out which port a docker container is running on

There are moments you might just want to know which port is exported by a docker container. Especially when you just run a docker image using docker run you might not explicitly define the ports. But which port is open on the container then? The image has its own configuration to define the ports.

You can follow two ways to find out which port was opened:

Container list with docker container ls #

The container overview containers the ports. You could run this command to get a list of containers:

$ docker container ls

Running this command will return a list of containers on your system with their opened ports:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c1bd2cf0ca37 brettt89/silverstripe-web:7.4-fpm "docker-php-entrypoiโ€ฆ" 7 minutes ago Up 7 minutes 9000/tcp

There you can see that port 9000 was opened.

Inspect container with docker inspect #

You can also inspect the container to get the port directly and get a list of all opened ports as output:

$ docker inspect --format='' c1bd2cf0ca37

prints:

map[9000/tcp:{}]

This uses the format argument to filter the port out.

Inspired by this Stackoverflow question.

๐Ÿ™๐Ÿ™๐Ÿ™

Since you've made it this far, sharing this article on your favorite social media network would be highly appreciated ๐Ÿ’–! For feedback, please ping me on Twitter.

Published