I am using mongo server in which there are three dockers that are running and another one which maps all the three mongo dockers and creates replica from them. Config for same is:
mongo1:
build: mongodb/
hostname: mongo1
expose:
- "27017"
ports:
- "ip:27017:27017"
# links:
# - mongo2:mongo2
# - mongo3:mongo3
restart: always
volumes:
- ./data/mongodb/mongo1:/data/db
entrypoint: [ "/usr/bin/mongod", "--replSet=rs", "--noauth", "--rest", "--httpinterface", "--dbpath", "/data/db","--port","27017","--bind_ip","0.0.0.0" ]
mem_limit: 1024m
mongo2:
build: mongodb/
hostname: mongo2
expose:
- "27018"
ports:
- "ip:27018:27017"
restart: always
volumes:
- ./data/mongodb/mongo2:/data/db
entrypoint: [ "/usr/bin/mongod", "--replSet=rs", "--rest", "--httpinterface", "--dbpath", "/data/db","--port","27017","--bind_ip","0.0.0.0" ]
# mem_limit: 512m
mongo3:
build: mongodb/
hostname: mongo3
expose:
- "27019"
ports:
- "ip:27019:27017"
restart: always
volumes:
- ./data/mongodb/mongo3:/data/db
entrypoint: [ "/usr/bin/mongod", "--replSet=rs", "--rest", "--httpinterface", "--dbpath", "/data/db","--port","27017","--bind_ip","0.0.0.0" ]
# mem_limit: 512m
where ip is my server ip. And the setup file which set replica is:
mongosetup:
build: mongodb/
links:
- mongo1:mongo1
- mongo2:mongo2
- mongo3:mongo3
volumes:
- ./mongodb/scripts:/scripts
restart: always
entrypoint: [ "bash", "/scripts/mongosetup.sh" ]
mem_limit: 256m
Script mongosetup.sh:
#!/bin/bash
MONGODB1=`ping -c 1 mongo1 | head -1 | cut -d "(" -f 2 | cut -d ")" -f 1`
MONGODB2=`ping -c 1 mongo2 | head -1 | cut -d "(" -f 2 | cut -d ")" -f 1`
MONGODB3=`ping -c 1 mongo3 | head -1 | cut -d "(" -f 2 | cut -d ")" -f 1`
echo SETUP.sh time now: `date +"%T" `
mongo --host ${MONGODB1}:27017 <<
var cfg = {
"_id": "rs",
"version": 1,
"members": [
{
"_id": 0,
"host": "${MONGODB1}:27017",
"priority": 2
},
{
"_id": 1,
"host": "${MONGODB2}:27018",
"priority": 0
},
{
"_id": 2,
"host": "${MONGODB3}:27019",
"priority": 0
}
]
};
rs.initiate(cfg, { force: true });
rs.reconfig(cfg, { force: true });
db.getMongo().setReadPref('nearest');
Now when I am trying to connect from the test server (which have all these dockers) its working. Connection url for same from my node js server:
var uri = "mongodb://ip:27017,ip:27018,ip:27019/stagedb/?replicaSet=rs";
mongoose.connect(uri);
where ip is my test server ip.
Also I tried to connect from mongo shell as
mongo --host rs/ip:27017,ip:27018,ip:27019 stagedb
But when trying to connect test server mongo from remote server with same connection string as above its throwing error:
Database connection error MongoError: failed to connect to server [172.17.0.5:27017] on first connect [MongoError: connection 71 to 172.17.0.5:27017 timed out]
The IP 172.17.0.5 is of docker while I am trying to connect to test server ip.
Also from local mongo shell its giving error as:
2017-06-12T14:22:55.531+0530 W NETWORK Failed to connect to 172.17.0.2:27017 after 5000 milliseconds, giving up.
2017-06-12T14:23:00.535+0530 W NETWORK Failed to connect to 172.17.0.5:27017 after 5000 milliseconds, giving up.
2017-06-12T14:23:05.495+0530 W NETWORK [ReplicaSetMonitorWatcher] Failed to connect to 172.17.0.3:27017 after 5000 milliseconds, giving up.
2017-06-12T14:23:05.498+0530 E QUERY Error: connect failed to replica set rs/test.elanic.co:27017,test.elanic.co:27018,test.elanic.co:27019 at connect (src/mongo/shell/mongo.js:181:14) at (connect):1:6 at src/mongo/shell/mongo.js:181 exception: connect failed
Anyone have idea why its trying to map directly to docker ip or any other issue.