Find the public port for a specific container index
In some advanced scenarios, a container might expose the same internal port multiple times, potentially mapped to different host ports or interfaces. While less common for simple services like Nginx, it's possible. In such cases, the docker port
command can return multiple mappings for a single internal port.
To retrieve a specific mapping when multiple exist, you can use an index. The output of docker port
for a given internal port is a list of mappings. You can access a specific mapping by its index, starting from 0.
Let's assume, for demonstration purposes, that our Nginx container somehow had multiple mappings for port 80. To get the first mapping (index 0), you would use the following command format:
docker port [CONTAINER_ID] [CONTAINER_PORT]/[PROTOCOL]/[INDEX]
Using our Nginx example, to get the first mapping for internal port 80 TCP, you would use:
docker port [CONTAINER_ID] 80/tcp/0
Replace [CONTAINER_ID]
with the actual ID of your Nginx container.
Since our Nginx container only has one mapping for port 80/tcp, this command will return the same output as docker port [CONTAINER_ID] 80/tcp
, which is 0.0.0.0:8080
.
While this specific example with Nginx might not show the full power of indexing (as there's only one mapping), understanding this capability is important for more complex container configurations where multiple mappings for the same internal port might exist.