How to run a Spring Boot microservice application and a MySQL instance using docker compose?

rizahorasan
4 min readMay 31, 2021

In this simple tutorial, we will see how easy to run a Spring Boot microservice application and a MySQL instance in a docker container using docker-compose.yml file.

To get a running environment, I will use Docker Desktop for Windows (when Docker Desktop is installed, you will have Docker Engine, Docker CLI client and Docker Compose), docker-hub, Spring Tool Suite (STS), GitBash, Visual Studio Code, Postman and MySQL Workbench on a Windows 10 machine.

I will keep the tutorial as simple as possible so I will not go into the details of tool setups and Spring Boot application.

Let’s start.

I will create two environments. In the first one, we will have Spring Boot application and MySQL instance which are running seperately. Second enviroment will be created using docker-compose.yml so application and MySQL instance will be running in the same docker network.

Environment 1

Environment 1

After downloading, import the customer-service application as a Maven Project in the Spring Tool Suite.

Update the application.properties file as following.

Do not run the application yet. We need the following MySQL instance for customer-service to run.

  • MySQL instance will run inside a docker container. MySQL instance is running on port 3306. But docker container serves this MySQL service on port 5005 for other clients. You can use the following link to create the MySQL container.
  • Now, we have a running MySQL container. Go to Spring Tool Suite, run the customer-service as a Spring Boot application. You should see that the customer-service is running on port 8001.
customer-service on port 8001
  • Environment 1 is ready.
  • Go to Postman and make a POST call to http://localhost:8001/customerapi/customer endpoint. You should get a Status 200 OK response. You can verify that a new customer is created using MySQL Workbench.
create customer api call
get all customer api call

Environment 2

Environment 2

We will create the environment using docker-compose.yml so application and MySQL instance will be running in the same docker network.

Firstly, we will create a new docker image for customer-service application and push it to docker hub.

  • Go to Spring Tool Suite and update the application.properties as following. As you can see customer-service will be using docker-mysql as the hostname. This is the MySQL instance service name in the docker-compose.yml file.
  • Open GitBash and go to your customer-service project directory. Then run the following command to build the application and create the artifacts (-DskipTest: without running the tests).
  • In the same directory, run the following command to create a new docker image.

You can run docker images command and see the new image as customer-service-image.

  • Run the following two commands to push customer-service-image to docker hub. Do not forget to use your docker hub user name. Pushing the image to docker hub may take a few minutes.
  • Now, our new customer-service-image docker image is ready. Let’s go and create the second environment using docker-compose.yml file (which is in the root folder of the customer service application). Run the following command. customer-service-image container may restart a few times.
  • Environment 2 is ready.
  • Go to Postman and make a POST call to http://localhost:6006/customerapi/customer endpoint. You should get a Status 200 OK response. You can verify that a new customer is created using MySQL Workbench.

That’s all. You have a running environment where Spring Boot application and MySQL instance are created with docker-compose.yml file.

--

--

rizahorasan

software developer, software architect, software something.