How to Dockerize your spring boot with mysql project

1. Deploy MySQL Image in a Container

Let's first deploy the MySQL image in a docker 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

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
mysql -uroot -proot

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.

5. Deploy Spring Boot Application in a Docker Container

mvn clean package

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 openjdk:17-jdk-slim
VOLUME /tmp
ARG JAR_FILE=target/middleware-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

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

Docker Composer => chat gpt
https://chat.openai.com/share/b49f8f64-315d-40be-9fb6-96a4c340b1dc