jondra
jondra

Reputation: 77

MongoDB authentication doesn't work when a docker-volume is defined

I use the following lines to define a mongodb docker container with docker-compose:

mongo:
  image: mongo
  container_name: mongodb
  hostname: ${host}
  environment:
    - MONGO_INITDB_ROOT_USERNAME=${user}
    - MONGO_INITDB_ROOT_PASSWORD=${password}
  ports:
    - "${port}:27017"
  volumes:
    - ./mongodb/data:/data/db

Like this, I get an error (Authentication failed.) when I try to connect to mongodb via MongoDB Compass. After two hours of despair I found out that it works when I remove the volumes part in the docker-compose file.

What's the problem with the volume and how can I fix it?

Upvotes: 2

Views: 680

Answers (2)

Pierre Pérez
Pierre Pérez

Reputation: 11

I got the same problem. My app works well without the volumes part. I solved the issue using docker compose down --volumes (See prompt) and running again the command docker compose up.

Upvotes: 1

jondra
jondra

Reputation: 77

I just solved the problem by my self. I had to delete everything that was already in the volume directory on my host-machine (the content of the ./mongodb/data folder). I previously used the mongodb container without authentication. I assume it has something to do with that.

Upvotes: 2

Related Questions