Hi, I am a newbie to both mongo (and Docker too), so please bear with me
Started a REST api example project express/mongo/mongoose/typescript.
The API works fine, does what it is supposed to do, persists data, all nice and dandy, except - all the collections it creates are placed into the implicit database named test
. Even if this database were to be the only one in the container, I would like to have more control and be more explicit about the db name I am using.
Below is my docker-compose.yml
version: '3.9'
services:
api:
build: .
container_name: api-mongodb
restart: unless-stopped
environment:
- DB_URL=mongodb://myself:pass123@mongo:27017
ports:
- '3131:3131'
depends_on:
- mongo
mongo:
container_name: mongo_container
image: mongo:latest
restart: always
volumes:
- mongo_dbv:/data/db/
environment:
- MONGO_INITDB_ROOT_USERNAME=myself
- MONGO_INITDB_ROOT_PASSWORD=pass123
- MONGO_INITDB_DATABASE=api
volumes:
mongo_dbv: {}
I connect via mongoose.connect(process.env.DB_URL)
.
To be honest, I don’t see the effect of MONGO_INITDB_DATABASE
anywhere.
When I cli into the container via
docker exec -it mongo_container mongosh -u myself -p pass123
and then show dbs
, I don’t see a database named api
anywhere. Tried appending api
to the DB_URL string - this resulted in auth error.
So how can I force mongo into creating the database called api
?
(Let me know if I can provide more info).
Thanks
ND