Deploy Spring Boot Application with MySQL Using Docker

Hiten Pratap Singh
TechCret Software
Published in
4 min readApr 25, 2020

We have already seen in an earlier blog post “Getting Started with Docker” about the basic introduction to Docker. So today we will see how to use Docker to deploy a Spring Boot Application with MySQL connectivity. For this, we will use a simple “ToDo Application” built in Spring Boot with MySQL as database.

Docker + Java + MySQL + Spring Boot

Let's get started by dividing the whole task into 3 parts.

■ Spring Boot Application:

In this part, we will first write Dockerfile for the project with all the tools/components used in the application like Java for starter. Our application will be running as jar file in the Docker container. Dockerfile is like the entry-point and using which image will be created in Docker. For our ToDo application, Dockerfile will look something like the following:

Dockerfile(ToDo Application)

You can also find above Dockerfile in the “project repository” as well.

■ Database (MySQL):

This process will be same for pretty much every resource like MySQL, Postgres, MariaDB etc with just some minor differences. Well, we can also use the MySQL’s official Docker image directly or we can use that image as base image in our Dockerfile as well. Either one is fine but in this blog post, we will go with the later one as using that way we have more flexibility at our disposal. So, the Dockerfile for MySQL will look like as the following:

Dockerfile(MySQL)

■ Stitch Everything Together:

It’s the last step where we will stitch everything together. Till now, we have seen how to write Dockerfile for both the components i.e. Spring Boot App and MySQL database. It’s time to put these pieces into action.

For applications to communicate with each other, it’s required to create user defined network in Docker and mention that one during spin-up containers rather than the default one. The same task can be achieved using — link but it’s considered as legacy feature and is not recommended to use it as well.

We will get into Networking in Docker later as that requires a separate and detailed post for itself.

Let’s stitch our Docker components step by step:

  1. First, we have to create a custom user defined network to facilitate the communication among the containers.
Docker Network

2. Second, create image for our MySQL database using the Dockerfile created earlier. So, to do that first browse into the root directory where the Dockerfile was created and then execute the following two commands in the given order.

MySQL Image + Container Commands

Here hitenpratap99/todo-mysql can be anything in the format username/image-name. Then we will just have to spin up the container using the image created above.

3. Last, we have to create the image and then spin-up the container for out ToDo application. So more or less similar to the step #2, we will do these tasks like the following:

ToDo Image + Container Commands

After all these steps, you can check the logs of any container using docker logs <container_id> command to see what is going in there. If you open the browser and browse to the http://127.0.0.1:7310/list then you will see the application up and running.

ToDo Application Screenshots

You can also checkout the repository for the ToDo application. You will find the Dockerfile for the application in the repository itself and I will mention the Dockerfile for the MySQL in the README there.

If you don’t want to manage the Dockerfile by yourself and just want to check the application by spin-up both the containers. You can do it as I have pushed both images to the project repository on GitHub. So, you can checkout both the Docker images using the following link:

If you face any issues or have any questions then just jot them down in the comment section.

--

--