{"id":1973,"date":"2021-05-14T12:00:00","date_gmt":"2021-05-14T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/introduction-to-dockerfile-with-spring-boot-how-to-dockerize-springboot-app\/"},"modified":"2026-07-05T03:25:30","modified_gmt":"2026-07-05T01:25:30","slug":"introduction-to-dockerfile-with-spring-boot-how-to-dockerize-springboot-app","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/introduction-to-dockerfile-with-spring-boot-how-to-dockerize-springboot-app\/","title":{"rendered":"Introduction to Dockerfile with Spring Boot \u2013 How to Dockerize SpringBoot App"},"content":{"rendered":"<p>In this tutorial, you will learn step by step how to Dockerize your Spring Boot application. We would create a Docker container of a Spring Boot application and run it on Docker.<\/p>\n<p>In this tutorial, we&#8217;ll work without a database. However, in the next part, we&#8217;ll see how to incorporate a MySQL database.<\/p>\n<p><strong>Why do you need to Dockerize?<\/strong><\/p>\n<p>This is simply to ensure that your application can easily be distributed and run anywhere without configuration. So when you dockerize, then you pack your application into an image(kind of OS) can be just start and run.<\/p>\n<p>Since this is a practical tutorial, let&#8217;s just get started.<\/p>\n<ol>\n<li><a href=\"#t1\">Build the Spring Boot Application<\/a><\/li>\n<li><a href=\"#t2\">Dockerize and Run it<\/a><\/li>\n<li><a href=\"#t3\">Using Buidpacks without Dockerfile<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h5 id=\"t1\"><strong>1. Build a SpringBoot Application<\/strong><\/h5>\n<p>For this application, I have used IntelliJ with Spring Initializer. Only two dependencies are added:<\/p>\n<ul>\n<li>Spring Web<\/li>\n<li>Lombok<\/li>\n<\/ul>\n<p>Next, I have created two classes:<\/p>\n<ul>\n<li><strong>Person<\/strong>: Just a simple model for person object<\/li>\n<li><strong>HomeController<\/strong>:\u00a0 The restcontroller to serve the application<\/li>\n<\/ul>\n<p>These two files are shown below.<\/p>\n<p>The Person class<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Data<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@NoArgsConstructor<\/span>\r\n<span style=\"color: #555555; font-weight: bold;\">@AllArgsConstructor<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Person<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> String firstname<span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> String lastname<span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> String location<span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The Controller<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@RestController<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">HomeController<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n    <span style=\"color: #555555; font-weight: bold;\">@GetMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> List<span style=\"color: #333333;\">&lt;<\/span>Person<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">personList<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\r\n        ArrayList<span style=\"color: #333333;\">&lt;<\/span>Person<span style=\"color: #333333;\">&gt;<\/span> personList <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> ArrayList<span style=\"color: #333333;\">&lt;<\/span>Person<span style=\"color: #333333;\">&gt;(<\/span>\r\n                Arrays<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">asList<\/span><span style=\"color: #333333;\">(<\/span>\r\n                <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Person<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Munonye\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Budapest\"<\/span><span style=\"color: #333333;\">),<\/span>\r\n                <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Person<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Adaku\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Okeke\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Nigeria\"<\/span><span style=\"color: #333333;\">),<\/span>\r\n                <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Person<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Helen\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Grudals\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Hungary\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n        <span style=\"color: #333333;\">));<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span> personList<span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Now if you run this application it works. You can go to http:\/\/localhost:8080\/ and you&#8217;ll see the list of users.<\/p>\n<p>&nbsp;<\/p>\n<h5 id=\"t2\"><strong>2. Let&#8217;s Dockerize and Run!<\/strong><\/h5>\n<p>The first step is to create a Java Archive (jar file) of your application. To do that, run the command in you terminal:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">mvn clean install\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>This would generate your .jar file. So if you look into the target folder you&#8217;ll see this file as shown below:<\/p>\n<figure id=\"attachment_14612\" aria-describedby=\"caption-attachment-14612\" style=\"width: 258px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/05\/Generate-jar-file-from-Spring-Boot.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-14612\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/05\/Generate-jar-file-from-Spring-Boot-258x300.png\" alt=\"Generate jar file from Spring Boot\" width=\"258\" height=\"300\" srcset=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/05\/Generate-jar-file-from-Spring-Boot-258x300.png 258w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/05\/Generate-jar-file-from-Spring-Boot-600x698.png 600w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/05\/Generate-jar-file-from-Spring-Boot.png 712w\" sizes=\"auto, (max-width: 258px) 100vw, 258px\" \/><\/a><figcaption id=\"caption-attachment-14612\" class=\"wp-caption-text\">Generate jar file from Spring Boot<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Next, create a new file and name it Dockerfile. Note the casing ( i<strong>t&#8217;s not DockerFile or dockerFile!<\/strong>)<\/p>\n<p>Enter the following in this file:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">FROM adoptopenjdk\/openjdk11:alpine-jre\r\nEXPOSE 8080\r\nARG JAR_FILE=target\/dockertutorial-0.0.1-SNAPSHOT.jar\r\nADD ${JAR_FILE} app.jar\r\nENTRYPOINT [\"java\",\"-jar\",\"\/app.jar\"]\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Next, build your the docker image of your application using the following command:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker build -t demoapp .\r\n<\/pre>\n<p>Before you run this command, ensure that Docker is started on your computer<\/p>\n<p>This command generates an image of your app with the tag demoapp. Also take note of the period(.) at the end of the line. This tells docker to use a Dockerfile in the same directory.<\/p>\n<p>You can now check that the image is created using the command:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker image ls\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Finally, to run the image, use the command:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker run -p8080:8080 demoapp\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The application starts up at port 8080 and you can access it as before.<\/p>\n<p>&nbsp;<\/p>\n<h5 id=\"t3\"><strong>3. Using BuildPacks Without Dockerfile<\/strong><\/h5>\n<p>Buildpack is a new feature introduces in Spring Boot 2.3.0.M1 to improve containerization. With this, you don&#8217;t need a docker file. Buildpacks takes your application and converts it to something that can run in docker.<\/p>\n<p>So to generate docker image using Buildpacks, you don&#8217;t need to create a Dockerfile. Just run the command below and your image is generated:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">mvn spring-boot:build-image<\/pre>\n<p>Again, you can run the container with:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker run -p8080:8080 image-name:tag-name<\/pre>\n<p>You can find the image-name and tag-name using docker image ls as before.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn step by step how to Dockerize your Spring Boot application. We would create a Docker container of a Spring &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":[414],"tags":[],"class_list":["post-1973","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1973","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=1973"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1973\/revisions"}],"predecessor-version":[{"id":2141,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1973\/revisions\/2141"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}