cft

Spring Boot CRUD - Mongo DB + Docker

Spring boot and Mongo DB integration. MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License


user

Atharva Siddhabhatti

3 years ago | 2 min read

CRUD Operations using Spring Boot and Mongo DB Database using docker-compose.

Docker is required to be install on the machine. Install Docker from here

Installation

  1. Clone the Repo from here Click Here & run the following commands.
docker-compose up

Output

Usage

ProductRepository.java

@Repositorypublic interface ProductRepository extends MongoRepository<Product, Integer> {}

Configuration

application.properties

spring.data.mongodb.host=mongo_db
spring.data.mongodb.port=27017
spring.data.mongodb.database=mydb

docker-compose.yml

version: '3.3'services:
#service 1: definition of mongo database
mongo_db:
image: mongo
container_name: mongoDB
restart: always
ports: - 27017:27017
#service 2: definition of your spring-boot app
productservice:
#it is just a name, which will be used only in this file. image: product-service #name of the image after dockerfile executes container_name: product-service-app
#name of the container created from docker image
build: context: . #docker file path (. means root directory) dockerfile: Dockerfile #docker file name
ports: - "8080:8080" #docker containter port with your os port
restart: always depends_on: #define dependencies of this app - mongo_db #dependency name (which is defined with this name 'db' in this file earlier)

Dockerfile

FROM openjdk:11 as mysqldoc
EXPOSE 8084
WORKDIR /app
# Copy maven executable to the image
COPY mvnw .COPY .mvn .mvn # Copy the pom.xml fileCOPY pom.xml .# Copy the project source
COPY ./src ./src
COPY ./pom.xml ./pom.xml
RUN chmod 755 /app/mvnw
RUN ./mvnw dependency:go-offline -B
RUN ./mvnw package -DskipTestsRUN ls -al ENTRYPOINT ["java","jar","target/springboot-demo-mysql-0.0.1-SNAPSHOT.jar"]

Upvote


user
Created by

Atharva Siddhabhatti

I am Currently working in Infogain India Pvt. Ltd. as Software Engineer. I am working in multiple technologies such as Spring Framework, Azure Cloud, GCP, React Native, React JS. In my spare time I like to listen to music and play guitar.


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles