Open In App

Dockerize Spring Boot Application with MySQL

Last Updated : 14 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Spring boot is the most modern framework used for building microservice-based applications today. Deploying spring boot applications requires multiple steps which require complex infrastructure and tools. Docker helps with the easy deployment of spring boot applications with the help of containers. So let's see how we can dockerize a spring boot application using docker.

Primary Terminologies

  • Containers: Containers are runtime units in docker that contain application code along with dependencies for the code.
  • Docker: Docker is a platform for building and running containerized applications.
  • Spring Boot: It's a framework built over spring to build modern and secured microservices and web applications.

Dockerize Spring Boot Application with MySQL

Create Sample Spring Boot Application

Before proceeding let's create a simple spring boot application that uses MySQL database.

  • Go to the Starter Spring website and create a sample application.
  • Add Spring Web, Spring JDBC, Spring JPA, and Mysql driver as dependencies. Once done click on generate.
f5390813-c77e-4047-a389-b8f63081881e-min
  • Extract the downloaded zip and open it in your favourite editor.
  • Now lets create a sample REST controller that has mapping "/hello".
Java
package com.example.demoGFG.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class DemoController {
    
    @RequestMapping(value="/hello", method=RequestMethod.GET)
    public String requestMethodName(@RequestParam String param) {
        return "Hello"+param;
    }   
}
  • This endpoint will accept one parameter and return "Hello Param" as a response.

Connect to MySQL Database

Now lets add configuration for MySQL Database.

  • open application.properties file and then specify following properties.
spring.application.name=demoGFG


spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/sys
spring.datasource.username=root
spring.datasource.password=adminpass
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  • Make sure you have MySQL installed on your local machine and it is running.

Test the application

  • After all the configuration is done. Run the application as spring boot app.
dc126620-9271-4c1c-af82-5594e91709ea-min-(1)
  • Now go to postman or any other API testing app and hit the endpoint along with param. You should similar response as below.
6287f6d5-3121-4c15-82d7-97bdfa07ead9-min

Create Docker Image

Now lets create docker image for our application.

  • For this application we will use locally hosted MySQL database. We can also use MySQL hosted in docker.
  • Change the application.properties and update the database URL as below. For MacOS add docker.for.mac.host.internal. For Linux start container by adding -add-host host.docker.internal:host-gateway.
spring.application.name=demoGFG


spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://docker.for.mac.host.internal:3306/sys
spring.datasource.username=root
spring.datasource.password=adminpass
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  • After updation we are ready to build the image.
  • Go to pom.xml of application and add below lines to maven plugin.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<name>deepcodr/simplespringapp-${project.artifactId}:${project.version}</name>
</image>
<pullPolicy>IF_NOT_PRESENT</pullPolicy>
</configuration>
</plugin>
  • The above code specifies name of docker image along with pullPolicy which is IF_NOT_PRESENT this allows docker to pull images only if they are not present already.
  • Once done with configuration. Run custom maven command "spring-boot:build-image -DskipTests".
  • Docker image should start building as below.
Building the application
  • After success you should see output as below.
Build success

Run the Image on Docker Engine

Once the docker image is built successfully run it on docker using below command.

docker run -p 8080:8080 <YOUR IMAGE NAME>

You should see the application is starting correctly and connecting to local MYSQL.

docker image

After successful start go to postman and hit the URL you should see endpoint response.

Acesess the application

Conclusion

In this article we have seen how we can build and deploy spring boot application in a containerized manner on docker along with MySQL as a database. We have seen proper steps and implementation methods to dockerize the spring boot application. Microservices and more complex spring boot applications can be deployed to docker using few modifications to above methods.


Next Article
Article Tags :

Similar Reads