Skip to content

Instantly share code, notes, and snippets.

@dalekurt
Created January 12, 2023 00:22
Show Gist options
  • Save dalekurt/8edb3986f9bd6e6ac74cf7066fd989ec to your computer and use it in GitHub Desktop.
Save dalekurt/8edb3986f9bd6e6ac74cf7066fd989ec to your computer and use it in GitHub Desktop.
Initialize custom users and databases in MongoDb
# Database
MONGO_INITDB_ROOT_USERNAME=admin
MONGO_INITDB_ROOT_PASSWORD=admin
MONGO_INITDB_DATABASE=my-app-db
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
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