Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do ports need to be specified twice separated by a colon?

A lot of times, I see ports described twice with a colon like in this Docker Compose file from the Docker Networking in Compose page:

version: "3"
services:

  web:
    build: .
    ports:
      - "8000:8000"
  db:
    image: postgres

networks:
  default:
    # Use a custom driver
    driver: custom-driver-1

I've often wondered why the "8000:8000" and not simply "8000"

Then I saw this example, which has the two ports different:

version: "3"
services:
  web:
    build: .
    ports:
      - "8000:8000"
  db:
    image: postgres
    ports:
      - "8001:5432"

Can someone explain what this port representation means?

like image 628
user3685285 Avatar asked May 26 '17 17:05

user3685285


People also ask

What does colon mean in port?

Founded in 1850 at the Atlantic (northern) terminus of the original Panama Railroad (now the Panama Canal Railway), the settlement was first called Aspinwall, named for one of the builders of the railway. Colón is the Spanish form of Columbus; the name of the neighbouring port of Cristóbal is Spanish for Christopher.

Is it possible to bind two containers on the same host port?

yes, it is possible as long as containers are using different IP address. you can check the IP address of the containers by using below command.


1 Answers

The first port is host's port and the second is the remote port (i.e: in the container). That expression bounds the remote port to the local port.

In the example you map container's 8080 port to host's 8080 port, but it's perfectly normal to use different ports (e.g: 48080:8080)

like image 69
Gonzalo Matheu Avatar answered Oct 22 '22 11:10

Gonzalo Matheu