How to connect mongodb on linux-host and mongo-express in docker - mongodb

I have installed mongodb successfully on a linux-host (redhat 8). mongosh is working perfectly and mongod listens on port 27017. I'd like to install mongo-express as docker container and connect it to mongod running on the host-machine (outside docker!). I have tried the following docker-compose.yml:
version: '3.7'
services:
mongo-express:
image: mongo-express
restart: always
# ports:
# - 8081:8081
environment:
ME_CONFIG_MONGODB_SERVER: localhost
ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGO_USER}
ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_PASS}
ME_CONFIG_BASICAUTH_USERNAME: ${MONGO_USER}
ME_CONFIG_BASICAUTH_PASSWORD: ${MONGO_PASS}
network_mode: host
I commented out the port-settings as incompatible with network_mode:host.
Result: The container will be built and gives this warnings:
mongoexpress-mongo-express-1 | Welcome to mongo-express
mongoexpress-mongo-express-1 | ------------------------
mongoexpress-mongo-express-1 |
mongoexpress-mongo-express-1 |
mongoexpress-mongo-express-1 | (node:7) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
mongoexpress-mongo-express-1 | (node:7) UnhandledPromiseRejectionWarning: MongoError: not authorized on config to execute command { listCollections: 1, filter: {}, cursor: {}, nameOnly: false, lsid: { id: UUID("2e2c2b92-4d5d-47d0-9c37-59e52dd3a5df") }, $db: "config" }
mongoexpress-mongo-express-1 | at Connection.<anonymous> (/node_modules/mongodb/lib/core/connection/pool.js:453:61)
mongoexpress-mongo-express-1 | at Connection.emit (events.js:314:20)
mongoexpress-mongo-express-1 | at processMessage (/node_modules/mongodb/lib/core/connection/connection.js:456:10)
mongoexpress-mongo-express-1 | at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connection.js:625:15)
mongoexpress-mongo-express-1 | at Socket.emit (events.js:314:20)
mongoexpress-mongo-express-1 | at addChunk (_stream_readable.js:297:12)
mongoexpress-mongo-express-1 | at readableAddChunk (_stream_readable.js:272:9)
mongoexpress-mongo-express-1 | at Socket.Readable.push (_stream_readable.js:213:10)
mongoexpress-mongo-express-1 | at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
mongoexpress-mongo-express-1 | (node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
mongoexpress-mongo-express-1 | (node:7) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Trying to access mongo-express on host gives:
[root#d20p280a mongoexpress]# curl localhost:8081
curl: (7) Failed to connect to localhost port 8081: Connection refused
docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5cc00d871e82 mongo-express "tini -- /docker-ent…" 13 minutes ago Up 13 minutes mongoexpress-mongo-express-1
Any hint appreciated - thanks!

The devil lies in the details, as always. The missing clue was this entry in the error log:
mongoexpress-mongo-express-1 | (node:7) UnhandledPromiseRejectionWarning: MongoError: not authorized on config to execute command { listCollections: 1, filter: {}, cursor: {}, nameOnly: false, lsid: { id: UUID("2e2c2b92-4d5d-47d0-9c37-59e52dd3a5df") }, $db: "config" }
The docker-compose.yml is correct as shown. The admin user in mongodb has only the following roles: "userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase" which are missing the listCollections privilege.
Strange though, that mongo-express didn't even start the server. I would have expected an error message in the webbrowser.
Thanks to all who took time to examine the problem.
This is the admin role in use now:
roles: [
{
role: 'root',
db: 'admin'
}
]

Related

connection issue while running docker images of mongo db and mongo-express

I am trying to run docker images of mongo and mongo-express but getting the enter code here below error on mongo-express logs
commands I used
docker run -d -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=password --name mongodb mongo
docker run -d -p 8081:8081 -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e ME_CONFIG_MONGODB_ADMINPASSWORD=password --name mongo-express -e ME_CONFIG_MONGODB_SERVER=mongodb mongo-express
Error logs on the mongo-express terminal
Welcome to mongo-express
------------------------
(node:7) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Could not connect to database using connectionString: mongodb://admin:password#mongodb:27017/"
(node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodb:27017] on first connect [Error: getaddrinfo EAI_AGAIN mongodb
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
name: 'MongoNetworkError'
}]
at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
at Pool.emit (events.js:314:20)
at /node_modules/mongodb/lib/core/connection/pool.js:564:14
at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
at /node_modules/mongodb/lib/core/connection/connect.js:32:7
at callback (/node_modules/mongodb/lib/core/connection/connect.js:300:5)
at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:7)
at Object.onceWrapper (events.js:421:26)
at Socket.emit (events.js:314:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:7) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Mongo express always crashes in docker-compose

