Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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\" ] })';
"
@jonfriesen
Copy link
Author

the mongo-config will be started after the mongo container starts and poll the mongo service in the mongo container until it is ready, then execute config scripts (in this case we are creating a user for the mymonogodb database). Afterwards the config container exits.

timeout should be added... one day

@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