How to Deploy a Spring Boot Application with MySQL Using Docker Compose: A Step-by-Step Guide
Deploying Spring Boot + MySQL with Docker Compose
Deploying a Spring Boot application with a MySQL database using Docker Compose simplifies the development and deployment process. Docker Compose allows you to define and run multi-container Docker applications. In this blog, we will walk through the steps to set up and deploy a Spring Boot application connected to a MySQL database using Docker Compose.
Prerequisites
Docker installed on your machine
Docker Compose installed on your machine
Basic knowledge of Spring Boot
Step 1: Create a Spring Boot Application
First, we need a Spring Boot application. If you don't have one, you can generate a new project from Spring Initializr.
Project Metadata:
Group:
com.example
Artifact:
spring-boot-mysql-docker
Dependencies:
Spring Web
,Spring Data JPA
,MySQL Driver
Download the project and extract it.
- Application Properties:
Update the application.properties
file to configure the MySQL database connection.
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
- Entity and Repository:
Create an entity class User
and a repository interface UserRepository
.
// src/main/java/com/example/springbootmysqldocker/entity/User.java
package com.example.springbootmysqldocker.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// Getters and setters
}
// src/main/java/com/example/springbootmysqldocker/repository/UserRepository.java
package com.example.springbootmysqldocker.repository;
import com.example.springbootmysqldocker.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
}
- Controller:
Create a simple REST controller to interact with the User
entity.
// src/main/java/com/example/springbootmysqldocker/controller/UserController.java
package com.example.springbootmysqldocker.controller;
import com.example.springbootmysqldocker.entity.User;
import com.example.springbootmysqldocker.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping
public List<User> getAllUsers() {
return userRepository.findAll();
}
@PostMapping
public User createUser(@RequestBody User user) {
return userRepository.save(user);
}
}
Step 2: Create Dockerfile
Next, we create a Dockerfile
to containerize the Spring Boot application.
# Dockerfile
FROM openjdk:11
VOLUME /tmp
COPY target/spring-boot-mysql-docker-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
Step 3: Create Docker Compose File
Create a docker-compose.yml
file to define the services for the Spring Boot application and the MySQL database.
version: '3.8'
services:
springboot-app:
build: .
ports:
- "8080:8080"
depends_on:
- mysql-db
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://mysql-db:3306/springbootdb
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: root
mysql-db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: springbootdb
ports:
- "3306:3306"
volumes:
- mysql-data:/var/lib/mysql
volumes:
mysql-data:
Step 4: Build and Run the Application
- Build the Spring Boot Application:
Make sure you build your Spring Boot application before running Docker Compose.
./mvnw clean package
- Run Docker Compose:
Start the services defined in the docker-compose.yml
file.
docker-compose up --build
Docker Compose will build the Spring Boot application image, start a MySQL container, and link them together. The depends_on
directive ensures that the MySQL container is started before the Spring Boot application.
Step 5: Verify the Deployment
Once the containers are up and running, you can access the Spring Boot application at http://localhost:8080
.
Test the REST API endpoints using tools like Postman or cURL.
Get all users:
GET
http://localhost:8080/users
Create a new user:
POST
http://localhost:8080/users
{
"name": "Nikhil"
}
Using Docker Compose to deploy a Spring Boot application with a MySQL database streamlines the setup process and makes it easier to manage dependencies and configurations. This approach is particularly useful for development and testing environments. For production use, additional configurations such as network settings, volumes for data persistence, and security considerations should be addressed.