I try to run MongoDB and Mongo-express by Docker-compose. I use following config:
version: \'3\'
services:
mongo:
image: mongo
environment:
In case you are still having this issue... Or in case someone else is going absolutely crazy trying to figure out why none of this works...
Running docker-compose up --force-recreate
Doesn't recreate the container completely (not sure why) - it DOES look like it did, but I figured out that it wasn't recreating the container's data (databases!) when I got into the DB without Auth, and a collection that I created an hour ago was still there.
I had run the above command, and also 'docker image rm mongo' as well as removing the mongo-express (Not needed)
The problem is I had to do a 'docker container list' to see the two containers I had (one for Mongo by itself and one for Mongo with Mongo-express - not sure which I needed to delete, but I deleted them both...)
Once I did that, and did the 'up' your configuration pulling the values from the .env file worked fine - it did the INITDB... and set the Admin passwords correctly in the database!
So, if you EVER created it with any configuration to test it, you will have a container that it is reusing the data for (I think it doesn't recreate that to avoid data loss - I mean, 99.9% of the time you just want to recreate the PROGRAM part, not the data - otherwise you would lose your databases.)
Killed a couple hours figuring this one out...
Some updates in new verion of mongo-express, the accepted solution doesn't work any more, especially you need fix this issue
TypeError: Cannot read property 'listDatabases' of undefined
Do remember, if you adjust the setting, restart it with option --force-recreate
.
docker-compose up --force-recreate -d
Here is the docker-compose.yml I am using currently.
version: '3'
services:
mongo:
image: mongo:${MONGO_TAG}
environment:
- MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER}
- MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD}
- MONGO_INITDB_DATABASE=project
mongo-express:
image: mongo-express
environment:
- ME_CONFIG_MONGODB_SERVER=mongo
- ME_CONFIG_MONGODB_PORT=27017
- ME_CONFIG_MONGODB_ENABLE_ADMIN=true
- ME_CONFIG_MONGODB_AUTH_DATABASE=admin
- ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_ROOT_USER}
- ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_ROOT_PASSWORD}
- ME_CONFIG_BASICAUTH_USERNAME=${MONGOEXPRESS_LOGIN}
- ME_CONFIG_BASICAUTH_PASSWORD=${MONGOEXPRESS_PASSWORD}
links:
- mongo
ports:
- "8081:8081"