Home > Net >  docker compose understanding the ports mapping
docker compose understanding the ports mapping

Time:10-17

docker newbie here. 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 mongo via port 27011, so what is the 27017? and why do I need to expose it:

expose:
  - 27017

CodePudding user response:

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.

  • Related