Dockerize Spring Boot Application with MySQL

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.
  • 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.
  • Now go to postman or any other API testing app and hit the endpoint along with param. You should similar response as below.

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.
  • After success you should see output as below.

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.

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

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.

Dockerize Spring Boot Application with MySQL – FAQs

How do I Link my Spring Boot Application Container with a MySQL Container?

You can use Docker Compose to define a multi-container Docker application and specify the network configuration for linking containers. Docker Compose allows you to define the services (containers) that make up your application and their dependencies.

How do I Manage Environment-Specific Configurations when Dockerizing my Spring Boot Application with MySQL?

When Dockerizing a Spring Boot application, you can use environment variables to configure different environments such as development, testing, and production. Spring Boot provides support for externalized configuration through properties files, environment variables, or configuration servers like Spring Cloud Config.

Can I use Docker Volumes to Persist MySQL Data?

Yes, Docker volumes can be used to persist data generated by MySQL containers. By mounting a volume to the MySQL container, you ensure that the data persists even if the container is stopped or removed. This is crucial for preserving database state across container restarts or updates.

How do I Handle Database Schema Migrations and Versioning when Dockerizing my Spring Boot Application with MySQL?

Managing database schema migrations and versioning in a Dockerized environment involves using tools like Flyway or Liquibase to automate the execution of SQL scripts or database change logs. These tools ensure that database schema changes are applied consistently across different environments, including development, testing, and production, as part of the application deployment process.

How can I secure my Dockerized Spring Boot Application with MySQL?

Securing a Dockerized Spring Boot application with MySQL involves various measures such as securing communication channels using TLS/SSL, managing secrets securely using Docker Secrets or a secrets management tool, implementing access control and authentication mechanisms, and regularly updating dependencies to patch security vulnerabilities.



Contact Us