How to authenticate mongo dB docker container through MongoDB compass? - mongodb

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/

Related

Connection issue between MongoDB and Mongo express in docker on the same host

i am facing Connection issue between MongoDB and Mongo express both the containers are on the same host which a Compute instance with ubuntu image.
Docker Commands used to build the containers:
sudo docker run -p 27017:27017 -d -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=password --name mongodb --net mongo-network mongo
sudo docker run -d -p 8082:8081 -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e ME_CONFIG_MONGODB_ADMINPASSWORD=password --net mongo-network --name mongo-express -e ME_CONFIG_MONGODB_SERVER=mongodb mongo-express
Error logs from the mongo express container:
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: connect EHOSTUNREACH 172.19.0.2:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
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.
I understand mongo express is not able to establish connection to mongo DB
sudo docker exec -it mongodb mongosh
Current Mongosh Log ID: 63cbdcc76f21488dfa6416fa
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.1
Using MongoDB: 6.0.3
Using Mongosh: 1.6.1`
I though the port was blocked by firewall but it not.
It seems you missed the container network part. So you should create a docker network.
docker network create mongo-network
Then attach your existing containers to this network
docker network connect mongo-network mongodb
docker network connect mongo-network mongo-express

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.

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

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'
}
]

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.