-
-
Save dalekurt/8edb3986f9bd6e6ac74cf7066fd989ec to your computer and use it in GitHub Desktop.
Initialize custom users and databases in MongoDb
This file contains 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
# Database | |
MONGO_INITDB_ROOT_USERNAME=admin | |
MONGO_INITDB_ROOT_PASSWORD=admin | |
MONGO_INITDB_DATABASE=my-app-db |
This file contains 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.8' | |
services: | |
mongo: | |
image: mongo:latest | |
container_name: mongo | |
restart: unless-stopped | |
env_file: | |
- ./.env | |
environment: | |
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME} | |
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD} | |
MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE} | |
volumes: | |
- mongodb-data:/data/db | |
- mongodb-log:/var/log/mongodb | |
- ./scripts/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro | |
ports: | |
- '27017:27017' | |
networks: | |
- mongo_net | |
mongo-express: | |
image: mongo-express | |
restart: always | |
ports: | |
- 8081:8081 | |
environment: | |
ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGO_INITDB_ROOT_USERNAME} | |
ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_INITDB_ROOT_PASSWORD} | |
ME_CONFIG_MONGODB_URL: mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@mongo:27017/ | |
networks: | |
- mongo_net | |
volumes: | |
mongodb-data: | |
driver: local | |
mongodb-log: | |
driver: local | |
networks: | |
mongo_net: | |
driver: bridge |
This file contains 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
db.createUser({ | |
user: 'app_user', | |
pwd: 'app_pass', | |
roles: [ | |
{ | |
role: 'dbOwner', | |
db: 'my-app-db', | |
}, | |
], | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment