MongoDB 4.4.5 in Docker: mongosh connection fails with "connection <monitor> to 127.0.0.1:27017 closed"

I’m running MongoDB 4.4.5 in Docker using docker-compose and experiencing connection issues when trying to connect from the host using mongosh.

Environment:

  • MongoDB: 4.4.5 (Docker image: mongo:4.4.5)
  • Host OS: Linux
  • mongosh version: 2.5.5 (latest)
  • Docker Compose configuration: Port 27017 exposed correctly

Problem: When attempting to connect from the host using mongosh mongodb://localhost:27017, the connection fails with:

connection <monitor> to 127.0.0.1:27017 closed

What I’ve tried:

  1. Verified MongoDB container is running and healthy
  2. Confirmed port 27017 is properly exposed in docker-compose.yaml
  3. Successfully connected using docker exec -it mongo mongo (classic mongo shell works inside container)
  4. Tested with older mongosh version (1.10.6) - same issue
  5. Attempted various mongosh connection parameters and timeouts
  6. Checked container logs - no authentication or networking errors

Working workarounds:

  • Classic mongo shell works when executed inside the container
  • docker exec -it mongo mongo connects successfully

Question: Is this a known compatibility issue between modern mongosh clients and MongoDB 4.4.5? Are there specific configuration changes needed for Docker networking or MongoDB settings to allow external mongosh connections to work properly?

version: '3'

services:
  # Mongodb
  mongo:
    image: mongo:4.4.5
    container_name: mongo
    restart: on-failure
    volumes:
      - ./.data/mongodb:/data/db
    ports:
      - "27017:27017"
1 Like

Sounds more like an application version issue than a Docker issue.

Either you try to use a matching client version or you try for testing to run a latest client and server version.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.