In this tutorial, I will take you step by step how to dockerize SpringBoot with MySQL database. And it’s quite a clear process. It takes three main steps which we would take in this tutorial. Then I’ll briefly talk about docker networks.
- Get a docker image of MySQL
- Create a docker image of the SprintBoot application
- Link the two images
- Working With Docker Networks
So let’s get started!
This lessons corresponds to Part 53 of the video series on FleetMS version 2.
1. Setup MySQL Container
Now we first obtain a MySQL image. We don’t have to build this as it is already available in docker hub. We’ll simply pull it from there. Then we need to run the image. To run the image, we specify the image name as well as the following environment variables:
- username
- password
- database name
Let’s follow the steps below
Step 1 – Pull the MySQL image using the command below
docker pull mysql
Then you can use the docker image ls command to check that the mysql image was pulled.
Step 2 – We now run the mysql image using the following command:
docker run -d -p 3308:3306 --name=mysql-docker --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=fleetdb_docker" mysql
The port parameter indicates that the mysql runs on port 3308 outside the container and 3306 inside the container. So the port mapping is outside:inside
Step 3 – Connect to the container and access the database inside the container. Use the command:
docker exec -it mysql-docker bash
Step 4 – Login into the MySQL server using the command:
mysql -uroot -proot
After getting in, you can use the show databases command to display the databases in the server. You will see that the fleetdb-docker database is available as well.
Optional – Try to connect to MySQL container using MySQL Workbench via port 3308
2. Setup the Spring Boot Container
Now we would have to also create a docker image of our Spring Boot application. There are two ways to do it: with Dockerfile and without Dockerfile(using Buildpacks, the easy way!). Learn both ways here. In this tutorial, we would use Dockerfile.
Step 1 – Create a file named Dockerfile in the root directory of the Spring Boot application. It should have the following content:
FROM adoptopenjdk/openjdk11:alpine-jre ADD target/fleetapp_v2-0.0.1-SNAPSHOT.jar app.jar ENTRYPOINT ["java","-jar","/app.jar"]
Update: For Spring Boot 3.0, you have to use this:
FROM openjdk:17-jdk-alpine ADD target/product-app-0.0.1-SNAPSHOT.jar app.jar ENTRYPOINT ["java","-jar","/app.jar"]
Step 2 – Adjust the spring.datasource.url parameter in the application.properties file to connect to the MySQL database in the container. So the new value would be:
spring.datasource.url=jdbc:mysql://mysql-docker:3308/fleetdb_docker?serverTimezone=UTC
Note we changed the port number and the database name. Also remember to set the right username and password.
Step 3 – From the root directory of the project, run the command
docker build -t fleet-image .
Note the period at the end!
You can now use docker images command to check the the fleet-image is created.
Step 4 – Start the application again and ensure it works.
What we’ve achieved
We have succeeded in creating an image of our application and we also pulled a MySQL image. Then we made our application run locally and connect to MySQL inside a container. Read on for next steps
3. Link the two Container
Now we want our application run inside a container as well as the MySQL. So somehow we need to network the two containers. This is called Docker Network.
Step 1 – Adjust the spring.datasource.url parameters to point to the docker-mysql image, then change the port to the internal port. So it would look like this:
spring.datasource.url=jdbc:mysql://mysql-docker:3306/fleetdb_docker?serverTimezone=UTC
Remember you need to rebuild the image after this change.
We would now spin up the container for our application such that it starts up along with the MySQL containers as well.
Step 2 – Start the application using the following command
docker run -t --link mysql-docker:mysql -p 8080:8080 fleet-image
This command spins up both containers and they’ll just work fine. If you got here thus far, thumbs up to you. We could cover the following topics in subsequent tutorials:
- spin up two containers using docker-compose (Learn a bit about docker-compose here | video tutorial here)
- setup and deploy to local Kubernetes cluster.
4. Basics of Docker Networks
Similar to how we used the –link option in the docker run command, we can also use docker networks to achieve same objective. We simply have to create a new network and then assign the two containers to the same network and that’s it!
Let follow the steps:
Step 1 – Create a docker network using the command below:
docker network create fleet-net
The network is created and you can use docker network ls to see the networks.
Step 2 – Connect the mysql-docker container to the fleet-net docker network using the command:
docker network connect fleet-net mysql-docker
Step 3 – Connect the fleet-image container to the command below:
docker network connect fleet-net fleet-image
You can go ahead to test the network just like before.