Here is my docker-compose file:
version: "3.1"
services:
mongodb:
container_name: mongodb
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: $$Admin123
MONGO_INITDB_DATABASE: xplorie
# hostname: mongodb
networks:
mynetwork:
ipv4_address: 172.19.0.20
ports:
- 27017:27017
volumes:
- /var/data/mongo:/data/db
mongo-express:
image: mongo-express
environment:
# ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_PORT: 27017
# ME_CONFIG_MONGODB_AUTH_USERNAME: admin
# ME_CONFIG_MONGODB_AUTH_PASSWORD: $$Admin123
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: $$Admin123
# ME_CONFIG_BASICAUTH_USERNAME: admin
# ME_CONFIG_BASICAUTH_PASSWORD: $$Admin123
# networks:
# mynetwork:
# ipv4_address: 172.19.0.21
volumes:
- /var/data/mongoclient:/data/db
depends_on:
- mongodb
ports:
- "3300:3000"
restart: always
networks:
mynetwork:
driver: bridge
ipam:
config:
- subnet: 172.19.0.0/24
gateway: 172.19.0.1
but when I run this mongo express always crashes. this is the log:
mongo-express_1 | Welcome to mongo-express
mongo-express_1 | ------------------------
mongo-express_1 |
mongo-express_1 |
mongo-express_1 | (node:7) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
mongo-express_1 | Could not connect to database using connectionString: mongodb://mongo:27017"
mongo-express_1 | (node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [Error: getaddrinfo EAI_AGAIN mongo
mongo-express_1 | at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
mongo-express_1 | name: 'MongoNetworkError'
mongo-express_1 | }]
mongo-express_1 | at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
mongo-express_1 | at Pool.emit (events.js:314:20)
mongo-express_1 | at /node_modules/mongodb/lib/core/connection/pool.js:564:14
mongo-express_1 | at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
mongo-express_1 | at /node_modules/mongodb/lib/core/connection/connect.js:32:7
mongo-express_1 | at callback (/node_modules/mongodb/lib/core/connection/connect.js:300:5)
mongo-express_1 | at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:7)
mongo-express_1 | at Object.onceWrapper (events.js:421:26)
mongo-express_1 | at Socket.emit (events.js:314:20)
mongo-express_1 | at emitErrorNT (internal/streams/destroy.js:92:8)
mongo-express_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
mongo-express_1 | at processTicksAndRejections (internal/process/task_queues.js:84:21)
mongo-express_1 | (node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
mongo-express_1 | (node:7) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
deploy_mongo-express_1 exited with code 0
You have the mongo database container on one network (user defined) and the mongo-express container on the default bridge network. So they can't talk to each other.
Also, the hostname of the mongo db container is mongodb, so you should comment that back in.
Mongo express listens on port 8081, so that's the port you should map.
I'd just use the default bridge network and do something like this
version: "3.1"
services:
mongodb:
container_name: mongodb
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: $$Admin123
MONGO_INITDB_DATABASE: xplorie
ports:
- 27017:27017
volumes:
- /var/data/mongo:/data/db
mongo-express:
image: mongo-express
environment:
ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: $$Admin123
# ME_CONFIG_BASICAUTH_USERNAME: admin
# ME_CONFIG_BASICAUTH_PASSWORD: $$Admin123
depends_on:
- mongodb
ports:
- "3300:8081"
restart: always
You should then be able to access Mongo express on http://localhost:3300/.
Be aware that Mongo will not create a database for you because you don't have any initialization scripts. From the docs: 'MongoDB is fundamentally designed for "create on first use", so if you do not insert data with your JavaScript files, then no database is created.'
Another thing is that Mongo Express is still alpha software and it tries to connect to the database immediately when it starts and that connection often fails because Mongo needs a little time to be ready. By setting restart: always as you have, you'll restart Express until the connection succeeds. But you might see a failed container or two on startup.

Default Docker Compose for MongoDB and Mongo-Express times out on Debian

I am trying to setup a docker-compose setup to run MongoDB and Mongo-Express on a Debian virtual machine.
I've read up on problems with volumes, same docker networks, setting ME_CONFIG_MONGODB_SERVER to the Mongodb container name and having problems with UFW.
This is my current docker-compose.yml
version: '3.7'
services:
mongodb:
image: mongo
container_name: mongodb
restart: always
ports:
- '27017:27017'
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_INITDB_DATABASE=default
networks:
- mongo-compose-network
volumes:
- /home/paul/mongodbdocker/data:/data/db
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
depends_on:
- mongodb
networks:
- mongo-compose-network
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_SERVER=mongodb
- ME_CONFIG_BASICAUTH_USERNAME=admin
- ME_CONFIG_BASICAUTH_PASSWORD=password
ports:
- '8081:8081'
volumes:
- /home/paul/mongodbdocker/data:/data/db
networks:
mongo-compose-network:
driver: bridge
I have disabled the UFW firewall with sudo ufw disable and also edited the ufw configuration as described in here What is the best practice of docker + ufw under Ubuntu from Feng.
The MongoDB starts up, but sadly mongo-express fails on first connection with this error:
mongo-express | Welcome to mongo-express
mongo-express | ------------------------
mongo-express |
mongo-express |
mongo-express | (node:8) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
mongo-express | Could not connect to database using connectionString: mongodb://admin:password#mongodb:27017/"
mongo-express | (node:8) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodb:27017] on first connect [MongoNetworkTimeoutError: connection timed out
mongo-express | at connectionFailureError (/node_modules/mongodb/lib/core/connection/connect.js:362:14)
mongo-express | at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:16)
mongo-express | at Object.onceWrapper (events.js:420:28)
mongo-express | at Socket.emit (events.js:314:20)
mongo-express | at Socket._onTimeout (net.js:483:8)
mongo-express | at listOnTimeout (internal/timers.js:554:17)
mongo-express | at processTimers (internal/timers.js:497:7)]
mongo-express | at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
mongo-express | at Pool.emit (events.js:314:20)
mongo-express | at /node_modules/mongodb/lib/core/connection/pool.js:564:14
mongo-express | at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
mongo-express | at /node_modules/mongodb/lib/core/connection/connect.js:32:7
mongo-express | at callback (/node_modules/mongodb/lib/core/connection/connect.js:300:5)
mongo-express | at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:7)
mongo-express | at Object.onceWrapper (events.js:420:28)
mongo-express | at Socket.emit (events.js:314:20)
mongo-express | at Socket._onTimeout (net.js:483:8)
mongo-express | at listOnTimeout (internal/timers.js:554:17)
mongo-express | at processTimers (internal/timers.js:497:7)
mongo-express | (node:8) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
mongo-express | (node:8) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

