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 mysql
Step 2: Create a docker network to communicate Spring boot application and MySQL database
docker network create springboot-mysql-net
docker network ls
Step 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 mysql
Step 4: Access the MySQL database in a container
docker exec -it mysqldb bash
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)
2. Deploy Spring Boot Application in a Docker Container
mvn clean package
Once 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 package
Step 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=docker
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
spring.datasource.url=jdbc:mysql://mysqldb:3306/employeedb
mvn clean package
Once 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 package
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 .
docker images
Step 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-webservices
Test 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:
Comments
Post a Comment
Leave Comment