Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 10, 2021 08:23 pm GMT

Spring Boot CRUD - Mongo DB Docker

Alt Mongo DB + Docker

CRUD Operations using Spring Boot and MYSQL 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

EndpointMethodBodyDescription
http://localhost:8080/productsGET-Fetch all products
http://localhost:8080/products/{id}GET-Find Product by Id
http://localhost:8080/productsPOST{ "id":"1", "name":"Mobile", "Description":"Samsung Mobile." }To Add product to the DB
http://localhost:8080/productsPUT{ "id":"1", "name":"Apple Iphone", "Description":"Apple" }To update existing record.
http://localhost:8080/products/{id}DELETE-Delete the record be Id

Usage

ProductRepository.java

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

Configuration

application.properties

spring.data.mongodb.host=mongo_dbspring.data.mongodb.port=27017spring.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 mysqldocEXPOSE 8084WORKDIR /app# Copy maven executable to the imageCOPY mvnw .COPY .mvn .mvn# Copy the pom.xml fileCOPY pom.xml .# Copy the project sourceCOPY ./src ./srcCOPY ./pom.xml ./pom.xmlRUN chmod 755 /app/mvnwRUN ./mvnw dependency:go-offline -BRUN ./mvnw package -DskipTestsRUN ls -alENTRYPOINT ["java","-jar","target/springboot-demo-mysql-0.0.1-SNAPSHOT.jar"]

Original Link: https://dev.to/atharvasiddhabhatti/spring-boot-crud-mongo-db-docker-jop

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To