In this article, we’ll focus on how to dockerize a Spring Boot + MYSQL application to docker-Using Dockerfile and containers
Furthermore, we’ll show how to create a composition of containers, which depend on each other and are linked against each other in a virtual private network. We’ll also see how they can be managed together with single commands.
We will first deploy this application to docker without docker-compose. Later sessions we will also see how to achieve the same using Docker Compose.
For deploying Spring Boot + MYSQL application to docker, we will need to consider Spring Boot Application and MYSQL as two different services. So for Spring Boot Application and MYSQL to be deployed we will need two docker containers. And also these will then need to be running on the same network so that they can communicate with each other.
Before We Begin:
- Ubuntu 18.04 system with root or user with sudo privileges
- build tool Gradle Installed
- Docker Installed
In this tutorial, we will first be creating a Spring Boot + MYSQL application and build using locally installed Gradle. We will then see how this application will be deployed on docker.
step 1 –Setup MySQL docker container
Now we will create and run an image of the MySQL database. From our terminal, we will run the below command. Here,
-d in this command indicates that the Docker command will run in detached mode.
docker run -d -p 3308:3306 --name=docker-mysql --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=book_manager" mysql
As a result , MySQL image is pulled and running as a container.
To check this, we can run
You will get response as
It shows our MySQL container is UP and running.
Now we can check by logging in to MySQL.
docker exec -it docker-mysql bash;
It will take us inside the docker-mysql container. Then we will log in to MySQL using
mysql -uroot -p using password root. Then, we will run the
show databases; command to see if the database setup is complete or not.
In my case, it returns the below result:
So we can say that the book_manager database was created inside the docker container. We can externally use this database from our host machine by using port 3308
Now we have to import the database script to the Docker MySQL database. The SQL script is available here. Run the following command to import this script to docker-MySQL.
docker exec -i docker-mysql mysql -uroot -proot book_manager < ~/springbootdockercompose/sql/book_manager.sql
where ~/springbootdockercompose/sql/book_manager.sql is the absolute path of sql file on the host machine.
Hopefully, the book_manager script executed successfully. You can confirm by executing the following command.
We can see that tables are created as per the imported sql file.In next step we need to build the application by connecting database created.
Step 2- Application Clone and Build Project
I have already pushed my code to my GitHub repository. Anyone can clone the codebase from here. I have already mentioned your host machine should have Gradle setup. So now we run the gradle build command to build the project. So the executable jar file is created at the build/jar directory of your cloned project.
Note:Make sure you have build tool Gradle installed properly and check the build process and finally make sure you have the required jar file created on the target folder
You can check the gradle installation as
will get response as
After running the gradle build command from the project folder you will get jar file inside build folder like below
Now we are good to go with dockerise our application.
Next, open the Dockerfile using any text editor. We can see that the file contains the following commands:
FROM java:8 VOLUME /home/docker/spring_boot EXPOSE 10222 COPY /build/libs/book-manager-1.0-SNAPSHOT.jar book-manager-1.0-SNAPSHOT.jar ENTRYPOINT ["java","-jar","book-manager-1.0-SNAPSHOT.jar"]
This file contains sequential commands to execute in docker. It will create an image of java 8. and also it will copy jar file from host machine to docker image and then run command which is given at entry point arguments.
Note:You may need to change VOLUME location as per your machine whether it is in home directory or somewhere else
Now we will build a docker image by using this Dockerfile.
docker build -f Dockerfile -t book_manager_app .
Note:You can also run command as docker build -t book_manager_app . if we are in the same directory where the Dockerfile is present.
This command will create a Docker image named book_manager_app to the Docker machine. Here, the
-f command indicates the Docker file name.
Now we will run this image as a container.
docker run -t --link docker-mysql:mysql -p 10222:10222 --name spring_boot book_manager_app
--link command will allow the book_manager_app container to use the port of MySQL container and
-t stands for
--tty, which will allocate a pseudo-terminal.
Will create a container named spring_boot and will get the out put as below
After running this command, we will hit http://localhost:10222/book from our host machine browser, and it will return a list of book
Also, you can run the command to run the container in detached mode using -d flag after stopping the containers (You can use Ctrl+C key )
You may need to give a new name for the container this time
docker run -t --link docker-mysql:mysql -d -p 10222:10222 --name spring_boot2 book_manager_app
Check the status of the running containers using docker ps command also
Now we have successfully deployed a sample spring boot application using customized Dockerfie and running containers
In this article we learned how to run and deploy a spring boot application with MYSQL database using Dockerfile and docker containers .In upcoming session we will learn how to deploy the same application only using docker-compose and mange these two containers with single commands