🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
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
Step1: Pull MySQL Image
docker pull mysqlStep 2: Create a docker network to communicate Spring boot application and MySQL database
docker network create springboot-mysql-netdocker network lsStep 3: Run MySQL image in a docker container in the same network
docker run --name mysqldb --network springboot-mysql-net -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=employeedb -d mysqlStep 4: Access the MySQL database in a container
docker exec -it mysqldb bashrameshfadatare@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)2. Deploy Spring Boot Application in a Docker Container
mvn clean packageOnce maven builds success, go target folder and you will be able to see thespringboot-restful-webservices-0.0.1-SNAPSHOT.jar file generated under the target folder.
mvn clean packageStep 5: Create Dockerfile to Build the docker image
FROM eclipse-temurin:17
LABEL mentainer="javaguides.net@gmail.com"
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
spring.profiles.active=dockerspring.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=updatespring.datasource.url=jdbc:mysql://mysqldb:3306/employeedbmvn clean packageOnce maven builds success, go target folder and you will be able to see thespring-boot-restful-web services-0.0.1-SNAPSHOT.jar file generated under the target folder.
mvn clean packageStep 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 .docker imagesStep 8: Run a docker image in a docker container in the same network
docker run --network springboot-mysql-net --name springboot-mysql-container -p 8080:8080 springboot-restful-webservicesTest CRUD RESTful WebServices using Postman Client
Create User REST API:
Request URL: http://localhost:8080/api/usersHTTP Method: POST
Request Body:{
"firstName": "ramesh",
"lastName":"fadatare",
"email": "ramesh@gmail.com"
}
Refer to this screenshot to test Create User REST API:
Request Body:
{
"firstName": "ramesh",
"lastName":"fadatare",
"email": "ramesh@gmail.com"
}Refer to this screenshot to test Create User REST API:Get User REST API:
Update User REST API:
Request URL: http://localhost:8080/api/users/1HTTP Method: PUT
Request Body:{
"firstName": "ram",
"lastName":"fadatare",
"email": "ram@gmail.com"
}
Refer to this screenshot to test the Update User REST API:
Request Body:
{
"firstName": "ram",
"lastName":"fadatare",
"email": "ram@gmail.com"
}Refer to this screenshot to test the Update User REST API:Get All Users REST API:
Request URL: http://localhost:8080/api/usersHTTP Method: GET
Refer to this screenshot to test GET All User REST API:
DELETE User REST API:
Request URL: http://localhost:8080/api/users/1HTTP 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
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business






Comments
Post a Comment
Leave Comment