Dockerizing Spring Boot + MySQL Application

Dockerizing a Spring Boot + MySQL ApplicationPhoto by Dominik Lückmann on UnsplashIn this article, we are going to discuss two ways of dockerizing a Spring Boot app with MySQL database. Those are;By creating a docker networkUsing docker-composeHere is …


This content originally appeared on Level Up Coding - Medium and was authored by Kanchana Ranmuthu

Dockerizing a Spring Boot + MySQL Application

Photo by Dominik Lückmann on Unsplash

In this article, we are going to discuss two ways of dockerizing a Spring Boot app with MySQL database. Those are;

  1. By creating a docker network
  2. Using docker-compose

Here is the application we intend to deploy to docker.

GitHub - Kanchana46/spring-boot-backend

This application contains CRUD operations for an employee-management system such as adding employees, getting employees, updating employees, and so on.

Project Structure

Project structure

One of the important files is application.properties which we provide database configurations.

And we need to have a jar of our spring boot app. To build a jar file do the following.

Right-click on pom.xml →Run As → Maven build

It will create the jar backend-0.0.1-SNAPSHOT.jar file inside the target folder, as we can see in the project structure.

We need to create the Dockerfile which includes the instruction to building the image of the spring-boot app as well.

Now we are good to go for dockerizing the application.

Deploying the application to Docker

  • Using a Docker network
  1. We need to pull the MySQL image from the docker hub with the following command. Here we will use version 5.7.
docker pull mysql:5.7

2. Create a docker image using the spring-boot app. Open the command terminal inside the project folder and do the following command.

docker build -t backend .
Docker build

We can verify whether images are created correctly using the following command.

docker images
Images

3. We will have a docker network that contains both Spring-boot and MySQL containers and these containers will communicate with each other. So the first thing we need to do is to create a docker network. Let’s create a docker network as springmysql-net.

Network

And also we can verify it with the following command.

docker network ls
Networks

4. Run the MySQL container in the network using the following.

Run MySQL container
docker run --name mysqldb --network springmysql-net -e MYSQL_ROOT_PASSWORD=1234 -e MYSQL_DATABASE=employeedb -e MYSQL_USER=sa -e MYSQL_PASSWORD=1234 -d mysql:5.7
  • Container name is mysqldb. Note that we have given this in the connection URL in the application.properties file.
  • Network is springmysql-net.
  • -e stands for environment variables. These values also have mentioned in the application.properties file.
  • -d instructs to run in detached mode.

To verify everything went fine, we can see the logs with the command below.

docker logs -f <container_name>
MySQL logs

If needed we can check if the database employeedb has been created correctly.

We can do the following commands.

- docker exec -it <container_id> bash
- mysql -u<username> -p<password>
- show databases;

If the database has been created successfully, we can see it like above.

5. Run the Spring-boot container in the same network with this command.

docker run --network springmysql-net --name backend-container -p 8080:8080 -d backend
Run Spring-boot container

You can if these containers are running correctly using the command below.

docker ps
Running containers

6. Let’s see the logs of the Spring-boot container to confirm everything is fine.

docker logs -f <container_id> 
Spring boot app logs

That’s all we need to do!.

Let’s check if our app is working as expected by doing a POST and GET request.

POST:

Endpoint → http://localhost:8080/api/employees/addEmployee

Add employee

GET:

Endpoint → http://localhost:8080/api/employees

Get employees
  • Using docker-compose

Without going through these many steps we can do the same thing with one command docker-compose.

To do that, we need to create docker-compose.yml file which includes the following.

Then Open the command terminal inside the project folder and do the following command.

docker-compose up

And let’s verify this one also by creating POST and GET requests.

POST:

Add employee

GET:

Get employees

So these are the methods you can use to dockerize a Spring boot application with MySQL. Even if you use different programming languages, databases the concept remains the same.

Hope you find this helpful.

Thank you for reading this article!.


Dockerizing Spring Boot + MySQL Application was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Kanchana Ranmuthu


Print Share Comment Cite Upload Translate Updates
APA

Kanchana Ranmuthu | Sciencx (2022-02-18T12:56:16+00:00) Dockerizing Spring Boot + MySQL Application. Retrieved from https://www.scien.cx/2022/02/18/dockerizing-spring-boot-mysql-application/

MLA
" » Dockerizing Spring Boot + MySQL Application." Kanchana Ranmuthu | Sciencx - Friday February 18, 2022, https://www.scien.cx/2022/02/18/dockerizing-spring-boot-mysql-application/
HARVARD
Kanchana Ranmuthu | Sciencx Friday February 18, 2022 » Dockerizing Spring Boot + MySQL Application., viewed ,<https://www.scien.cx/2022/02/18/dockerizing-spring-boot-mysql-application/>
VANCOUVER
Kanchana Ranmuthu | Sciencx - » Dockerizing Spring Boot + MySQL Application. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/18/dockerizing-spring-boot-mysql-application/
CHICAGO
" » Dockerizing Spring Boot + MySQL Application." Kanchana Ranmuthu | Sciencx - Accessed . https://www.scien.cx/2022/02/18/dockerizing-spring-boot-mysql-application/
IEEE
" » Dockerizing Spring Boot + MySQL Application." Kanchana Ranmuthu | Sciencx [Online]. Available: https://www.scien.cx/2022/02/18/dockerizing-spring-boot-mysql-application/. [Accessed: ]
rf:citation
» Dockerizing Spring Boot + MySQL Application | Kanchana Ranmuthu | Sciencx | https://www.scien.cx/2022/02/18/dockerizing-spring-boot-mysql-application/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.