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.
https://docs.docker.com/engine/install/ubuntu/
docker -v
docker info
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
docker images
docker network create springboot-mysql-net
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
Syntax: docker exec -it
docker exec -it 81e7 bash
mysql -u sys -p 1234
show databases;
# MySQL properties
spring.datasource.url=jdbc:mysql://mysqldb:3306/expensetracker
spring.datasource.username=sys
spring.datasource.password=1234
docker build -t springbootmysql .
docker run --network=springboot-mysql-net --name springboot-container -p 8080:8080 -d springbootmysql
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 login
command.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
Hopefully, you find this article useful. Let me know your feedback in comments section.