ПІДТРИМАЙ УКРАЇНУ ПІДТРИМАТИ АРМІЮ
Uk Uk

Guide to Dockerize your Spring Boot application with MySQL database

Guide to Dockerize your Spring Boot application with MySQL database

So, you want to build a Java Spring Boot application and run it inside a Docker container? This...

So, you want to build a Java Spring Boot application and run it inside a Docker container? This article will provide you step by step guide to build MySQL Database & spring boot application images to run it in a container.

I will be using AWS EC2 Linux instance in this article, but all the commands will be similar on other OS except the installation part.

  • First step is to have docker installed on the machine that you are using. Follow steps on below link for installation.

https://docs.docker.com/engine/install/ubuntu/ 

  • Once Docker is installation is finished, check if its installed correctly.

docker -v

Image description

docker info

Image description

  • We are using MySQL database in our application, so we will have to first create docker network for the MySQL database.

  • Pull the MySQL image from docker hub and check if the image is available

docker pull mysql:5.7

Image description

docker images

Image description

  • Create docker network to communicate Spring boot application with MySQL database.

docker network create springboot-mysql-net

Image description

  • Run the MySQL container in the network and wait for few

docker run -it --name mysqldb --network=springboot-mysql-net -e MYSQL_ROOT_PASSWORD=1234 -e MYSQL_DATABASE=expensetracker -e MYSQL_USER=sys -e MYSQL_PASSWORD=1234 -d mysql:5.7

Image description

  • Now that our MySQL container is running, we will check if the database is created using interactive mode. To connect to mysqldb container, you can use below command with few initial characters from the container id.

Syntax: docker exec -it bash

docker exec -it 81e7 bash

mysql -u sys -p 1234

show databases;

Image description

  • With our database running in container now, lets move on to update the application properties to connect to this database.

# MySQL properties
spring.datasource.url=jdbc:mysql://mysqldb:3306/expensetracker
spring.datasource.username=sys
spring.datasource.password=1234

Image description

  • Now we need to build docker image from the spring boot application jar file & it will be created using below Dockerfile.

Image description

  • Run below command to build the docker image.

docker build -t springbootmysql .

Image description

  • So, We have our mysql database container running and we have built docker image for the application, lets start the spring boot container on the same network as that of the database one.

docker run --network=springboot-mysql-net --name springboot-container -p 8080:8080 -d springbootmysql

Image description

Image description

  • As you can see, our spring boot application is now running in a container.

  • Lets see how we can push this image to docker hub. You will first need to create your account https://hub.docker.com/ 

    Once account is created, login to docker hub usingdocker logincommand.

    Image description

  • Once login is successful, you can use below command(change name as per your login & image) to push the image to docker hub.

docker push devanandukalkar/springbootmysql

Image description

Image description

Hopefully, you find this article useful. Let me know your feedback in comments section.

Ресурс : dev.to


Scroll to Top