Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

docker compose understanding the ports mapping

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading