When using the appropriate compose format, hostnames resolve automagically via dockers internal container network dns.

pull/117/head
geethree 2018-03-26 18:26:21 -07:00
parent 6197d981d8
commit ac9332f374
2 changed files with 27 additions and 50 deletions

View File

@ -1,43 +1,25 @@
# This will boot 4 containers: 3 mongo as a cluster, and 1 to issue mongo commands to others # This will boot 4 containers: 3 mongo as a cluster, and 1 to issue mongo commands to others
# #
version: "2.2"
services:
mongo3:
image: mongo:3.2
restart: always
entrypoint: [ "/usr/bin/mongod", "--replSet", "rs", "--rest", "--httpinterface" ]
mongo3: mongo2:
image: mongo:3.0 image: mongo:3.2
hostname: mongo3 restart: always
expose: entrypoint: [ "/usr/bin/mongod", "--replSet", "rs", "--rest", "--httpinterface" ]
- "27017"
- "28017"
restart: always
entrypoint: [ "/usr/bin/mongod", "--replSet", "rs", "--rest", "--httpinterface" ]
mongo2: mongo1:
image: mongo:3.0 image: mongo:3.0
hostname: mongo2 restart: always
expose: entrypoint: [ "/usr/bin/mongod", "--replSet", "rs", "--rest", "--httpinterface" ]
- "27017"
- "28017"
restart: always
entrypoint: [ "/usr/bin/mongod", "--replSet", "rs", "--rest", "--httpinterface" ]
mongo1: mongosetup:
image: mongo:3.0 image: mongo:3.0
hostname: mongo1 volumes:
expose: - ./scripts:/scripts
- "27017" restart: always
- "28017" entrypoint: [ "bash", "/scripts/mongosetup.sh" ]
links:
- mongo2:mongo2
- mongo3:mongo3
restart: always
entrypoint: [ "/usr/bin/mongod", "--replSet", "rs", "--rest", "--httpinterface" ]
mongosetup:
image: mongo:3.0
links:
- mongo1:mongo1
- mongo2:mongo2
- mongo3:mongo3
volumes:
- ./scripts:/scripts
restart: always
entrypoint: [ "bash", "/scripts/mongosetup.sh" ]

View File

@ -1,39 +1,34 @@
#!/bin/bash #!/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 "Waiting for startup.." echo "Waiting for startup.."
until curl http://${MONGODB1}:28017/serverStatus\?text\=1 2>&1 | grep uptime | head -1; do until curl http://mongo1:28017/serverStatus\?text\=1 2>&1 | grep uptime | head -1; do
printf '.' printf '.'
sleep 1 sleep 1
done done
echo curl http://${MONGODB1}:28017/serverStatus\?text\=1 2>&1 | grep uptime | head -1 echo curl http://mongo1:28017/serverStatus\?text\=1 2>&1 | grep uptime | head -1
echo "Started.." echo "Started.."
sleep 10 sleep 10
echo SETUP.sh time now: `date +"%T" ` echo SETUP.sh time now: `date +"%T" `
mongo --host ${MONGODB1}:27017 <<EOF mongo --host mongo1:27017 <<EOF
var cfg = { var cfg = {
"_id": "rs", "_id": "rs",
"version": 1, "version": 1,
"members": [ "members": [
{ {
"_id": 0, "_id": 0,
"host": "${MONGODB1}:27017", "host": "mongo1:27017",
"priority": 2 "priority": 2
}, },
{ {
"_id": 1, "_id": 1,
"host": "${MONGODB2}:27017", "host": "mongo2:27017",
"priority": 0 "priority": 0
}, },
{ {
"_id": 2, "_id": 2,
"host": "${MONGODB3}:27017", "host": "mongo3:27017",
"priority": 0 "priority": 0
} }
] ]
@ -43,4 +38,4 @@ mongo --host ${MONGODB1}:27017 <<EOF
db.getMongo().setReadPref('nearest'); db.getMongo().setReadPref('nearest');
EOF EOF
ping 127.0.0.1 > /dev/null tail -f /dev/null