Introduction Docker With Spring Boot and MySQL

Introduction Docker With Spring Boot and MySQL

Introduction Docker With Spring Boot and MySQL - This article explains the basic concepts of Docker and includes a tutorial that explains how to setup MySQL.

Originally published by Sanjoy Kumer Deb at

Docker is a technology where developers or DevOps teams can build, deploy, and manage applications by using containers. Docker is an open source software so that everyone can run this on their own operating system, which should support virtualization and Docker for Mac/Windows/Linux.

Docker also ships the ready images from one computer to another. Docker containers are sets of processes that are isolated from the rest of the processes in the host OS. Docker shares the kernel of the host operating system. On the other hand, VM is a technology that depends on the guest OS, which is completely separated from the host OS. Virtual machines communicate with the host OS through hypervisor. VM requires many hardware resources and processes. It's not a container-based technology.

Docker moves up the abstractions of resources from hardware level to the operating system level. That's why application portability and infrastructure separation is easier.

Container-based technology takes lower resources of the host system. Most of the time, container-based technology uses the kernel of the host machine. The main goal of this article is to implement Docker with a Spring Boot application and MySQL.

You might also enjoy: Spring Boot: Run and Build in Docker

Basic Concepts

Docker Engine works as a client-server architecture. Docker daemon works as a server, which is the core part of Docker and is run on the host operating system. This service exposes some rest APIs that can be used by the client. A command line interface (CLI) client uses the services provided by Docker daemon with Docker command.


Important Terms

Image: Image is the executable application package file inside Docker. It contains everything needed to run an application, including libraries, config files, runtime, etc. It's a snapshot of a container. 

Container: An instance of an image is called a container. When an image is executed and takes place in memory, then the instance of this image is called a container. It executes in a completely isolated environment.

In OOP terms, Image is a class, and a container is an instance of this class — a runtime object.

*Registry: *A registry is a storage and content delivery system that stores Docker images. Docker Hub is a popular Docker registry. We can store different versions of images with different tag numbers to Docker Hub.  

Dockerfile: It's a text file that contains all the commands to assemble an image. 

Dockerfile  > (Build) >  Image  > (Run) >  Container


In this article, we will use Docker Hub as a Docker registry. Then, we will follow the instructions to set up a desktop Docker application. I am using Mac OS as my host machine. After installation, we can see that the Docker app is running. Then, I have to sign in using my Docker Hub credentials.




After Docker Desktop Install and Login

Now, we can open the terminal to check some Docker commands.

 docker --version command should return the Docker version. In my case, this command returns 

Docker version 19.03.2, build 6a30dfc

 docker info command should return detailed information of the installed Docker machine of the host machine. Initially, you have zero images and containers.

 docker pull hello-world This command should pull a hello-world image from the Docker registry. Now again, if we run the Docker info command, we can see that the image count is 1. But the container count is still zero.

 docker run hello-world This command should create an instance of the hello-world image. It will return a long message: 

Hello from Docker!

This message shows that your installation appears to be working correctly.

If we run the Docker info command, we can see that the image count is 1 and the container count is also one. So the Docker installation is complete.

Setup MySQL

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 6033:3306 --name=docker-mysql --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=book_manager" mysql

Hopefully, the MySQL image is pulled and running as a container. To check this, we can run

 docker image ls and docker container ls commands. In my case, the responses of these commands are:

mysql latest b8fd9553f1f0 3 days ago 445MB 
hello-world latest fce289e99eb9 8 months ago 1.84kB
5db66654ba6a mysql "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 33060/tcp,>3306/tcp docker-mysql

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:

+--------------------+ | Database | +--------------------+ | book_manager | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)

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 6033. 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 <book_manager.sql 

Hopefully, the book_manager script executed successfully. You can confirm by executing the following command.

$ docker exec -it docker-mysql bash; 
[email protected]:/# mysql -uroot -p 
Enter password: 
mysql> show databases; 
+--------------------+ | Database | +--------------------+ | book_manager | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> use book_manager Database changed 
mysql> show tables; 
+------------------------+ | Tables_in_book_manager | +------------------------+ | author | | book | | book_author | | book_publisher | | book_tag | | bookshelf | | publisher | | tag | +------------------------+ 8 rows in set (0.01 sec) mysql> 

Application Clone and Build Project

I have already pushed my code to my GitHub repository. Anyone can clone the codebase from here. I think your host machine has Gradle setup. So now we run the gradle build commandto build the project. So the executable jar file is created at the build/jar directory of your cloned project.

Now open the Dockerfile. We can see that the file contains the following commands:

FROM java:8
VOLUME /tmp 
EXPOSE 10222 
ADD /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 entrypoint arguments. Now we will build a docker image by using this Dockerfile

docker build -f Dockerfile -t book_manager_app .

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 book_manager_app 

The --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.

After running this command, we will hit http://localhost:10222/book from our host machine browser, and it will return a list of books. 

That's all for introductory knowledge of Docker with a simple Spring Boot implementation and MySQL database.

Happy coding!

Further Reading

How To Set Up Laravel, Nginx, and MySQL with Docker Compose

Docker Basics: Docker Compose

Tutorial Laravel 6 with Docker and Docker-Compose

docker spring-boot mysql

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Build Full-stack App with Docker, Spring Boot, Mysql and React

In this article, you'll learn how to use Docker compose to run a full-stack application built with Spring Boot, Mysql, and React.

Best MySQL DigitalOcean Performance – ScaleGrid vs. DigitalOcean Managed Databases

Compare ScaleGrid MySQL vs. DigitalOcean Managed Databases - See which offers the best MySQL throughput, latency, and pricing on DigitalOcean across workloads.

Spring Boot + JWT + MySQL: Token based Authentication with Spring Security example

In this tutorial, we’re gonna build a Spring Boot Application that supports Token based Authentication with JWT. You’ll know:

Spring Boot with PostgreSQL and Docker Compose | RESTful CRUD API Example | Spring Data JPA Auditing

In this video, You will learn how to create a Spring Boot + JPA/Hibernate + PostgreSQL RESTful CRUD API Example with a PostgreSQL database to run inside a Docker container and how to auditing, tracking and logging user activity across the application project by using JPA Auditing

Creating Efficient Docker Images with Spring Boot 2.3

This is an update to the original blog post about creating docker images with Spring Boot 2.3. There were a few things related to image creation that changed between the first milestone of Spring Boot 2.3 and the GA release.