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,42 +1,24 @@
# 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"
mongo3: services:
image: mongo:3.0 mongo3:
hostname: mongo3 image: mongo:3.2
expose:
- "27017"
- "28017"
restart: always restart: always
entrypoint: [ "/usr/bin/mongod", "--replSet", "rs", "--rest", "--httpinterface" ] entrypoint: [ "/usr/bin/mongod", "--replSet", "rs", "--rest", "--httpinterface" ]
mongo2: mongo2:
image: mongo:3.0 image: mongo:3.2
hostname: mongo2
expose:
- "27017"
- "28017"
restart: always restart: always
entrypoint: [ "/usr/bin/mongod", "--replSet", "rs", "--rest", "--httpinterface" ] entrypoint: [ "/usr/bin/mongod", "--replSet", "rs", "--rest", "--httpinterface" ]
mongo1: mongo1:
image: mongo:3.0 image: mongo:3.0
hostname: mongo1
expose:
- "27017"
- "28017"
links:
- mongo2:mongo2
- mongo3:mongo3
restart: always restart: always
entrypoint: [ "/usr/bin/mongod", "--replSet", "rs", "--rest", "--httpinterface" ] entrypoint: [ "/usr/bin/mongod", "--replSet", "rs", "--rest", "--httpinterface" ]
mongosetup: mongosetup:
image: mongo:3.0 image: mongo:3.0
links:
- mongo1:mongo1
- mongo2:mongo2
- mongo3:mongo3
volumes: volumes:
- ./scripts:/scripts - ./scripts:/scripts
restart: always restart: always

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