Dockerize REST Spring-boot application with Hibernate having database as MySQL
Refer Part-1 If you need basic understanding of docker, docker setup, docker installation and want to dockerize a basic spring boot gradle application.
In this example I will be creating two Docker containers:
- A container for the MySQL database
- A container for the Spring boot application
Let’s first set up the container for the MySQL database. As Docker Hub already contains a public image provided by MySQL, All we have to do is run a container, it will automatically download the mysql image if it doesn’t already have it installed.
$ docker run --name=docker-mysql --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=test" mysql
You can name it anything but make sure to use the same name wherever needed further in the article.
In case you want to connect MYSQL container from host (via MYSQL Workbench) port let say 6603 then run below.
$ docker run -p 6603:3306 --name=docker-mysql --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=test" mysql
You should see lots of lines appear. That’s MySQL initialization when starting up as newly installed software. You should see MySQL is ready to accept connections:
[Note] mysqld: ready for connections.Version: '5.7.21' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
In case you want to run it in detached mode
$ docker run -d --name=docker-mysql --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=test" mysql
Now a docker container with MySQL is up and running. It’s time to run another docker container with Spring boot application.
Let’s create a Gradle REST Spring-boot application which uses ORM Hibernate to connect to MySQL DB. Download the application from github link here and create a Dockerfile in the project directory as below
FROM java:8VOLUME /tmpEXPOSE 8080ADD /build/libs/gradle-springboot-1.0.jar gradle-springboot-1.0.jarENTRYPOINT ["java","-jar","gradle-springboot-1.0.jar"]
Now build the docker image
$ docker build -f Dockerfile -t gradle-springboot-docker .
Once build is done, you can check the image with
$ docker images
Now lets run this container and link with mysql docker container as this application need mysql db
$ docker run -t --name gradle-springboot-docker --link docker-mysql:mysql -p 8080:8080 gradle-springboot-docker
You can see gradle spring-boot application is up and connected to other mysql docker container.
To verify if it’s linked correctly, enter into the gradle-springboot-docker container and look at the content of /etc/hosts.To get in to the container type :
$ docker exec -it gradle-springboot-docker bash
You can see “172.17.0.2 mysql 93c261be3855 docker-mysql” entry in the bash file of gradle-springboot-docker container.
This confirms docker containers are linked and Gradle Spring boot Application is up and running in one docker container and connected to another MySQL docker container.
In Part-3 We will use Gradle to build Docker Image of REST Spring-boot application and use Docker Compose file to run Springboot container and MySQL container.