Connect spring-boot with MongoDB database using Docker.
Sep 18, 2021
mongo.yml file configuration:-
version: '3'
services:
mongodb:
image: mongo:latest
restart: on-failure
volumes:
- $HOME/Documents/udaanDb:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
ports:
- 27017:27017
Now run the mongo.yml file. To run the file:
docker-compose -f <file-location> up -d
# Now check for the mongo dependency in pom.xml file. If the below dependency is not there add it.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
- That’s all in dependency for now. Let’s configure the application-properties file now.
server.port = 8080
spring.data.mongodb.uri=mongodb://root:example@0.0.0.0:27017/?authSource=admin&authMechanism=SCRAM-SHA-1
spring.data.mongodb.database=udaanUserDb