{"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-08-31T09:59:17","modified_gmt":"2026-08-31T07:59:17","slug":"dockerize-springboot-with-mysql-database","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/dockerize-springboot-with-mysql-database\/","title":{"rendered":"Dockerize SpringBoot with MySQL Database"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><em>Learn how to Dockerize SpringBoot with MySQL by creating Docker images, running containers, connecting them with Docker networks, and testing the application.<\/em><\/p>\n\n\n<h3>TL;DR<\/h3>\n<ul>\n<li>\n<p>Pull and run a MySQL Docker image with the required database credentials.<\/p>\n<\/li>\n<li>\n<p>Create a Dockerfile for the SpringBoot application and build its Docker image.<\/p>\n<\/li>\n<li>\n<p>Configure SpringBoot to connect to MySQL running inside a Docker container.<\/p>\n<\/li>\n<li>\n<p>Link the SpringBoot and MySQL containers so they can communicate.<\/p>\n<\/li>\n<li>\n<p>Use Docker networks as an alternative way to connect the application and database containers.<\/p>\n<\/li>\n<\/ul>\n<p>In this tutorial, I will take you step by step how to dockerize SpringBoot with MySQL database. And it\u2019s quite a clear process. It takes three main steps which we would take in this tutorial. Then I\u2019ll 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\u2019s 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\u2019t have to build this as it is already available in docker hub. We\u2019ll 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\u2019s follow the steps below<\/p>\n<p><strong>Step 1<\/strong> \u2013 Pull the MySQL image using the command below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker pull mysql\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> \u2013 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\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> \u2013 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\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4<\/strong> \u2013 Login into the MySQL server using the command:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">mysql -uroot -proot\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> \u2013 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:\/\/kindsonthegenius.com\/blog\/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> \u2013 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\nADD target\/fleetapp_v2-0.0.1-SNAPSHOT.jar app.jar\nENTRYPOINT [\"java\",\"-jar\",\"\/app.jar\"]\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\nADD target\/product-app-0.0.1-SNAPSHOT.jar app.jar\nENTRYPOINT [\"java\",\"-jar\",\"\/app.jar\"]\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2<\/strong>\u00a0\u2013 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\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\u2013 From the root directory of the project, run the command<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker build -t fleet-image .\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> \u2013 Start the application again and ensure it works.<\/p>\n<p><strong>What we\u2019ve\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> \u2013 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\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> \u2013 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\n<\/pre>\n<p>&nbsp;<\/p>\n<p>This command spins up both containers and they\u2019ll 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:\/\/kindsonthegenius.com\/blog\/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 \u2013link 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\u2019s it!<\/p>\n<p>Let follow the steps:<\/p>\n<p><strong>Step 1<\/strong> \u2013 Create a docker network using the command below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker network create fleet-net\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> \u2013 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\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong> \u2013 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\n<\/pre>\n<p>&nbsp;<\/p>\n<p>You can go ahead to test the network just like before.<\/p>\n<h3>FAQs<\/h3>\n<p><strong>What does it mean to Dockerize a SpringBoot application?<\/strong><\/p>\n<p>Dockerizing a SpringBoot application means packaging the application into a Docker image so it can run inside a Docker container.<\/p>\n<p><strong>How do I run MySQL in a Docker container?<\/strong><\/p>\n<p>You can pull the MySQL image from Docker Hub using <code>docker pull mysql<\/code> and then run it with <code>docker run<\/code>, providing the required username, password, database, and port configuration.<\/p>\n<p><strong>How does SpringBoot connect to MySQL in Docker?<\/strong><\/p>\n<p>SpringBoot can connect to MySQL through the Docker container&#8217;s network address and internal MySQL port. The database URL needs to reference the MySQL container rather than the local host.<\/p>\n<p><strong>How can I connect two Docker containers?<\/strong><\/p>\n<p>The containers can be connected using Docker&#8217;s networking features. The tutorial demonstrates using the <code>--link<\/code> option and also shows how to create and connect containers to a custom Docker network.<\/p>\n<p><strong>Can I use Docker networks instead of the <code>--link<\/code> option?<\/strong><\/p>\n<p>Yes. The tutorial demonstrates creating a custom Docker network with <code>docker network create<\/code> and connecting the MySQL and SpringBoot containers to that network.<\/p>\n<h3>Final Thoughts<\/h3>\n<p>Dockerizing SpringBoot with MySQL provides a practical introduction to running an application and its database in separate containers. In this tutorial, you created a MySQL container, built a Docker image for the SpringBoot application, and connected the two containers.<\/p>\n<p>You also explored Docker networking as an alternative to linking containers directly. These concepts provide a foundation for the next steps, including using Docker Compose and deploying the application to a local Kubernetes cluster.<\/p>","protected":false},"excerpt":{"rendered":"<p>Learn how to Dockerize SpringBoot with MySQL by creating Docker images, running containers, connecting them with Docker networks, and testing the application. TL;DR Pull and &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[311],"tags":[],"class_list":["post-1995","post","type-post","status-publish","format-standard","hentry","category-sql"],"acf":[],"_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":3,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1995\/revisions"}],"predecessor-version":[{"id":2590,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1995\/revisions\/2590"}],"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}]}}