Authenticate mongodb with Docker

Asked

Viewed 69 times

0

I’m starting studies with Docker and mongodb, I can’t authenticate, I would like to have a user and password for mongodb (container). If you can guide me how to create a user and password, I have to do this in the container before going up with authentication or it is possible via Docker-Compose?

The mistake I have when trying to upload the Node project is this:

Mongonetworkerror: failed to connect to server [db:27017] on first connect [{ Mongoerror: Authentication failed.

Without authenticating everything ok, but I believe that by serial security ideal have a user and password in the bank and also even with volume the data are zeroed, something is dropping the base automatically, I honestly do not know where yet!

Below is an excerpt "? authSource=admin" where through searches, videos and tutorials I am trying to use, but I did not understand what this would be, if it would be a user, protocol, anyway!

db.js

mongoose.connect(
 `mongodb://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}?authSource=admin`,
  {
   useNewUrlParser: true,
  });

Docker-Compose.yml

version: "3.7"
services:
  db: 
    image: mongo:latest
    container_name: db
    restart: always
    ports: 
      - ${DB_PORT}:27017
    environment:
      MONGO_INITDB_DATABASE: ${DB_NAME}
      MONGO_INITDB_ROOT_USERNAME: ${DB_USER}
      MONGO_INITDB_ROOT_PASSWORD: ${DB_PASS}
    volumes:
      - './dockervolume/mongodb:/data/db'

.env

NODE_ENV=development

#SERVER
SERVER_PORT=3001

#DB
DB_HOST=db
DB_PORT=27017
DB_NAME=banco
DB_USER=api
DB_PASS=api

1 answer

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.