Deploy Spring Boot MySQL Application to Docker

In this tutorial, we will learn step by step how to Dockerize Spring Boot MySQL Application.

For deploying Spring Boot + MySQL application to docker, we will need to consider Spring Boot Application and MySQL as two different services. Basically, we are going to deploy the Spring Boot Application service and MySQL service into two separate docker containers. And we will run these containers in the same Docker network so that they can communicate with each other. 

Prerequisites

First, you need to set up the Spring boot CRUD project using below GitHub repository:

Tutorial for above GitHub repository: Spring Boot CRUD RESTful WebServices with MySQL Database

Deploy Spring Boot MySQL Application to Docker

Since are going to create two docker containers that should communicate with each other, we will need to start them on the same Docker network. 

1. Deploy MySQL Image in a Container

Let's first deploy the MySQL image in a docker container.

Step1: Pull MySQL Image

Here is the docker command to pull the latest MySQL docker image:
docker pull mysql

Step 2: Create a docker network to communicate Spring boot application and MySQL database

Here is the docker command to create a new network:
docker network create springboot-mysql-net
Here springboot-mysql-net is the network name.

Use the below command to list the networks:

docker network ls

Step 3: Run MySQL image in a docker container in the same network

Here is the docker command to run MySQL image in a container in the same network:
docker run --name mysqldb --network springboot-mysql-net -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=employeedb -d mysql

Step 4: Access the MySQL database in a container

Here is the command to access the MySQL database in a container:
docker exec -it mysqldb bash
For example:
rameshfadatare@Rameshs-MacBook-Air springboot-restful-webservices % docker exec -it mysqldb bash
bash-4.4# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 8.0.31 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| employeedb         |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.08 sec)
That's it. Once the MySQL image is deployed in a docker container. Next, we will deploy the Spring boot application in a docker container.

2. Deploy Spring Boot Application in a Docker Container

I assume that you have downloaded the Spring boot project from the GitHub repository and set up the project in your IDE.

Next, use the following command to maven build this project:

mvn clean package
Once maven builds success, go target folder and you will be able to see the
springboot-restful-webservices-0.0.1-SNAPSHOT.jar file generated under the target folder.

Step 5: Create Dockerfile to Build the docker image

Docker builds images automatically by reading the instructions from a Dockerfile. The Dockerfile is a text file that contains all commands, in order, needed to build a given image. 

Let's go to the project root directory and create a file named Dockerfile and the following content to it:
FROM eclipse-temurin:17

LABEL mentainer="[email protected]"

WORKDIR /app

COPY target/springboot-restful-webservices-0.0.1-SNAPSHOT.jar /app/springboot-restful-webservices.jar

ENTRYPOINT ["java", "-jar", "springboot-restful-webservices.jar"]

FROM: A docker image can use another image available in the docker registry as its base or parent image. In the above example, we use the openjdk:11 image as our base image.

LABEL: The LABEL instruction is used to add metadata to the image. In the above Dockerfile, we have added some info about the maintainer of the image through LABEL instruction.

WORKDIR: This instruction creates a working directory in a docker container.

COPY: The COPY instruction copies new files or directories and adds them to the filesystem of the container at the path.

ENTRYPOINT: This is where you configure how the application is executed inside the container.

Step 6: Adding Profile to Deploy in Docker Environment

Let's implement the profile in the Spring boot application to deploy it in a docker environment.

Let's create an application-docker.properties file under the resources folder and the below property to active docker profile in the application.properties file:
spring.profiles.active=docker
Next, let's change the application-docker.properties file as per the docker environment:
spring.datasource.url=jdbc:mysql://mysqldb:3306/employeedb
spring.datasource.username=root
spring.datasource.password=root

#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
The localhost won't work in the docker network so let's change it to the container name that is mysqldb.
spring.datasource.url=jdbc:mysql://mysqldb:3306/employeedb
Also, no need to configure MySQLDialect in Spring boot 3, Hibernate 8 will automatically configure dialect based on the database that we are using.

Next, use the following command to maven build this project:

mvn clean package
Once maven builds success, go target folder and you will be able to see the
spring-boot-restful-web services-0.0.1-SNAPSHOT.jar file generated under the target folder.

Step 7: Build Docker Image from Dockerfile

Now that we have defined the Dockerfile, let’s build a docker image for our application.

Before building the docker image, you need to make sure that you’ve packaged the application in the form of a jar file using maven. 

Let’s now build the docker image by typing the following command:

docker build -t springboot-restful-webservices .
The file path defines the location of the Dockerfile in the current directory, and the -t argument tags the resulting image, where the image name is the springboot-restful-webservices and the tag is the latest.

After the build is successfully finished, we can check to see if it appears in the list of docker images available locally. To do so, we can execute the below command.
docker images

Step 8: Run a docker image in a docker container in the same network

Once you have a docker image, you can run it using the docker run command like so:

docker run --network springboot-mysql-net --name springboot-mysql-container -p 8080:8080 springboot-restful-webservices
Note that we are running the Spring boot application in a container in the same docker network (springboot-mysql-net).

Test CRUD RESTful WebServices using Postman Client

Create User REST API:

HTTP Method: POST
Request Body:
{
    "firstName": "ramesh",
    "lastName":"fadatare",
    "email": "[email protected]"
}
Refer to this screenshot to test Create User REST API:

Get User REST API:

HTTP Method: GET

Refer to this screenshot to test GET User REST API:

Update User REST API:

HTTP Method: PUT
Request Body:
{
    "firstName": "ram",
    "lastName":"fadatare",
    "email": "[email protected]"
}
Refer to this screenshot to test the Update User REST API:

Get All Users REST API:

HTTP Method: GET

Refer to this screenshot to test GET All User REST API:

DELETE User REST API:

HTTP Method: DELETE

Refer to this screenshot to test Delete User REST API:

Source Code on GitHub

The source code of this tutorial is available on my GitHub repository at https://github.com/RameshMF/springboot-docker-course/tree/main/springboot-restful-webservices

Conclusion

In this tutorial, we have seen step-by-step how to deploy Spring Boot MySQL Application to docker using the Docker network.

Further Reading

Comments