Spring Boot MySQL Docker Compose Example

 Spring Boot MySQL Docker Compose Example

Introduction

        I was looking online for a simple example/tutorial on how to dockerize my existing Spring Boot and MySQL Database Microsevice using Docker Compose/Docker. Most of the tutorials have complicated examples/complex steps explained. Hence I have come up with this post to explain about Docker Compose and its usage in simple steps. 

        In this tutorial we will be dockerizing an existing SpringBoot fat jar with database related configurations along with MySQL database, building it and running the microservice.

        To learn about Docker, simple Docker commands, Dockerfile, dockerizing a simple application and Docker Hub, please read my blog post: Dropwizard Docker Example.

Docker Compose

        Compose is a tool for defining and running multi-container Docker applications. Using Compose, a single YAML file can be used to configure your application’s services. Then, with a single command, all the services can be created and started from your configuration. Typical use case for this is to run a microservice. In this post I am using it for a Spring Boot application server and a database.

More details about Docker Compose can be read from: Docker Docs Website.

Requirements to Run the Spring Boot MySQL Microservice on Docker:
  1. Spring Boot MySQL Application
  2. Docker
 I am assuming that you have a basic knowledge of Spring Boot and have a Basic Spring Boot MySQL Application running in your machine. If not, please check my blog post on Spring Boot MySQL Integration by going to the link: Spring Boot MySQL Integration Tutorial. This application should be created on your machine before running it on Docker. It should run successfully using the command:
java -jar target\SpringBootMySQL-1.0.0.jar src\main\resources\application.yml

Docker should be setup and running in your machine. Docker Compose is packaged along with Docker and there is no separate installation for it. To setup, run and test if Docker is working fine, please refer to my post on: Docker Setup

Once the above setup is completed, the Spring Boot MySQL microservice can be run on Docker in a few simple steps.

Step 1: Create Dockerfile

Create a file named as Dockerfile to give instructions to Docker how to build the image. This file should be placed in the root of your project directory. Here is a basic Dockerfile that I have created for my Spring Boot MySQL application:
      FROM openjdk:8-jdk

      COPY /target/SpringBootMySQL-1.0.0.jar /data/SpringBootMySQL/SpringBootMySQL-1.0.0.jar

      WORKDIR /data/SpringBootMySQL

      RUN java -version

      CMD ["java","-jar","SpringBootMySQL-1.0.0.jar"]

      EXPOSE 4000-4001 
   
Here is a brief explanation for the Dockerfile commands: 
FROM openjdk:8-jdk: I am using this as the base image.
COPY: This is used to copy artifacts from my project. This is based on your project structure. 
WORKDIR: Set the working directory as a best practice. 
RUN: To check Java version. 
CMD: The command used to run the jar. 
EXPOSE: To expose ports for the Spring Boot MySQL service. 

Step 2: Create Docker Compose yml file

Create a file named as docker-compose.yml to define the services that make up our microservice so that they can run together in an isolated environment. This file should be placed in the root of your project directory. Here is a basic docker-compose.yml that I have created for my Spring Boot MySQL application:
# Docker Compose file Reference (https://docs.docker.com/compose/compose-file/)

version: '2.1'

# Define services
services:
  # App backend service
  app-server:
    # Configuration for building the docker image for the backend service
    build:
      context: . # Use an image built from the specified dockerfile in the `springboot-app-server` directory.
      dockerfile: Dockerfile
    ports:
      - "4000:4000" # Forward the exposed port 4000 on the container to port 4000 on the host machine
    restart: always
    depends_on: 
      - db # This service depends on mysql. Start that first.
    environment: # Pass environment variables to the service
      SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/softwaredevelopercentral?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
      SPRING_DATASOURCE_USERNAME: ajtechdeveloper
      SPRING_DATASOURCE_PASSWORD: ajtechd3v3l0p3r     
    networks: # Networks to join (Services on the same network can communicate with each other using their name)
      - backend

  # Database Service (Mysql)
  db:
    image: mysql:5.7
    ports:
      - "3306:3306"
    restart: always
    environment:
      MYSQL_DATABASE: softwaredevelopercentral
      MYSQL_USER: ajtechdeveloper
      MYSQL_PASSWORD: ajtechd3v3l0p3r
      MYSQL_ROOT_PASSWORD: r00t
    volumes:
      - db-data:/var/lib/mysql
    networks:
      - backend  

