Itzik.B
Itzik.B

Reputation: 1086

Mongo image does not created with credentials

I am creating mongo:latest container on my machine using docker-compose.

This is my docker-compose.yaml:

version: '3.6'

services:
  backend:
    image: backend:1.0.0
    container_name: backend
    ports:
      - '5000:5000'
    links:
      - mongodb
    environment:
      NODE_ENV: PRODUCTION
      API_PORT: 5000
      MONGO_USERNAME: root
      MONGO_PASSWORD: rootPassXXX
      MONGO_HOST: mongodb
      MONGO_PORT: 27017
      MONGO_DATABASE: automatic
      JWT_TOKEN: Auto
  scheduler:
    image: scheduler:1.0.0
    container_name: scheduler
    links:
      - mongodb
    environment:
      NODE_ENV: PRODUCTION
      API_PORT: 5000
      MONGO_USERNAME: root
      MONGO_PASSWORD: rootPassXXX
      MONGO_HOST: mongodb
      MONGO_PORT: 27017
      MONGO_DATABASE: automatic
  mongodb:
    image: mongo
    container_name: mongodb
    expose:
      - 27017
    ports:
      - '27017:27017'
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=rootPassXXX
    volumes:
      - ./conf/mongodb:/data/db

volumes:
  autoc:
    external: true
    
networks: 
  default: 
    external: 
      name: automatic 

After creating those containers I am receiving from them an error: MongoServerError: Authentication failed.

This is the URI that the application is trying to connect: mongodb://root:rootPassXXX@mongodb:27017/matiautomai.

While trying to investigate what was happening, I was trying to log in to the same mongo container that initializes with the user name and password variables without the credentials using that way: mongodb://mongodb:27017/automatic, And the application manages to connect to it.

I've reviewed the docs inside the Docker hub and it seems to be that I used the environment variables in the correct way.

What am I missing here?

Upvotes: 0

Views: 395

Answers (2)

wuta
wuta

Reputation: 21

Just to add to @itzik-b's response. If you happen to be using env variables, the variables are available via process.env.

db = db.getSiblingDB(process.env.MONGO_INITDB_DATABASE);
db.createUser(
    {
        user: process.env.MONGO_INITDB_ROOT_USERNAME,
        pwd: process.env.MONGO_INITDB_ROOT_PASSWORD,
        roles: [{ role: 'readWrite', db: process.env.MONGO_INITDB_DATABASE }],
    },
);
db.createCollection("initialCollection");

Upvotes: 1

Itzik.B
Itzik.B

Reputation: 1086

After seeing your helpful comments, I found the solution for this issue.

I needed to initialize a one-time docker-compose with an initial script that located inside.

docker-compose.yaml:

version: '3.7'

services:
  mongodb:
    container_name: mongodb
    image: mongo:latest
    restart: always
    ports:
      - 27017:27017

    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: password
      MONGO_INITDB_DATABASE: root-db

    volumes:
      - ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro

This is the script that located inside ./docker-entrypoint-initdb.d/mongo-init.js:

print('start ~~~~');

db = db.getSiblingDB('yourDBname');
db.createUser(
  {
    user: 'username',
    pwd: 'password',
    roles: [{ role: 'readWrite', db: 'yourDBname' }],
  },
);
db.createCollection('users');

print('end ~~~~');

After that, you will be able to connect to mongo with the credential you have set in the .js file.

Upvotes: 1

Related Questions