How to authenticate mongo dB docker container through MongoDB compass?

Problem:
I have set up a docker container for MongoDB and mongo-express. Both are up and running successfully. This is how my docker-compose configurations.
questionsdb:
container_name: questionsdb
restart: always
command: mongod --auth
environment:
- MONGO_INITDB_DATABASE=questions
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=org1234
volumes:
- mongo_data:/data/db
ports:
- "27019:27017"
mongo-express:
container_name: mongo-express
restart: always
ports:
- "8081:8081"
volumes:
- mongoadmin_data:/data/dbadmin
environment:
#- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
#- ME_CONFIG_MONGODB_ADMINPASSWORD=org1234
- ME_CONFIG_MONGODB_SERVER=questionsdb
- ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
- ME_CONFIG_MONGODB_URL=mongodb://admin:org1234#questionsdb:27017
- ME_CONFIG_BASICAUTH_USERNAME=admin#myorg.com
- ME_CONFIG_BASICAUTH_PASSWORD=org#1234
- ME_CONFIG_MONGODB_ENABLE_ADMIN=true
links:
- questionsdb
But when I open the mongo-express nothing showing. Then I look on mongo-express logs. Then I saw this.
(node:7) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
(node:7) UnhandledPromiseRejectionWarning: MongoError: command listDatabases requires authentication
at Connection.<anonymous> (/node_modules/mongodb/lib/core/connection/pool.js:453:61)
at Connection.emit (events.js:314:20)
at processMessage (/node_modules/mongodb/lib/core/connection/connection.js:454:10)
at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connection.js:623:15)
at Socket.emit (events.js:314:20)
at addChunk (_stream_readable.js:297:12)
at readableAddChunk (_stream_readable.js:272:9)
at Socket.Readable.push (_stream_readable.js:213:10)
at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
(node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:7) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Then I tried to access the MongoDB container through MongoDbComapsss like this.
but that also failed with giving me authentication failed error. If I remove MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD it working without any issues. I tried a lot to find a solution to this one. But I was unable to do so. Can someone help me to solve this issue? Thank you
this issue can be resolved if you put
USERNAME & PASSWORD
encrypted in BASE64
Replace this
ME_CONFIG_MONGODB_ADMINUSERNAME=admin
ME_CONFIG_MONGODB_ADMINPASSWORD=org1234
with
ME_CONFIG_MONGODB_ADMINUSERNAME=YWRtaW4=
ME_CONFIG_MONGODB_ADMINPASSWORD=b3JnMTIzNA==
BASE64 Encode & Decoder
https://www.base64encode.org/

