Created
July 25, 2018 18:15
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\" ] })'; | |
" |
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
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