I am trying to secure a mongoDB docker container and am stumped by the authentication. So far, in my docker compose file, I am setting root environment variables, but I am unable to connect to the mongoDB from outside of the container. Without setting auth, I have no such issues.
services:
  mongodb:
    image: mongo:latest
    restart: always
    ports:
      - "27017:27017"
    command: ["--auth"]
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=root
I've tried to connect to mongodb://root:root@localhost:27017 using MongoDB Compass tool, but I keep getting authentication errors.
I am assuming that the root user is already created since I am getting authentication errors. I have tried using a script to create a user, but still I could not connect.
 
     
    