Last active
November 1, 2024 17:00
-
-
Save gbzarelli/c15b607d62fc98ae436564bf8129ea8e to your computer and use it in GitHub Desktop.
Initializing mongo db in docker-compose with init script #helpdev-blog
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: | |
# 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 |
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 = 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 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
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/).
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
Where do you put the mongo-init.js file if you're using the Windows Mongo container?