pylos
pylos

Reputation: 112

Docker Compose understanding the ports mapping

Docker newbie here. I am trying to understand the meaning of the following:

services:
  mongo1:
   hostname: mongo1
   container_name: mongo1
   image: mongo:5.0.6
   expose:
      - 27017
   ports:
     - 27011:27017
   restart: always

Note the ports: 27011:27017. When the Docker is up and running, I can access the MongoDB server via port 27011, so what is the 27017? And why do I need to expose it like the following?

expose:
  - 27017

Upvotes: 2

Views: 5135

Answers (1)

David Maze
David Maze

Reputation: 158977

expose: is a legacy implementation detail of first-generation Docker networking. It does almost nothing in current Docker, and it's always safe to remove it from Compose files. No other changes are required.

ports: describes how to map a port from the host system to a port in the container. The second port number is a fixed property of the image and is typically the "normal" port the container listens on; in your example MongoDB normally listens on port 27017 and the second port number must be exactly that. The first port number can be any otherwise-unused port on the host system.

Upvotes: 6

Related Questions