Skip to content

Instantly share code, notes, and snippets.

@gbzarelli
Last active July 10, 2024 11:04
Show Gist options
  • Save gbzarelli/c15b607d62fc98ae436564bf8129ea8e to your computer and use it in GitHub Desktop.
Save gbzarelli/c15b607d62fc98ae436564bf8129ea8e to your computer and use it in GitHub Desktop.
Initializing mongo db in docker-compose with init script
version: '3.8'
services:
# Database - Mongo DB
mongo:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: helpdev
MONGO_INITDB_ROOT_PASSWORD: 123456
ports:
- "27017:27017"
volumes:
- ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
# Database Manager
mongo-express:
image: mongo-express
ports:
- 8099:8081
depends_on:
- mongo
environment:
ME_CONFIG_BASICAUTH_USERNAME: express
ME_CONFIG_BASICAUTH_PASSWORD: 123456
ME_CONFIG_MONGODB_PORT: 27017
ME_CONFIG_MONGODB_ADMINUSERNAME: helpdev
ME_CONFIG_MONGODB_ADMINPASSWORD: 123456
links:
- mongo
db = db.getSiblingDB('sample_db');
db.createCollection('sample_collection');
db.sample_collection.insertMany([
{
org: 'helpdev',
filter: 'EVENT_A',
addrs: 'http://rest_client_1:8080/wh'
},
{
org: 'helpdev',
filter: 'EVENT_B',
addrs: 'http://rest_client_2:8081/wh'
},
{
org: 'github',
filter: 'EVENT_C',
addrs: 'http://rest_client_3:8082/wh'
}
]);
@petryuno1
Copy link

Where do you put the mongo-init.js file if you're using the Windows Mongo container?

@gbzarelli
Copy link
Author

@petryuno1 into the folder docker-entrypoint-initdb.d. Like showed in line #14

Structure:

 -- my-folder/
 -- -- docker-compose.yml
 -- -- docker-entrypoint-initdb.d/
 -- -- -- -- mongo-init.js

@petryuno1
Copy link

That only works in a Linux container. I am using the mongo:6.0.7-nanoserver image, which has a Windows filesystem (so no /docker-entrypoint-initdb.d/).

@YorkHwang
Copy link

YorkHwang commented Jul 10, 2024

can we create tow or more test databases?

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