Hi everyone
From what I see, I’m more concerned with connectivity issues when we talk about using MongoDB in Docker. And I would like to be able to count on the help of the community.
I’m running the project in:
Docker version 24.0.2, build cb74dfc
macOS Monterey version 12.6.7
And I’m also having trouble connecting to the replica set running in a docker container. This project’s infrastructure has 3 nodes. And it has the default setting.
Option used to star my project : docker-compose up -d
`scripts % docker network ls
NETWORK ID NAME DRIVER SCOPE
e090044221b8 bridge bridge local
a16aa24d85b5 host host local
1060cad3b9ed none null local
08c22266be93 testcompose_mongodb_network bridge local`
`scripts % docker-compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
mongodb1 mongo:5.0 "docker-entrypoint.s…" mongodbsvr01 About an hour ago Up About an hour 27016/tcp, 0.0.0.0:27016->27017/tcp
mongodb2 mongo:5.0 "docker-entrypoint.s…" mongodbsvr02 About an hour ago Up About an hour 27018/tcp, 0.0.0.0:27018->27017/tcp
mongodb3 mongo:5.0 "docker-entrypoint.s…" mongodbsvr03 About an hour ago Up About an hour 27019/tcp, 0.0.0.0:27019->27017/tcp`
<houseadm@houseadms-iMac scripts % docker container inspect 0c64d3267a22 | grep 'IPAddress'
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.21.0.2",
houseadm@houseadms-iMac scripts % docker container inspect 280b455a3abe | grep 'IPAddress'
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.21.0.4",
houseadm@houseadms-iMac scripts % docker container inspect 0fe371970fe0 | grep 'IPAddress'
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.21.0.3",
about the configuration
** docker-compose.yml*
*version: "3.9"*
*services:*
* mongodbsvr01:*
* image: mongo:5.0*
* restart: unless-stopped*
* container_name: mongodb1*
* hostname: mongodb1*
* command: --bind_ip_all --replSet mongo-replica*
* # command: --replSet mongo-replica*
* environment:*
* DB: mongodb*
* networks:*
* mongodb_network:*
* # ipv4_address: 127.0.10.5*
* volumes:*
* - mgodb_data_1:/data/db*
* - mgodb_logs_1:/data/logs*
* expose:*
* - 27016*
* ports:*
* - 27016:27017*
* mongodbsvr02:*
* image: mongo:5.0*
* restart: always*
* container_name: mongodb2*
* hostname: mongodb2*
* command: --bind_ip_all --replSet mongo-replica*
* environment:*
* DB: mongodb*
* networks:*
* mongodb_network:*
* # ipv4_address: 127.0.10.2*
* volumes:*
* - mgodb_data_2:/data/db*
* - mgodb_logs_2:/data/logs*
* expose:*
* - 27018*
* ports:*
* - 27018:27017*
* mongodbsvr03:*
* image: mongo:5.0*
* restart: unless-stopped*
* container_name: mongodb3*
* hostname: mongodb3*
* command: --bind_ip_all --replSet mongo-replica*
* environment:*
* DB: mongodb*
* networks:*
* mongodb_network:*
* volumes:*
* - mgodb_data_3:/data/db*
* - mgodb_logs_3:/data/logs*
* - ./scripts:/scripts*
* expose:*
* - 27019*
* ports:*
* - 27019:27017*
* mongosetup: *
* image: mongo:5.0*
* restart: no*
* depends_on:*
* - mongodbsvr01*
* - mongodbsvr02*
* - mongodbsvr03*
* networks:*
* mongodb_network:*
* volumes:*
* - ./scripts:/scripts*
* environment:*
* DB: mongodb*
* entrypoint: [ "bash", "-c", "sh ./scripts/mongo_setup.sh"]*>
*volumes:*
* mgodb_data_1:*
* mgodb_logs_1:*
* mgodb_data_2:*
* mgodb_logs_2:*
* mgodb_data_3:*
* mgodb_logs_3:*
*networks:*
* mongodb_network:*
* driver: bridge*
Configuring replication
*#!/bin/bash*
*sleep 15*
*echo SETUP.sh time now: `date +"%T" `*
*mongo --host mongodb1:27017 <<EOF*
* var cfg = {*
* "_id": "mongo-replica",*
* "version": 1,*
* "members": [*
* {*
* "_id": 0,*
* "host": "mongodb1:27017",*
* "priority": 2*
* },*
* {*
* "_id": 1,*
* "host": "mongodb2:27017",*
* "priority": 1*
* },*
* {*
* "_id": 2,*
* "host": "mongodb3:27017",*
* "priority": 0*
* }*
* ]*
* };*
* rs.initiate(cfg, { force: true });*
* rs.reconfig(cfg, { force: true });*
* rs.secondaryOk();*
* db.getMongo().setReadPref('nearest');*
* db.getMongo().setSecondaryOk();*
* replSetStepDown(cfg, { force: true });*
*EOF*
Some tests
houseadm@houseadms-iMac scripts % docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c64d3267a22 mongo:5.0 "docker-entrypoint.s…" 51 minutes ago Up 51 minutes 27016/tcp, 0.0.0.0:27016->27017/tcp mongodb1
280b455a3abe mongo:5.0 "docker-entrypoint.s…" 51 minutes ago Up 51 minutes 27019/tcp, 0.0.0.0:27019->27017/tcp mongodb3
0fe371970fe0 mongo:5.0 "docker-entrypoint.s…" 51 minutes ago Up 51 minutes 27018/tcp, 0.0.0.0:27018->27017/tcp mongodb2
houseadm@houseadms-iMac scripts % docker exec -it mongodb2 mongosh --eval "rs.status()"
Current Mongosh Log ID: 64b768ce32f675388996d04f
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.1
Using MongoDB: 5.0.19
Using Mongosh: 1.10.1
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.
------
The server generated these startup warnings when booting
2023-07-19T03:45:52.242+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2023-07-19T03:45:53.510+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------
{
set: 'mongo-replica',
date: ISODate("2023-07-19T04:38:38.607Z"),
myState: 2,
term: Long("1"),
syncSourceHost: 'mongodb1:27017',
syncSourceId: 0,
heartbeatIntervalMillis: Long("2000"),
majorityVoteCount: 2,
writeMajorityCount: 2,
votingMembersCount: 3,
writableVotingMembersCount: 3,
optimes: {
lastCommittedOpTime: { ts: Timestamp({ t: 1689741509, i: 1 }), t: Long("1") },
lastCommittedWallTime: ISODate("2023-07-19T04:38:29.430Z"),
readConcernMajorityOpTime: { ts: Timestamp({ t: 1689741509, i: 1 }), t: Long("1") },
appliedOpTime: { ts: Timestamp({ t: 1689741509, i: 1 }), t: Long("1") },
durableOpTime: { ts: Timestamp({ t: 1689741509, i: 1 }), t: Long("1") },
lastAppliedWallTime: ISODate("2023-07-19T04:38:29.430Z"),
lastDurableWallTime: ISODate("2023-07-19T04:38:29.430Z")
},
lastStableRecoveryTimestamp: Timestamp({ t: 1689741499, i: 1 }),
electionParticipantMetrics: {
votedForCandidate: true,
electionTerm: Long("1"),
lastVoteDate: ISODate("2023-07-19T03:46:19.140Z"),
electionCandidateMemberId: 0,
voteReason: '',
lastAppliedOpTimeAtElection: { ts: Timestamp({ t: 1689738367, i: 1 }), t: Long("-1") },
maxAppliedOpTimeInSet: { ts: Timestamp({ t: 1689738367, i: 1 }), t: Long("-1") },
priorityAtElection: 1,
newTermStartDate: ISODate("2023-07-19T03:46:19.168Z"),
newTermAppliedDate: ISODate("2023-07-19T03:46:20.508Z")
},
members: [
{
_id: 0,
name: 'mongodb1:27017',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 3150,
optime: { ts: Timestamp({ t: 1689741509, i: 1 }), t: Long("1") },
optimeDurable: { ts: Timestamp({ t: 1689741509, i: 1 }), t: Long("1") },
optimeDate: ISODate("2023-07-19T04:38:29.000Z"),
optimeDurableDate: ISODate("2023-07-19T04:38:29.000Z"),
lastAppliedWallTime: ISODate("2023-07-19T04:38:29.430Z"),
lastDurableWallTime: ISODate("2023-07-19T04:38:29.430Z"),
lastHeartbeat: ISODate("2023-07-19T04:38:38.155Z"),
lastHeartbeatRecv: ISODate("2023-07-19T04:38:37.335Z"),
pingMs: Long("0"),
lastHeartbeatMessage: '',
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
electionTime: Timestamp({ t: 1689738379, i: 1 }),
electionDate: ISODate("2023-07-19T03:46:19.000Z"),
configVersion: 32556,
configTerm: -1
},
{
_id: 1,
name: 'mongodb2:27017',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 3166,
optime: { ts: Timestamp({ t: 1689741509, i: 1 }), t: Long("1") },
optimeDate: ISODate("2023-07-19T04:38:29.000Z"),
lastAppliedWallTime: ISODate("2023-07-19T04:38:29.430Z"),
lastDurableWallTime: ISODate("2023-07-19T04:38:29.430Z"),
syncSourceHost: 'mongodb1:27017',
syncSourceId: 0,
infoMessage: '',
configVersion: 32556,
configTerm: -1,
self: true,
lastHeartbeatMessage: ''
},
{
_id: 2,
name: 'mongodb3:27017',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 3150,
optime: { ts: Timestamp({ t: 1689741509, i: 1 }), t: Long("1") },
optimeDurable: { ts: Timestamp({ t: 1689741509, i: 1 }), t: Long("1") },
optimeDate: ISODate("2023-07-19T04:38:29.000Z"),
optimeDurableDate: ISODate("2023-07-19T04:38:29.000Z"),
lastAppliedWallTime: ISODate("2023-07-19T04:38:29.430Z"),
lastDurableWallTime: ISODate("2023-07-19T04:38:29.430Z"),
lastHeartbeat: ISODate("2023-07-19T04:38:38.187Z"),
lastHeartbeatRecv: ISODate("2023-07-19T04:38:38.186Z"),
pingMs: Long("0"),
lastHeartbeatMessage: '',
syncSourceHost: 'mongodb1:27017',
syncSourceId: 0,
infoMessage: '',
configVersion: 32556,
configTerm: -1
}
],
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1689741509, i: 1 }),
signature: {
hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
keyId: Long("0")
}
},
operationTime: Timestamp({ t: 1689741509, i: 1 })
}
scripts % docker exec -it mongodb2 mongosh --eval 'var m = db.isMaster(); print("Is Primary?", m.ismaster); print("hosts:", m.hosts); print("Primary:", m.primary)' --quiet
Is Primary? false
hosts: [ 'mongodb1:27017', 'mongodb2:27017' ]
Primary: mongodb1:27017
iMac scripts % mongosh --host mongodb2:27017 --eval 'var m = db.isMaster(); print("Is Primary?", m.ismaster); print("hosts:", m.hosts); print("Primary:", m.primary)' --quiet
MongoServerSelectionError: Server selection timed out after 30000 ms
Another when a try to connect using mongosh or connect to replica set
houseadm@houseadms-iMac scripts % mongosh "mongodb://mongodb1:27017,mongodb2:27017,mongodb3:27017/mongodb?replicaSet=mongo-replica"
Current Mongosh Log ID: 64b76a37fd5bf937a6cca38f
Connecting to: mongodb://mongodb1:27017,mongodb2:27017,mongodb3:27017/mongodb?replicaSet=mongo-replica&appName=mongosh+1.10.1
MongoServerSelectionError: Server selection timed out after 30000 ms
houseadm@houseadms-iMac testcompose % mongosh "mongodb://mongodb1:27017,mongodb2:27017,mongodb3:27017/database?replicaSet=mongo-replica"
Current Mongosh Log ID: 64b6930329ab4371a505f7b4
Connecting to: mongodb://mongodb1:27017,mongodb2:27017,mongodb3:27017/database?replicaSet=mongo-replica&appName=mongosh+1.10.1
MongoNetworkError: getaddrinfo ENOTFOUND mongodb1
An interesting point to share is that through Studio 3T, the connection via node is also successful, as well as via “docker exe”
I hope I managed to share all the procedures performed in search of a workaround solution. But without success…
Regards
Carlos