Skip to content

Comments

fix: decouple MONGODB_PORT_NUMBER from internal container port#25

Open
BrahimAfa wants to merge 1 commit intoRocketChat:mainfrom
BrahimAfa:fix/mongodb-port-mapping
Open

fix: decouple MONGODB_PORT_NUMBER from internal container port#25
BrahimAfa wants to merge 1 commit intoRocketChat:mainfrom
BrahimAfa:fix/mongodb-port-mapping

Conversation

@BrahimAfa
Copy link

@BrahimAfa BrahimAfa commented Feb 20, 2026

Problem

Setting MONGODB_PORT_NUMBER to a non-default value (e.g. 49335) in .env breaks MongoDB replica set initialization and causes Rocket.Chat to crash on startup.

Root cause: MONGODB_PORT_NUMBER is used for both the host port and the container port in three places:

  1. Port mapping (line 128): ${MONGODB_PORT_NUMBER}:${MONGODB_PORT_NUMBER} — maps host:container to the same value
  2. Healthcheck (line 120): connects to mongodb:${MONGODB_PORT_NUMBER} via the Docker network
  3. Init container (line 60): registers the replica set member at mongodb:${MONGODB_PORT_NUMBER}

However, mongod is started without a --port flag (line 99), so it always listens on port 27017 inside the container. When MONGODB_PORT_NUMBER is anything other than 27017, all three references point to a port nothing is listening on.

Steps to reproduce

  1. Clone the repo and set MONGODB_PORT_NUMBER=49335 (or any non-default value) in .env
  2. Run docker compose -f compose.database.yml -f compose.yml up -d
  3. Check mongodb-init-container logs:
    MongoServerError: No host described in new configuration with
    {version: 1, term: 0} for replica set rs0 maps to this node
    
  4. Check rocketchat logs:
    MongoTopologyClosedError: Topology is closed
    MongoServerSelectionError: Server selection timed out after 30000 ms
    reason: TopologyDescription { type: 'ReplicaSetNoPrimary', ... }
    

Fix

Hardcode the internal container port to 27017 (matching mongod's default) and keep MONGODB_PORT_NUMBER only for the host-side mapping. This is consistent with how NATS_PORT_NUMBER already works in this project:

ports:
  - "${NATS_BIND_IP:-127.0.0.1}:${NATS_PORT_NUMBER:-4222}:4222"
                                   

…ernal port to 27017

MONGODB_PORT_NUMBER was used for both the host port and the container
port in the MongoDB port mapping, healthcheck, and replica set init.
When set to a non-default value, mongod still starts on 27017 (no
--port flag), but the healthcheck, replica set init, and port mapping
all reference the custom port — causing replica set initialization to
fail with "No host described in new configuration maps to this node"
and Rocket.Chat to crash with MongoTopologyClosedError /
ReplicaSetNoPrimary.

Hardcode the internal container port to 27017 (matching mongod's
default) and keep MONGODB_PORT_NUMBER for the host mapping only,
consistent with how NATS_PORT_NUMBER already works.
@CLAassistant
Copy link

CLAassistant commented Feb 20, 2026

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants