Guide to Dockerize your Spring Boot application with MySQL database
Tags: docker, springboot, mysql, java
Published: 2022-06-30 21:27:47

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2iywz79aetrbuuvgy6f8.png)

`docker info`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ca6sq3g22smgsd0ehoj.png)
 
- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lx34z9uidu0udl418w36.png)

`docker images`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1zk4h8zv4updpeq23drv.png)

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

`docker network create springboot-mysql-net`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tdmhtgpbqbjv7dag56e8.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kd8zx1gpujkwo3k7jw5t.png) 
 
- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6i9h649eca01kw8cgyeu.png)
 
- 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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c8yugtwx6vtbhqicftdl.png)

- Now we need to build docker image from the spring boot application jar using Dockerfile. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0e7ww0neeqvwzmiqmic.png)

- Run below command to build the docker image.

`docker build -t springbootmysql .`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ve5y2siqhkc13q8rdy5m.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iljmeapaup318ge26kck.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nln0sp489wf5ok48sb0r.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17sg2rxe8wa9uza23ra2.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6r3hz7xfhiwlra55sxss.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jv1tn00xt91qmdyocu6f.png)
 

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