Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jonfriesen/ef57b8cfe80a608d649c0485e8665061 to your computer and use it in GitHub Desktop.
Save jonfriesen/ef57b8cfe80a608d649c0485e8665061 to your computer and use it in GitHub Desktop.
Shows how to create a docker mongo status checker and run command after it connects successfully
version: '3.0'
services:
mongo:
image: mongo
ports:
- "27017:27017"
- "28017:28017"
environment:
MONGO_INITDB_ROOT_USERNAME: mongoadmin
MONGO_INITDB_ROOT_PASSWORD: secret
mongo-config:
image: mongo
depends_on:
- mongo
command: >
/bin/bash -c "
until mongo --host mongo -u mongoadmin -p secret --authenticationDatabase admin --eval \"printjson(db.serverStatus())\"
do
echo sleeping;
sleep 3;
done;
echo Successfully Connected, running Mongo config commands.;
mongo --host mongo -u mongoadmin -p secret --authenticationDatabase admin mymongodb --eval 'db.createUser({ \"user\": \"mongouser\", \"pwd\": \"secret\", \"roles\": [ \"readWrite\", \"dbAdmin\" ] })';
"
@payalb
Copy link

payalb commented Sep 7, 2019

I am trying a similar thing, it says unknown host mongo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment