DEV Community

Cover image for Guide to Dockerize your Spring Boot application with MySQL database
Devanand Ukalkar
Devanand Ukalkar

Posted on • Edited on

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 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

  • I am using MySQL database in this application, so we will have to first create docker network for the MySQL database.

  • Pull the MySQL image from docker hub and check if its available locally.

docker pull mysql:5.7
Image description

docker images
Image description

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

docker network create springboot-mysql-net
Image description

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

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 MySQL container is running, Lets check if the database is created using interactive mode. To connect to mysqldb container in interactive mode, you can use below command with few initial characters from the container id.

Syntax: docker exec -it <container-id> bash

docker exec -it 81e7 bash
mysql -u sys -p 1234
show databases;

Image description

  • With MySql 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
Enter fullscreen mode Exit fullscreen mode

Image description

  • Now we need to build docker image from the spring boot application jar using 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 using docker login command.
    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.

Top comments (1)

Collapse
 
gregorgonzalez profile image
Gregor Gonzalez

What a coincidence, right now I'm doing several exercises with spring + mysql and I wanted to try it with docker and this article just popped up on Twitter 👍
Really well explained, now I'll try it. Thx