When working with MongoDB in Docker, you might encounter connection issues like the "Connection reset by peer" error. This typically happens when the MongoDB shell fails to connect to the MongoDB server running in a Docker container.
The error message usually looks like this:
MongoDB shell version: 3.2.3
connecting to: test
2016-02-18T13:52:08.110-0700 I NETWORK [thread1] Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017
2016-02-18T13:52:08.110-0700 I NETWORK [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:27017]
2016-02-18T13:52:08.110-0700 E QUERY [thread1] Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:27017'
Several factors could cause this issue:
- Incorrect port mapping in Docker
- Network configuration issues
- Firewall blocking connections
- MongoDB not properly initialized
First, check if your MongoDB container is running properly:
docker ps -a
Then examine the container logs:
docker logs api
The correct way to run MongoDB in Docker is:
docker run --name mongodb -p 27017:27017 -d mongo
For more recent MongoDB versions, you might need to add authentication:
docker run --name mongodb \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password \
-p 27017:27017 \
-d mongo
To test the connection from your host machine:
mongo --host 127.0.0.1
Or with authentication:
mongo --host 127.0.0.1 -u admin -p password --authenticationDatabase admin
If you're experiencing network issues within the container:
Option 1: Use host networking
docker run --name mongodb --net=host -d mongo
Option 2: Create a custom bridge network
docker network create mongo-net
docker run --name mongodb --network mongo-net -p 27017:27017 -d mongo
For better management, use docker-compose.yml:
version: '3'
services:
mongodb:
image: mongo
container_name: mongodb
ports:
- "27017:27017"
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
volumes:
- mongodb_data:/data/db
volumes:
mongodb_data:
To check network connectivity from inside the container:
docker exec -it mongodb bash
apt-get update && apt-get install -y iputils-ping
ping google.com
If you can't connect to the internet from the container, check your Docker network settings and DNS configuration.
Before concluding:
- Verify MongoDB is listening on the correct port inside the container
- Check if the port is properly exposed and mapped
- Ensure no other service is using port 27017 on your host
- Test with different MongoDB client versions
When running MongoDB in Docker, you might encounter connection issues where the MongoDB shell fails to connect with errors like:
Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017
SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR]
First, let's verify the basic Docker run command:
docker run --name mongo-container -p 27017:27017 -d mongo
Check if the container is running properly:
docker ps
docker logs mongo-container
1. Network Configuration Issues
Docker's default networking might cause connectivity problems. Try these alternatives:
# Option 1: Use host networking
docker run --name mongo-container --net=host -d mongo
# Option 2: Create a custom bridge network
docker network create mongo-net
docker run --name mongo-container --network mongo-net -p 27017:27017 -d mongo
2. Firewall and Port Conflicts
Check for port conflicts and firewall settings:
# Check port usage
netstat -tuln | grep 27017
# Temporarily disable firewall for testing
sudo ufw disable
When basic solutions don't work, try these debugging steps:
# Inspect container network configuration
docker inspect mongo-container | grep IPAddress
# Test connectivity from host to container
telnet 127.0.0.1 27017
# Check MongoDB logs inside container
docker exec -it mongo-container bash
cat /var/log/mongodb/mongod.log
For production environments, consider these best practices:
docker run --name mongo-production \
-p 27017:27017 \
-v /path/to/data:/data/db \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=secret \
--restart unless-stopped \
-d mongo:latest
Test your connection using various MongoDB clients:
Using MongoDB Shell:
mongo --host 127.0.0.1 --port 27017
Using Node.js Driver:
const { MongoClient } = require('mongodb');
const uri = "mongodb://127.0.0.1:27017";
const client = new MongoClient(uri);
async function run() {
try {
await client.connect();
console.log("Connected successfully to server");
} finally {
await client.close();
}
}
run().catch(console.dir);
- Verify Docker container is running
- Check port mapping is correct
- Test network connectivity
- Review MongoDB logs
- Try different connection methods