I am trying to create my first docker network with docker using mongo db and mongo express images.
I’ve pulled with docker pull mongo
and docker pull mongo-express
.
I’ve created a docker network with docker network create mongo-network
, then I first launched the mongo docker with:
docker run -p 27017:27017 -d -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=mongoadmin --name mongodb --net mongo-network mongo
and similarly the mongo-express:
docker run -d -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e ME_CONFIG_MONGODB_ADMINPASSWORD=password --net mongo-network --name me -e ME_CONFIG_MONGODB_SERVER=mongodb mongo-express
The env variables are the ones indicated here:
https://hub.docker.com/_/mongo-express
But the logs is showing the following error:
Sun Sep 15 10:52:34 UTC 2024 retrying to connect to mongo:27017 (10/10)
/docker-entrypoint.sh: line 15: mongo: Try again
/docker-entrypoint.sh: line 15: /dev/tcp/mongo/27017: Invalid argument
No custom config.js found, loading config.default.js
Welcome to mongo-express 1.0.2
------------------------
Could not connect to database using connectionString: mongodb://admin:****@mongodb:27017/"
/app/node_modules/mongodb/lib/cmap/connection.js:227
callback(new error_1.MongoServerError(document));
^
MongoServerError: Authentication failed.
at Connection.onMessage (/app/node_modules/mongodb/lib/cmap/connection.js:227:30)
at MessageStream.<anonymous> (/app/node_modules/mongodb/lib/cmap/connection.js:60:60)
at MessageStream.emit (node:events:517:28)
at processIncomingData (/app/node_modules/mongodb/lib/cmap/message_stream.js:125:16)
at MessageStream._write (/app/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
at writeOrBuffer (node:internal/streams/writable:392:12)
at _write (node:internal/streams/writable:333:10)
at Writable.write (node:internal/streams/writable:337:10)
at Socket.ondata (node:internal/streams/readable:809:22)
at Socket.emit (node:events:517:28) {
ok: 0,
code: 18,
codeName: 'AuthenticationFailed',
connectionGeneration: 0,
[Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
}
Node.js v18.20.3
What am I missing?
UPDATE
The password was wrong, but after using the correct one, I am getting this log:
/docker-entrypoint.sh: line 15: /dev/tcp/mongo/27017: Invalid argument
Sun Sep 15 14:45:36 UTC 2024 retrying to connect to mongo:27017 (10/10)
/docker-entrypoint.sh: line 15: mongo: Try again
/docker-entrypoint.sh: line 15: /dev/tcp/mongo/27017: Invalid argument
No custom config.js found, loading config.default.js
Welcome to mongo-express 1.0.2
------------------------
Mongo Express server listening at http://0.0.0.0:8081
Server is open to allow connections from anyone (0.0.0.0)
basicAuth credentials are "admin:pass", it is recommended you change this in your config.js!
The server is up but is not accessible from the browser.
The updated commands are:
docker run -p 27017:27017 -d -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=password --name mongodb --net mongo-network mongo
docker run -d -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e ME_CONFIG_MONGODB_ADMINPASSWORD=password --net mongo-network --name me -e ME_CONFIG_MONGODB_SERVER=mongodb mongo-express
2
Answers
In the first container, you’re using the username
admin
and the passwordmongoadmin
In the second container, you’re trying to authenticate using the username
admin
and the passwordpassword
As of 2024 solution is:
1: create a network
2: run the mongo db
3: run the mongo express
make sure that –name mongo and @mongo:27017 both these things should be same