My goal is to build a Mongo database with its express using Docker. I am using the terminal.
I created the network.
docker network create mongo
Then, I put this command.\
docker run --detach
--name mongodatabaseimage
--network mongo
--publish 27017:27017
--mount type=bind,source="$(pwd)/containers/mongodb/databases",target=/data/db
--env MONGO_INITDB_ROOT_USERNAME="DatabaseAdmin"
--env MONGO_INITDB_ROOT_PASSWORD="0112213949Ibrahim"
mongo:4.4.20-rc0
I want a required authentication to connect to the database, so I put these variables: MONGO_INITDB_ROOT_USERNAME
, MONGO_INITDB_ROOT_PASSWORD
.
When I try to build the Express container using:\
docker run -it
--name mongoexpress
--network mongo
-p 8081:8081
-e ME_CONFIG_MONGODB_SERVER=mongodatabaseimage
-e ME_CONFIG_MONGODB_ADMINUSERNAME=DatabaseAdmin
-e ME_CONFIG_MONGODB_ADMINPASSWORD=0112213949Ibrahim
-e ME_CONFIG_BASICAUTH_USERNAME=admin
-e ME_CONFIG_BASICAUTH_PASSWORD=2213949ib
-e ME_CONFIG_MONGODB_ENABLE_ADMIN=true
mongo-express:1.0.0-alpha.4
I receive this error:
(node:6) [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://DatabaseAdmin:0112213949Ibrahim@mongodatabaseimage:27017/" (node:6) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodatabaseimage:27017] on first connect [MongoError: Authentication failed. at Connection.messageHandler (/node_modules/mongodb/lib/core/connection/connection.js:364:19) at Connection.emit (events.js:314:20) at processMessage (/node_modules/mongodb/lib/core/connection/connection.js:456:10) at Socket. (/node_modules/mongodb/lib/core/connection/connection.js:625: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) { ok: 0, code: 18, codeName: 'AuthenticationFailed' }] at Pool. (/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 callback (/node_modules/mongodb/lib/core/connection/connect.js:75:5) at /node_modules/mongodb/lib/core/connection/connect.js:158:27 at /node_modules/mongodb/lib/core/auth/scram.js:110:14 at _callback (/node_modules/mongodb/lib/core/connection/connection.js:333:7) at Connection.messageHandler (/node_modules/mongodb/lib/core/connection/connection.js:364:9) at Connection.emit (events.js:314:20) at processMessage (/node_modules/mongodb/lib/core/connection/connection.js:456:10) at Socket. (/node_modules/mongodb/lib/core/connection/connection.js:625: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) (node:6) 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:6) [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 tried assigning the default port to 8080 using
-p 8080:27017
to the database container, and changed the corresponding variable in the express container. - I tried changing the containers' names.
- My node application is connecting directly without an authentication, which is a security Issue I trying to solve.
mongodb://192.168.134.128:27017/messages