How to have duplicate Port (I'm using different network)

Dear folks,
I’m using PostgreSQL for my company project, which is configured by them, and uses the default port.
And I also configured the SonarQube with standalone database (PostgreSQL). I also defined an network for it. and in the end, I only used the default port for internal, but used another port number for external port.
Still it says the port are duplicated.

Any solution?

Thank you,
Hassan F.

version: "3.9"
name: Sonar
services:
  postgres:
    image: postgres:latest
    container_name: sonar.postgres
    ports: 
      - "5432:65432"
    networks:
      - sonarnet
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=Pa$$word123
    volumes:
      - sonar_postgresql:/var/lib/postgresql
      - sonar_postgresql_data:/var/lib/postgresql/data
    deploy:
      restart_policy:
        condition: unless-stopped

  sonarqube:
    image: sonarqube:latest
    container_name: sonarqube
    ports: 
      - "9000:9000"
    networks:
      - sonarnet
    environment: #internal port
      - sonar.jdbc.url=jdbc:postgresql://postgres:5432/postgres
      - sonar.jdbc.username=sonar
      - sonar.jdbc.password=Pa$$word123
      - sonar.search.javaAdditionalOpts=-Dnode.store.allow_mmap=false
    volumes:
      - sonarqube_conf:/opt/sonarqube/conf
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
    deploy:
      restart_policy:
        condition: unless-stopped
    depends_on:
      - postgres
      
networks:
  sonarnet:
    driver: bridge
    
volumes:
  sonarqube_conf:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:
  sonar_postgresql:
  sonar_postgresql_data:

You did the opposite!

Left hand side before the colon character: host port.
Right hand side after the colon character: container port.

You can not bind a host port to more than one process, regardless whether it runs isolated in a container or native on the host. You can run multiple containers with the same container port (unless they use --network=host).

Oh thank you, since it’s container config, I always expect to map container to host, not host to container…
But if it’s container part, why postgres uses the 5432 instead of 65432…?

The port mapping does not influence on which port a service inside a container runs. It is merely a port forwarding from a host port to a private container ip port. It is usually a good idea to look at the compose specification if you are uncertain about how to configure a specific configuration element.

If you want the service inside the container to bind to a different port you will need to configure it (if possible). Some images provide environment variables to change the port, some have a config file in a volume that could be manually changed and some simply don’t allow it at all.

I often don’t even publish/map ports for a postgres container, as I don’t plan to access them with any external tools. Containers in the same (docker) network can access the database without using published ports.