{"id":1995,"date":"2022-01-24T12:00:00","date_gmt":"2022-01-24T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/dockerize-springbootfleetms-v2-with-mysql-database\/"},"modified":"2026-07-05T03:26:21","modified_gmt":"2026-07-05T01:26:21","slug":"dockerize-springbootfleetms-v2-with-mysql-database","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/dockerize-springbootfleetms-v2-with-mysql-database\/","title":{"rendered":"Dockerize SpringBoot(FleetMS v2) with MySQL Database"},"content":{"rendered":"<p>In this tutorial, I will take you step by step how to dockerize SpringBoot with MySQL database. And it&#8217;s quite a clear process. It takes three main steps which we would take in this tutorial. Then I&#8217;ll briefly talk about docker networks.<\/p>\n<ol>\n<li><a href=\"#t1\">Get a docker image of MySQL<\/a><\/li>\n<li><a href=\"#t2\">Create a docker image of the SprintBoot application<\/a><\/li>\n<li><a href=\"#t3\">Link the two images<\/a><\/li>\n<li><a href=\"#t4\">Working With Docker Networks<\/a><\/li>\n<\/ol>\n<p>So let&#8217;s get started!<\/p>\n<p>This lessons corresponds to Part 53 of the video series on FleetMS version 2.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Setup MySQL Container<\/strong><\/h4>\n<p>Now we first obtain a MySQL image. We don&#8217;t have to build this as it is already available in docker hub. We&#8217;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:<\/p>\n<ul>\n<li>username<\/li>\n<li>password<\/li>\n<li>database name<\/li>\n<\/ul>\n<p>Let&#8217;s follow the steps below<\/p>\n<p><strong>Step 1<\/strong> &#8211; Pull the MySQL image using the command below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker pull mysql\r\n<\/pre>\n<p>Then you can use the <em><strong>docker image ls<\/strong> <\/em>command to check that the mysql image was pulled.<\/p>\n<p><strong>Step 2<\/strong> &#8211; We now run the mysql image using the following command:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker run -d -p 3308:3306 --name=mysql-docker --env=\"MYSQL_ROOT_PASSWORD=root\" --env=\"MYSQL_PASSWORD=root\" --env=\"MYSQL_DATABASE=fleetdb_docker\" mysql\r\n<\/pre>\n<p>The port parameter indicates that the mysql runs on port 3308 outside the container and 3306 inside the container. So the port mapping is <em><strong>outside:inside<\/strong><\/em><\/p>\n<p><strong>Step 3<\/strong> &#8211; Connect to the container and access the database inside the container. Use the command:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker exec -it mysql-docker bash\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4<\/strong> &#8211; Login into the MySQL server using the command:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">mysql -uroot -proot\r\n<\/pre>\n<p>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.<\/p>\n<p><strong>Optional<\/strong> &#8211; Try to connect to MySQL container using MySQL Workbench via port 3308<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Setup the Spring Boot Container<\/strong><\/h4>\n<p>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!). <a href=\"https:\/\/www.kindsonthegenius.com\/introduction-to-dockerfile-with-spring-boot-how-to-dockerize-springboot-app\/\" target=\"_blank\" rel=\"noopener\">Learn both ways here<\/a>. In this tutorial, we would use Dockerfile.<\/p>\n<p><strong>Step 1<\/strong> &#8211; Create a file named Dockerfile in the root directory of the Spring Boot application. It should have the following content:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">FROM adoptopenjdk\/openjdk11:alpine-jre\r\nADD target\/fleetapp_v2-0.0.1-SNAPSHOT.jar app.jar\r\nENTRYPOINT [\"java\",\"-jar\",\"\/app.jar\"]\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Update:<\/strong> For Spring Boot 3.0, you have to use this:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">FROM openjdk:17-jdk-alpine\r\nADD target\/product-app-0.0.1-SNAPSHOT.jar app.jar\r\nENTRYPOINT [\"java\",\"-jar\",\"\/app.jar\"]\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2<\/strong>\u00a0&#8211; Adjust the<em><strong> spring.datasource.url<\/strong> <\/em>parameter in the application.properties file to\u00a0 connect to the MySQL database in the container. So the new value would be:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">spring.datasource.url=jdbc:mysql:\/\/mysql-docker:3308\/fleetdb_docker?serverTimezone=UTC\r\n<\/pre>\n<p>Note we changed the port number and the database name. Also remember to set the right username and password.<\/p>\n<p><strong>Step 3<\/strong>\u00a0&#8211; From the root directory of the project, run the command<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker build -t fleet-image .\r\n<\/pre>\n<p>Note the period at the end!<\/p>\n<p>You can now use docker images command to check the the fleet-image is created.<\/p>\n<p><strong>Step 4<\/strong> &#8211; Start the application again and ensure it works.<\/p>\n<p><strong>What we&#8217;ve\u00a0<\/strong><b>achieved<\/b><\/p>\n<p>We have succeeded in creating an image of\u00a0 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<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Link the two Container<\/strong><\/h4>\n<p>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 <strong>Docker Network<\/strong>.<\/p>\n<p><strong>Step 1<\/strong> &#8211; 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:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">spring.datasource.url=jdbc:mysql:\/\/mysql-docker:3306\/fleetdb_docker?serverTimezone=UTC\r\n<\/pre>\n<p>Remember you need to rebuild the image after this change.<\/p>\n<p>We would now spin up the container for our application such that it starts up along with the MySQL containers as well.<\/p>\n<p><strong>Step 2<\/strong> &#8211; Start the application using the following command<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker run -t --link mysql-docker:mysql -p 8080:8080 fleet-image\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>This command spins up both containers and they&#8217;ll just work fine.\u00a0 If you got here thus far, thumbs up to you. We could cover the following topics in subsequent tutorials:<\/p>\n<ul>\n<li>spin up two containers using docker-compose (<a href=\"https:\/\/www.kindsonthegenius.com\/introduction-to-docker-compose-with-spring-boot-example\/\" target=\"_blank\" rel=\"noopener\">Learn a bit about docker-compose here<\/a> | <a href=\"https:\/\/youtu.be\/aCIl7-cyrWE\" target=\"_blank\" rel=\"noopener\">video tutorial here<\/a>)<\/li>\n<li>setup and deploy to local Kubernetes cluster.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><strong>4. Basics of Docker Networks<\/strong><\/h4>\n<p>Similar to how we used the &#8211;link option in the docker run command, we can also use docker networks to achieve same objective. We simply have to create\u00a0 a new network and then assign the two containers to the same network and that&#8217;s it!<\/p>\n<p>Let follow the steps:<\/p>\n<p><strong>Step 1<\/strong> &#8211; Create a docker network using the command below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker network create fleet-net\r\n<\/pre>\n<p>The network is created and you can use <strong><em>docker network ls<\/em><\/strong> to see the networks.<\/p>\n<p><strong>Step 2<\/strong> &#8211; Connect the mysql-docker container to the fleet-net docker network using the command:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker network connect fleet-net mysql-docker\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong> &#8211; Connect the fleet-image container to the command below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker network connect fleet-net fleet-image\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>You can go ahead to test the network just like before.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, I will take you step by step how to dockerize SpringBoot with MySQL database. And it&#8217;s quite a clear process. It takes &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[311],"tags":[],"class_list":["post-1995","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1995"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1995\/revisions"}],"predecessor-version":[{"id":2163,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1995\/revisions\/2163"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}