Cannot connect to my own DB in a container, Authentication failed

Hey guys,

I’m trying to set up a pretty simple MongoDB with docker-compose, here’s my compose-file:

version: '3.8'

services:
  mongo:
    image: mongo:latest
    container_name: contact_mongo_container
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    volumes:
      - contact_mongo_data:/data/db
    ports:
      - 27017:27017

  second_mongo:
    image: mongo:latest
    container_name: buy_mongo_container
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    volumes:
      - buy_mongo_data:/data/db
    ports:
      - 27018:27017

volumes:
  contact_mongo_data:
  buy_mongo_data:

Im able to run the container in container desktop without any issues, but Im not able to sign into my database, I always get the error message “Authentication failed” .Here’s the connection string I am using: mongodb://root:example@localhost:27017/

Do you know what I might be doing wrong?

Thank you for your help.

Cheers,

Gabe