Containerized Spring Boot + MySQL +Open API Docker REST API implementation

Containerized Spring Boot + MySQL +Open API Docker REST API implementation

Play this article

REST API implementation is mostly the defacto standard in the industry to develop any API. Mostly REST APIs are used by most developers. Cloud-native development makes it compulsory to develop containerized, portable applications. In this article, you are going learn how to create spring boot, Open API and MySQL REST API with a docker container.

Tech Stack:

  • Spring Boot

  • Java 11

  • Gradle

  • MySQL

  • Docker

  • Open API documentation


Let us first start with the implementation and understand the model of the database, here we have a simple database structure that contains the SQL script of two table students and classrooms where the student is part.

SQL Script:

USE student_classroom;

INSERT INTO classrooms (name, capacity) VALUES ('Mathematics', 30);
INSERT INTO classrooms (name, capacity) VALUES ('History', 25);

INSERT INTO students (name, age, email, classroom_id) VALUES ('John Doe', 20, '', 1);
INSERT INTO students (name, age, email, classroom_id) VALUES ('Jane Smith', 19, '', 2);


Set up an application using or using any of your editors. I am using VSCode editor. First, create a new Spring Boot project using the Spring Initializr, which is a web-based tool that helps you generate the basic structure of your project. You can choose to use Maven or Gradle as your build tool, depending on your preference. Here in this example we choose Gradle.

Let us check the code file one by one.


Define the data model:

Create the data model for the student and classroom entities using JPA annotations. Here's an example:

@Table(name = "students")
public class Student {
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;
   private String name;
   private int age;
   private String email;

   @JoinColumn(name = "classroom_id")
   private Classroom classroom;

   // getters and setters

@Table(name = "classrooms")
public class Classroom {
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;
   private String name;
   private int capacity;
   // getters and setters

Create the repository:

Create the repository interface for the student and classroom entities that extends JpaRepository. Here's an example:

public interface StudentRepository extends JpaRepository<Student, Long> {

public interface ClassroomRepository extends JpaRepository<Classroom, Long> {

Create the REST controller:

Create the REST controller that handles HTTP requests and responses for the student and classroom entities. Here's an example:

public class SchoolController {
   private StudentRepository studentRepository;
   private ClassroomRepository classroomRepository;

   public List<Student> getAllStudents() {
       return studentRepository.findAll();

   public Student createStudent(@RequestBody Student student) {

   public List<Classroom> getAllClassrooms() {
       return classroomRepository.findAll();

   public Classroom createClassroom(@RequestBody Classroom classroom) {

Create the Dockerfile:

Create a Dockerfile that defines the environment and dependencies for your application. Simple docker file, for production definitely you can follow best practices.

FROM openjdk:8-jdk-alpine
COPY target/school-0.0.1-SNAPSHOT.jar school.jar
ENTRYPOINT ["java","-jar","/school.jar"]

Create build.gradle file:

Create the build gradle file, which is helping us to define the build instructions. Here's an example:

plugins {
    id 'org.springframework.boot' version '2.6.3'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
java.sourceCompatibility = '11'

repositories {

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.springdoc:springdoc-openapi-ui:1.6.3'
    implementation 'org.springdoc:springdoc-openapi-data-rest:1.6.3'

test {

bootRun {
    args = ['']

Create Application java file:

The application java file is an entry point for the application to start. Here's an example:

  info = @Info(
    title = "Student and Classroom API",
    version = "1.0",
    description = "API for managing students and classrooms"
public class MyApplication {

    public static void main(String[] args) {, args);

Run the application:

If you want to run the application using the docker file only then run the below command and validate.

docker build -t school .
docker run -p 8080:8080 school

Docker compose:

Docker compose file which contains both MySQL database and spring application container to run together.

version: '3.9'

    image: mysql:8.0.28
    container_name: mysql
    restart: always
      MYSQL_ROOT_PASSWORD: example
      MYSQL_DATABASE: student_classroom
      - "3306:3306"
      - ./mysql-data:/var/lib/mysql
      - ./mysql-scripts:/docker-entrypoint-initdb.d
      context: .
      dockerfile: Dockerfile
    image: myapp
    container_name: myapp
      - mysql
      - "8080:8080"

Run docker-compose using the below command

docker-compose up

Now enjoy the application by hitting the REST endpoint using the postman or curl command.

This will start the MySQL container and the Spring Boot app container. You should see logs from both containers in the terminal.

Once the containers are running, you can access the Spring Boot app by opening a web browser and navigating to http://localhost:8080. If everything is working correctly, you should see the default Spring Boot landing page.

I hope this helps, you!!

More such articles:


If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!