0

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
  • 2 containers already connect to same network. This is error about authenticate in mongo. May be you missing database authentication, if you use default config add ?authSource=admin in mongoUrl. Commented Sep 15, 2024 at 11:24
  • You need a docker run -p ????:8081 option on the mongo-express container to make it accessible from outside Docker. You can use any port number you want, and it will be available on that port number; ignore the 8081 in the container's log message (beyond checking that it matches the second port number). Commented Sep 15, 2024 at 22:17

2 Answers 2

1

In the first container, you're using the username admin and the password mongoadmin

In the second container, you're trying to authenticate using the username admin and the password password

Sign up to request clarification or add additional context in comments.

2 Comments

Hi thanks, true but it does not solve
@roschach different error though?
0

As of 2024 solution is:

1: create a network

docker create network mongo-network

2: run the mongo db

docker run --network mongo-network -d -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=password --name mongo mongo:latest

3: run the mongo express

docker run --network mongo-network -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e ME_CONFIG_MONGODB_ADMINPASSWORD=password -e ME_CONFIG_MONGODB_URL="mongodb://admin:password@mongo:27017/" -e ME_CONFIG_BASICAUTH=false -d --name my-mongoexpress -p 8081:8081 mongo-express:latest

make sure that --name mongo and @mongo:27017 both these things should be same

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.