Mongo authentication inside Docker

I am trying to run the mongo docker image with authentication. Following the most simple example from the documentation I ran the mongo and the mongo-express images by the docker-compose up command. My docker-compose.yml at this stage:
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
This runs, both containers start ok and I can browse the contents of mongo from the mongo-express website. However, whenever I change the username or the password in the docker-compose.yml file, for example to this:
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example123
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example123
the mongo-express throws an unauthotrized error message:
mongo-express_1 | Admin Database connected
mongo-express_1 | { MongoError: Authentication failed.
mongo-express_1 | at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:31:11)
mongo-express_1 | at /node_modules/mongodb-core/lib/connection/pool.js:483:72
mongo-express_1 | at authenticateStragglers (/node_modules/mongodb-core/lib/connection/pool.js:429:16)
mongo-express_1 | at Connection.messageHandler (/node_modules/mongodb-core/lib/connection/pool.js:463:5)
mongo-express_1 | at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:319:22)
mongo-express_1 | at emitOne (events.js:116:13)
mongo-express_1 | at Socket.emit (events.js:211:7)
mongo-express_1 | at addChunk (_stream_readable.js:263:12)
mongo-express_1 | at readableAddChunk (_stream_readable.js:250:11)
mongo-express_1 | at Socket.Readable.push (_stream_readable.js:208:10)
mongo-express_1 | name: 'MongoError',
mongo-express_1 | message: 'Authentication failed.',
mongo-express_1 | ok: 0,
mongo-express_1 | errmsg: 'Authentication failed.',
mongo-express_1 | code: 18,
mongo-express_1 | codeName: 'AuthenticationFailed' }
mongo-express_1 | unable to list databases
mongo-express_1 | { MongoError: command listDatabases requires authentication
mongo-express_1 | at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:31:11)
mongo-express_1 | at /node_modules/mongodb-core/lib/connection/pool.js:483:72
mongo-express_1 | at authenticateStragglers (/node_modules/mongodb-core/lib/connection/pool.js:429:16)
mongo-express_1 | at Connection.messageHandler (/node_modules/mongodb-core/lib/connection/pool.js:463:5)
mongo-express_1 | at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:319:22)
mongo-express_1 | at emitOne (events.js:116:13)
mongo-express_1 | at Socket.emit (events.js:211:7)
mongo-express_1 | at addChunk (_stream_readable.js:263:12)
mongo-express_1 | at readableAddChunk (_stream_readable.js:250:11)
mongo-express_1 | at Socket.Readable.push (_stream_readable.js:208:10)
mongo-express_1 | name: 'MongoError',
mongo-express_1 | message: 'command listDatabases requires authentication',
mongo-express_1 | ok: 0,
mongo-express_1 | errmsg: 'command listDatabases requires authentication',
mongo-express_1 | code: 13,
mongo-express_1 | codeName: 'Unauthorized' }
No matter what username or password I enter in docker-compose.yml, I cannot make mongo-express connect to mongo, only if I use the original root and example pair.
Note, that I am not getting the username and password as environment variables, but they are directly typed into the docker-compose.yml file as you can see here.
Also note, that when I change the MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD (mongo's) variables to anything, they don't seem to have an effect, I can still connect with mongo-express using the original root and example credentials.
What causes this behaviour? How can I make this work?
Your docker-compose command:
docker-compose up --build --force-recreate
Mongo image uses anonymous volumes, so you need also --renew-anon-volumes (doc):
docker-compose up --build --force-recreate --renew-anon-volumes
Otherwise previous volume with already initialized DB is used => INITDB env variables won't be used.
What worked for me was just adding the environment variable for the mongodb server.
ME_CONFIG_MONGODB_SERVER: mongo
like this:
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_SERVER: mongo
If you create the mongodb container (docker-compose up will build/pull the image and start a container) giving it the username and password, mongodb will configure itself on the first run and never again! Until you create a new container for that service, the default user/pass will be the ones set initially. If you want to change the default, a new container must be created.
You can also add users after the container has started.
Only stopping the service containers (docker-compose stop) will not destroy de container. To do so, call docker-compose down or, when starting, call docker-compose up --force-recreate.
Regards
In my case I had to first stop all my containers.
Then docker system prune -a --volumes
⚠️ This is the last resort. Before using this command check the docker doc,
The docker system prune -a --volumes can solve the problem but a much simpler way is (if you are mounting a data volume for db to be persistent) to just delete and recreate a new one. This way other volumes will be untouched.
And of course combine it with : docker-compose up --build --force-recreate --renew-anon-volumes