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:
- Verified MongoDB container is running and healthy
- Confirmed port 27017 is properly exposed in docker-compose.yaml
- Successfully connected using
docker exec -it mongo mongo(classic mongo shell works inside container) - Tested with older mongosh version (1.10.6) - same issue
- Attempted various mongosh connection parameters and timeouts
- Checked container logs - no authentication or networking errors
Working workarounds:
- Classic mongo shell works when executed inside the container
docker exec -it mongo mongoconnects 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"