# Volumes
volumes:
  db-data:

# Networks to be created to facilitate communication between containers
networks:
  backend: 
    
I have added comments in most of the lines above. Points to note:
  1. I have set context as . indicating that the docker file created in the previous step is available in this same folder. 
  2. I have referenced the docker file created in the previous step in the field dockerfile
  3. environment is used to specify any environment variables required. I have defined required environment variables for both app-server and db 
  4. ports is used to define ports to be used. I have specified 4000 as port for Spring Boot MySQL Application and 3306 as port for MySQL DB.
  5. Under db I am specifying image to pull for MySQL Database. I have given this as mysql:5.7
  6. networks is used to define a network on which the application and database can communicate. I have defined this as backend
  7. depends_on is used to specify any dependencies. I have defined app-server depends on db

Step 3: Docker Compose Run

My Docker uses the IP 192.168.99.100. This IP can be found from the first line in the terminal when Docker starts as per the image below:
docker-compose up
Access the Docker MySQL Database by using Windows Command Prompt in the location where you have MySQL installed in your machine and use the docker IP address 192.168.99.100. For me it is: D:\Programs\mysql-5.7.19-winx64\bin 
D:\Programs\mysql-5.7.19-winx64\bin>mysql -u root -p -h 192.168.99.100
Enter password as r00t, which is defined in the file: docker-compose.yml

My MySQL Docker container name is springbootmysql_db_1. You can also use docker command below to access the Docker MySQL database:
docker exec -i springbootmysql_db_1 mysql -uroot -p
Here are a few commands to execute after logging into the MySQL Database:
  > show databases;
  > use softwaredevelopercentral;  
In this post, I am using a simple Employee Table to perform CRUD operations. Here is the script to create this table in the MySQL Database:
create table employee (
id int not null auto_increment primary key,
name varchar(50),
department varchar(50),
salary int(20)
);  

API calls when the microservice is running:

1. POST API to create an employee
    JSON Request Body:
  {
   "name": "Mary",
   "department": "Accounts",
   "salary": 10000
  }

2. GET API to get all employees:

3. GET API to get employee by NAME

4. PUT  API to update an employee
    http://192.168.99.100:4000/employee
    JSON Request Body:
    {
        "id":1,
        "name": "Mary",
        "department": "Accounts",
        "salary": 12000
    }
5. DELETE API to delete an employee by ID

Other than these APIs, this application has the following APIs:

1. GET API to Ping and test if the application is up and running:

2. POST API to Ping and test if the application is up and running:
    JSON Request Body:
    {
     "input": "ping"
    }

Docker Compose Stop

Run the command below in the location where the file: docker-compose.yml resides to stop the related containers and the entire microservice:
docker-compose down

Build the Docker Image

To build a Docker Image of this microservice which can be pushed to Docker Hub, use the command below:
docker-compose build

Next Steps

        To learn about Kubernetes, Minikube and deployment of a microservice on Minikube, please read my blog post: Spring Boot MySQL Kubernetes Minikube Example.

Conclusion, Docker Hub Link and GitHub link: 

    This tutorial gives a comprehensive overview on running an existing Spring Boot MySQL microservice on Docker using Docker Compose. The code for the Spring Boot MySQL application used in this post is available on GitHub. The docker image used in this post is available on Docker Hub.
    Learn the most popular and trending technologies like Blockchain, Cryptocurrency, Machine Learning, Chatbots, Internet of Things (IoT), Big Data Processing, Elastic Stack, React, Highcharts, Progressive Web Application (PWA), Angular 5, GraphQL, Akka HTTP, Play Framework, Dropwizard, Docker, Netflix Eureka, Netflix Zuul, Spring Cloud, Spring Boot, Flask and RESTful Web Service integration with MongoDB, Kafka, Redis, Aerospike, MySQL DB in simple steps by reading my most popular blog posts at Software Developer Central.   
    If you like my post, please feel free to share it using the share button just below this paragraph or next to the heading of the post. You can also tweet with #SoftwareDeveloperCentral on Twitter. To get a notification on my latest posts or to keep the conversation going, you can follow me on Twitter or Instagram. Please leave a note below if you have any questions or comments.

Comments

Popular Posts

Elasticsearch, Logstash, Kibana Tutorial: Load MySQL Data into Elasticsearch

Dropwizard MySQL Integration Tutorial

Send Email in Java