问题
I try to run MongoDB and Mongo-express by Docker-compose. I use following config:
version: '3'
services:
mongo:
image: mongo
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=false
- ME_CONFIG_MONGODB_AUTH_DATABASE=admin
- ME_CONFIG_MONGODB_AUTH_USERNAME=${MONGO_ROOT_USER}
- ME_CONFIG_MONGODB_AUTH_PASSWORD=${MONGO_ROOT_PASSWORD}
- ME_CONFIG_BASICAUTH_USERNAME=${MONGOEXPRESS_LOGIN}
- ME_CONFIG_BASICAUTH_PASSWORD=${MONGOEXPRESS_PASSWORD}
depends_on:
- mongo
ports:
- "8080:8081"
and .env file:
MONGO_ROOT_USER=devroot
MONGO_ROOT_PASSWORD=devroot
MONGOEXPRESS_LOGIN=dev
MONGOEXPRESS_PASSWORD=dev
After docker-compose up mongo-service is running, but mongo-express fails. I see error in logs:
mongo-express_1 | Welcome to mongo-express
mongo-express_1 | ------------------------
mongo-express_1 | Mongo Express server listening at http://0.0.0.0:8081
mongo-express_1 | Server is open to allow connections from anyone (0.0.0.0)
mongo-express_1 | Database connected
mongo-express_1 | Connecting to admin...
mongo-express_1 | TypeError: db is not a function
mongo-express_1 | at /node_modules/mongo-express/lib/db.js:114:53
mongo-express_1 | at Array.forEach (native)
What I do wrong? Thanks for advance!
回答1:
it works in my mac. If you delete docker-compose containers and rebuild, maybe it will work. I added my files. Only i changed mongo-express port.
.env
MONGO_ROOT_USER=devroot
MONGO_ROOT_PASSWORD=devroot
MONGOEXPRESS_LOGIN=dev
MONGOEXPRESS_PASSWORD=dev
docker-compose.yml
version: '3'
services:
mongo:
image: mongo
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=false
- ME_CONFIG_MONGODB_AUTH_DATABASE=admin
- ME_CONFIG_MONGODB_AUTH_USERNAME=${MONGO_ROOT_USER}
- ME_CONFIG_MONGODB_AUTH_PASSWORD=${MONGO_ROOT_PASSWORD}
- ME_CONFIG_BASICAUTH_USERNAME=${MONGOEXPRESS_LOGIN}
- ME_CONFIG_BASICAUTH_PASSWORD=${MONGOEXPRESS_PASSWORD}
depends_on:
- mongo
ports:
- "8888:8081"
Docker Version
Client:
Version: 18.06.0-ce
API version: 1.38
Go version: go1.10.3
Git commit: 0ffa825
Built: Wed Jul 18 19:05:26 2018
OS/Arch: darwin/amd64
Experimental: false
Server:
Engine:
Version: 18.06.0-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: 0ffa825
Built: Wed Jul 18 19:13:46 2018
OS/Arch: linux/amd64
Experimental: true
回答2:
Running it like this just worked for me maybe it helps you
mongoex:
image: mongo-express
environment:
- ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
- ME_CONFIG_MONGODB_SERVER=database
- ME_CONFIG_MONGODB_PORT=27017
- ME_CONFIG_MONGODB_ENABLE_ADMIN=false
- ME_CONFIG_MONGODB_AUTH_DATABASE=admin
ports:
- "8081:8081"
links:
- database
database:
image: mongo:latest
command: --smallfiles
ports:
- "27017:27017"
good luck and regards!
回答3:
The simplest way is run docker-compose up -d in directory with docker-compose.yml:
version: "3"
services:
mongo:
image: "mongo:3-stretch"
mongo-express:
image: "mongo-express:latest"
ports:
- "8081:8081"
And then open http://localhost:8081/ in your browser.
回答4:
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...
回答5:
This is very useful things but same time unstable. You need find out your own way How to write docker-compose.yml For me It's was works something like that
version: '3'
services:
db:
image: mongo:2.6.12
container_name: app_db
volumes:
- store:/data/db
ports:
- "27017:27017"
mongo-express:
container_name: mongo-express
links:
- 'db:mongo'
ports:
- '8081:8081'
environment:
- 'ME_CONFIG_OPTIONS_EDITORTHEME=ambiance'
- 'ME_CONFIG_BASICAUTH_USERNAME=user'
- 'ME_CONFIG_BASICAUTH_PASSWORD=pass'
image: mongo-express
来源:https://stackoverflow.com/questions/47901561/how-to-run-mongodb-and-mongo-express-with-docker-compose