skip to Main Content

I am trying to use MongoDb with SpringBoot using docker-compose, but I can’t seem to connect to the database using the username and password that i set in the docker-compose.yaml and application.properties

Below is the docker-compose.yaml.

version: "3.8"
services:
  mongodb:
    image: mongo
    container_name: mongodb
    ports:
      - 27017:27017
    volumes:
      - ./001_users.js:/docker-entrypoint-initdb.d/001_users.js:ro
      - data:/data
    environment:
      - MONGO_INITDB_ROOT_USERNAME=rootuser
      - MONGO_INITDB_ROOT_PASSWORD=rootpass
  mongo-express:
    image: mongo-express
    container_name: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=rootuser
      - ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass
      - ME_CONFIG_MONGODB_SERVER=mongodb
volumes:
  data: {}

networks:
  default:
    name: mongodb_network

application.properties:

spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=rootuser
spring.data.mongodb.password=rootpass
spring.data.mongodb.database=foober
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
spring.data.mongodb.auto-index-creation=true

I tried adding the docker-entrypoint file (001_users.js) with the code below but it didn’t fix the problem.

db.createUser(
    {
        user: "rootuser",
        pwd: "rootpass",
        roles:[
            {
                role: "readWrite",
                db:   "foober"
            }
        ]
    }
);

Also the mongodb logs show that the rootuser can’t be found in admin database, but when I go to the mongo express the user is present in the admin database.

2

Answers


  1. Chosen as BEST ANSWER

    It turns out the port 27017 was taken by another process which was causing the authentication fail... I changed the port to a free one and now it is working.


  2. If you are running your spring boot service as part of your compose stack, then change spring.data.mongodb.host to mongodb. The hostname, when referring to a service from another service, is the service name. Note that you didn’t include your service that uses spring data in your compose file, so I don’t know if that’s what you are doing.

    Or, are you running this outside of docker? You should also look at your volume mapping for data. Perhaps you should provide a real directory for the volume, and indicate that it is external in your compose file.

    Have you looked at the logs for mongodb? Does it show that a login attempt is being made? You also don’t need to set up a network with what you’re doing, so you can remove that part. Can you access mongodb with the express container you’re standing up?

    Whatever you try, make sure you completely remove all of the files in the data directory each time before you re-run if it’s not yet working. You should remove the entrypoint user creation file, because the environment variables will suffice.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search