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

Viewed 4652

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?

2 Answers

If the 'host' port and the ':' of the publish port is omitted, eg. 'docker run -d -p 3000 myimage'. Docker will auto assign a (high number) host port for you. You can check to see it by running 'docker ps'.

Related