Spring Boot Mysql Docker Compose Example, easy in 15 minutes

This post demonstrates a Spring Boot Mysql Docker Compose Example. You will learn how to use Docker-compose to dockerize a Spring Boot Restful API application. Docker Compose is a tool for defining and running multi-container Docker applications. With Docker Compose, you use a YAML file to configure your all application’s services. Let’s dig deeper.

Spring Boot Mysql Docker Compose Example

Install Docker-Compose

first, install the Docker for your OS as described on the Get Docker page.

To install docker-compose on Linux Mint (Ubuntu) please follow instructions: https://docs.docker.com/compose/install/#install-compose

Post-installation do check the docker-compose version

Working with Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. With Docker Compose, you use a YAML file to configure your all application’s services. Then, with a single command, you create and start all the services from your configuration. That YAML file is called docker-compose.yml . For example, to run a spring boot application, you could use the docker-compose.yml file with the configuration like this:

The above configuration creates a specific container and it exposes a specific port. To run that container, you just hit the command docker-compose up then access the application through http://localhost:8080.

In reality, our application usually connects to third-parties, e.g: MySQL, ActiveMQ,…Now we create a container for MySQL and link it with springboot-mysql-app container above in the same docker-compose.yml file.

In the above configuration, we create a MySQL container like a database. You see the depends_on property, this property express dependency between services and makes sure that Docker will start MySQL container before springboot-mysql-app container. Moreover, you can access the container via its name, e.g jdbc url look like : jdbc:mysql://mysql-db/dockerdemo.

Store the sensitive data in a separated file

Docker Compose supports declaring default environment variables in an environment file named .env placed in the folder where the docker-compose command is executed (current working directory). We will create a .env file that stores database account like this

Now, we update the configuration file using .env file

Manage data in Docker

When you run the docker-compose up command, Docker will destroy your all containers and recreate it, so your current data will be lost. Don’t worry, you can fix this issue by mapping a specific volume in your docker-compose.yml file. Here is a sample with MySQL container

In this case, your database will be stored in the folder /mysql-data in your host machine.

Spring Boot Mysql Docker Compose Example

Here, we will dockerize a Spring Boot Restful API application from the project source code Spring Boot JPA MySQL CRUD Example, the project structure like below

Spring Boot Mysql Docker Compose Example

Docker Compose working folder structure

We create a working folder name docker-compose, it contains the docker-compose.yml file, .env file, other folders:

  • mysql-data (store MySQL database files) folder.

  • mysql-init folder stores our SQL script for initializing database, this script:

  • springboot-mysql-docker-compose is project folder of our Spring Boot Restful API application

Dockerize application uses Docker compose

Open the console and go into the folder docker-compose then run the command like below

docker-compose build

This docker-compose build command builds our containers that are defined in the docker-compose.ymlfile.

Now, we run our application by running the docker-compose up command, you will see the output like below

So far, our application is deployed successfully on the Docker container. You can verify this Spring Boot Restful application with the Postman tool, like this:

The above image shows that we just created an employee using Restful api of our application.

Next, we query the employee above, like this:

 

You can verify the employee record above by access directly in the console, please see the below screenshot:

That’s all about Spring Boot Mysql Docker Compose Example.

If you find this post useful, don't hesitate to share it with your friends or other people for growing knowledge together and is an effort to contribute us.

References

Docker Compose
Spring Boot Docker using Maven Plugin

Download complete source code

docker-compose.zip (218 downloads)
Please share it if you found this useful
Hide Buttons