0

Simple Node app and mongo containers created using docker-compose below... What am I missing?

mongodb://user:password@mongo:27017/

version: '3.8' 

services: 

  mongo: 
    image: mongo
    restart: always
    environment: 
      - MONGO_INITDB_ROOT_USERNAME=user
      - MONGO_INITDB_ROOT_PASSWORD=password


  app:  
    image: app
    build: 
      context: ./app
      dockerfile: Dockerfile 
    ports: 
      - "3000:3000"
    depends_on:
      - mongo

I've read several posts on the same issue and the official mongo docker page and seem to be doing everything correct. Keep getting the following msg.

app_1    | mongodb://user:password@mongo:27017/ {
app_1    |   autoIndex: false,
app_1    |   poolSize: 10,
app_1    |   bufferMaxEntries: 0,
app_1    |   useNewUrlParser: true,
app_1    |   useUnifiedTopology: true
app_1    | }
app_1    | MongoDB connection with retry
app_1    | MongoDB connection unsuccessful, retry after 5 seconds.  2
rickhg12hs
  • 10,638
  • 6
  • 24
  • 42
Joe So
  • 1

1 Answers1

0

I prepared interesting test for you

version: '3.8'

services:
  mongo:
    image: mongo
    environment:
      - MONGO_INITDB_ROOT_USERNAME=user
      - MONGO_INITDB_ROOT_PASSWORD=password
    healthcheck:
      test: "echo 'db.runCommand(\"ping\").ok'"
      interval: 5s
      timeout: 5s
      retries: 3

  app:
    image: mongo
    command: "mongosh mongodb://user:password@mongo:27017/admin --eval \"printjson(db.test.insertOne({'a': 1}))\""
    ports:
      - "3000:3000"
    depends_on:
      - mongo

It will print

app_1    | {
app_1    |   acknowledged: true,
app_1    |   insertedId: ObjectId("63696c4e99703eb4ab9fba62")
app_1    | }

but if you will change

    command: "mongosh mongodb://user:password@mongo:27017/admin --eval \"printjson(db.test.insertOne({'a': 1}))\""

to

    command: "mongosh mongodb://user:password@mongo:27017/local --eval \"printjson(db.test.insertOne({'a': 1}))\""

you will see error. Even if you will add MONGO_INITDB_DATABASE to mongo with value local.

You can test it running in second console command:

docker-compose run app bash

and then try

mongosh mongodb://user:password@mongo:27017/admin --eval "printjson(db.test.insertOne({'a': 1}))"

with success

and

mongosh mongodb://user:password@mongo:27017/local --eval "printjson(db.test.insertOne({'a': 1}))"

that failing. You can see logs from mongo like

mongo_1  | {"t":{"$date":"2022-11-07T20:40:35.686+00:00"},"s":"I",  "c":"ACCESS",   "id":20249,   "ctx":"conn6","msg":"Authentication failed","attr":{"mechanism":"SCRAM-SHA-256","speculative":true,"principalName":"user","authenticationDatabase":"local","remote":"172.20.0.3:50382","extraInfo":{},"error":"UserNotFound: Could not find user \"user\" for db \"local\""}}
mongo_1  | {"t":{"$date":"2022-11-07T20:40:35.687+00:00"},"s":"I",  "c":"ACCESS",   "id":20249,   "ctx":"conn6","msg":"Authentication failed","attr":{"mechanism":"SCRAM-SHA-1","speculative":false,"principalName":"user","authenticationDatabase":"local","remote":"172.20.0.3:50382","extraInfo":{},"error":"UserNotFound: Could not find user \"user\" for db \"local\""}}
mongo_1  | {"t":{"$date":"2022-11-07T20:40:35.688+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn5","msg":"Connection ended","attr":{"remote":"172.20.0.3:50374","uuid":"b77fff1f-b832-4900-9c2d-1e7fd1e79424","connectionId":5,"connectionCount":1}}
mongo_1  | {"t":{"$date":"2022-11-07T20:40:35.699+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn6","msg":"Connection ended","attr":{"remote":"172.20.0.3:50382","uuid":"3995bcbf-706d-4bed-92a2-04736305b7c2","connectionId":6,"connectionCount":0}}

this problem is described in topic User not found on MongoDB Docker image with authentication

You can authenticate with user,password against admin db, not mydb

You can read more about creating database with user and password here:

How to create a DB for MongoDB container on start up?

Daniel
  • 7,684
  • 7
  • 52
  • 76
  • Daniel - are you saying that the issue is caused by MONGO_INITDB_ROOT_USERNAME=user? I tried changing that to MONGO_INITDB_ROOT_USERNAME=admin and still doesn't help me connect. pls. let me know if I misunderstood your answer. – Joe So Nov 07 '22 at 23:00
  • No username. This is issue of db name. Using this user name and password you have access only to admin db, but in last link there are plenty approaches to this problem. If you want to do it simple resign from user name and password, if you need this you have to follow after one of answers from last link. – Daniel Nov 08 '22 at 07:40