{"id":143,"date":"2019-12-12T10:40:18","date_gmt":"2019-12-12T10:40:18","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/microservices\/?p=143"},"modified":"2020-07-26T11:05:13","modified_gmt":"2020-07-26T11:05:13","slug":"how-to-dockerize-your-spring-boot-application-step-by-step","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/microservices\/how-to-dockerize-your-spring-boot-application-step-by-step\/","title":{"rendered":"How to Dockerize Your Spring Boot Application (Step by Step)"},"content":{"rendered":"<p>If you are learning microservices,then you should also know about docker and container.<\/p>\n<p>In this tutorial, I will teach you how to create a docker image from your Spring Boot application. Then we are going to run this image to create a container.<\/p>\n<p><strong>Note<\/strong>: A container is created when you run an image. That is why a container is sometimes called &#8216;a running image&#8217;.<\/p>\n<ul>\n<li>You can get the complete demo app from my <a href=\"https:\/\/github.com\/KindsonTheGenius\/ThymeleafApp\">github repository here<\/a>.<\/li>\n<li>Or build the demo app(thymeleaf-app) by following <a href=\"https:\/\/www.youtube.com\/playlist?list=PL9l1zUfnZkZnEqlZLA_GI8vySZCZ7EBk9\">this tutorial.<\/a><\/li>\n<li>Learn about <a href=\"https:\/\/www.kindsonthegenius.com\/dockers-containers-and-kubernetes-a-simple-explanation\/\" target=\"_blank\" rel=\"noopener noreferrer\">dockers and containers here<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>What we would do<\/strong><\/p>\n<ol>\n<li><a href=\"#t1\">Install Docker on Windows\/Linux<\/a><\/li>\n<li><a href=\"#t2\">Create a Dockerfile<\/a><\/li>\n<li><a href=\"#t3\">Build a Docker Image<\/a><\/li>\n<li><a href=\"#t4\">Run the image (Create a container)<\/a><\/li>\n<li><a href=\"#t5\">View, Stop and Delete Containers<\/a><\/li>\n<li><a href=\"#t6\">Some Important Docker Commands<\/a><\/li>\n<li><a href=\"#t7\">What Next? Kubernetes!<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t1\">1. Install and Start Docker on Windows\/Linux<\/strong><\/h5>\n<p>The interesting thing is that whatever, your operating system is, you can install docker!<\/p>\n<ul>\n<li>If your system is Windows 10 64-bit, Pro, Enterprise or Education then download and install <a href=\"https:\/\/hub.docker.com\/?overlay=onboarding\">Docker from Docker Hub<\/a>.<\/li>\n<li>If your system is Windows 10 Home or any other, then download and install <a href=\"https:\/\/hub.docker.com\/?overlay=onboarding\">Docker Toolbox here<\/a><\/li>\n<li>Check that visualization is enabled in your Operating System. Do this by opening Task Manager. Click on performance tab as you can see in the figure below<\/li>\n<\/ul>\n<figure id=\"attachment_145\" aria-describedby=\"caption-attachment-145\" style=\"width: 602px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/12\/Visualization-Enabled-on-Windows-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-145 \" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/12\/Visualization-Enabled-on-Windows-1.jpg\" alt=\"Visualization Enabled on Windows\" width=\"602\" height=\"354\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/12\/Visualization-Enabled-on-Windows-1.jpg 845w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/12\/Visualization-Enabled-on-Windows-1-300x176.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/12\/Visualization-Enabled-on-Windows-1-768x452.jpg 768w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/a><figcaption id=\"caption-attachment-145\" class=\"wp-caption-text\">Visualization Enabled on Windows 10 64-bit<\/figcaption><\/figure>\n<p>In this tutorial, we use Docker Toolbox.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t2\">2. Create a Dockerfile<\/strong><\/h5>\n<p>Now you need to open the Spring Boot application either in Spring Toolsuite or IntelliJ (STS is used here).<\/p>\n<p>Run the application and test it to make sure it works well!<\/p>\n<p><strong>Step 1:<\/strong> Create a file in your spring boot project directory(in sts). Name it Dokerfile<\/p>\n<p><strong>Step 2:<\/strong> Copy the code below and paste in this file and save it.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">FROM openjdk:8-jdk-alpine\r\nVOLUME \/tmp\r\nARG JAR_FILE=target\/*.jar\r\nCOPY ${JAR_FILE} app.jar\r\nENTRYPOINT [\"java\",\"-Djava.security.egd=file:\/dev\/.\/urandom\",\"-jar\",\"\/app.jar\"]\r\n<\/pre>\n<p>You can also get this code from <a href=\"https:\/\/spring.io\/guides\/gs\/spring-boot-docker\/\">here(Spring Boot with Docker)<\/a><\/p>\n<p><strong>Step 3<\/strong>: Start Docker by simply opening the Docker QuickStart Terminal. You&#8217;ll see it as a shortcut on your desktop.<\/p>\n<p>It opens and provides a command line environment as shown below:<\/p>\n<figure id=\"attachment_147\" aria-describedby=\"caption-attachment-147\" style=\"width: 604px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/12\/Docker-Toolbox-QuickStart-Terminal.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-147 \" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/12\/Docker-Toolbox-QuickStart-Terminal.jpg\" alt=\"Docker Toolbox QuickStart Terminal\" width=\"604\" height=\"349\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/12\/Docker-Toolbox-QuickStart-Terminal.jpg 936w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/12\/Docker-Toolbox-QuickStart-Terminal-300x173.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/12\/Docker-Toolbox-QuickStart-Terminal-768x444.jpg 768w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/a><figcaption id=\"caption-attachment-147\" class=\"wp-caption-text\">Docker Toolbox QuickStart Terminal<\/figcaption><\/figure>\n<p>Notice the ip address of the default docker machine. We would use it later when we run our image. This is because the container has to run in this machine.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t3\">3. Build a Docker Image<\/strong><\/h5>\n<p>The next step is to build a Docker image of your application. Follow the steps<\/p>\n<p><strong>Step 1<\/strong>: Right-click on your project name and choose &#8216;Show in Local Terminal&#8217; &gt; Git Bash<\/p>\n<p>If you don&#8217;t see Git Bash, just install it easily and restart Spring Tool Suite.<\/p>\n<p><strong>Step 2:<\/strong> Run the command below to build the docker image<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker build -t thymeleaf-image .\r\n<\/pre>\n<p>This command creates an docker image of your application with name thymeleaf-image. The dot at the end represents the directory where the image is to be create. In this case is the current directory.<\/p>\n<p>Now to you can run the command below to see the images available<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker image ls\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t4\">4. Run the image (Create a container)<\/strong><\/h5>\n<p>Now that you have the image built, you can run the image using the command below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker run -p8080:8080 thymeleaf-image\r\n<\/pre>\n<p>Note that you need to provide exactly the same name as the image you created. Also note the port mapping of -p8080:8080, this means the we are mapping port 8080 of the container to port 8080 in the machine.<\/p>\n<p>Now visit http:\/\/192.168.9.101:8080<\/p>\n<p>You can see that the application runs perfectly inside a container!.<\/p>\n<p>Thumbs up if you got to this point.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t5\">5. View, Stop and Delete Containers<\/strong><\/h5>\n<p>If for any reason, you want to delete a container (for any reason), you can do that following two steps:<\/p>\n<ul>\n<li>stop the container<\/li>\n<li>remove the container<\/li>\n<\/ul>\n<p>Use the command below to stop a container<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker stop &lt;container-id&gt;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The used the command delete a container<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker rm &lt;container-id&gt;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Note<\/strong>: To get the container id for your container, run the command below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker rm &lt;container-id&gt;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t6\">6. Some Important Docker Commands<\/strong><\/h5>\n<p>By now you have learn the commands the build an image, to run an image as well as to list containers. I&#8217;ll teach you\u00a0 a few more command<\/p>\n<p>To remove an image,<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker rmi &lt;container-name&gt;\r\n<\/pre>\n<p>To view all images,<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker image ls\r\n<\/pre>\n<p>Starting a stopped container<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">docker container start &lt;container-name&gt;\r\n<\/pre>\n<p>At this point, I think I should make another lesson on docker commands. So we can keep this tutorial simple.<\/p>\n<p>&nbsp;<\/p>\n<h5 id=\"t7\"><strong>7. What Next? Kubernetes!<\/strong><\/h5>\n<p>I&#8217;m sure you&#8217;ve heard of Kubernetes.<\/p>\n<p>Now what we did in this tutorial is this: we created docker image and hosted it locally. Similarly, we could also push our image to a remote location. For example, <a href=\"https:\/\/hub.docker.com\/\">Docker Hub<\/a>.<\/p>\n<p>We can also host our images in Google Cloud. <a href=\"https:\/\/en.wikipedia.org\/wiki\/Kubernetes\">Kubernetes<\/a> is a feature provided by Google for orchestrating containers. So we can have a repository in <a href=\"https:\/\/cloud.google.com\/gcp\/?utm_source=google&amp;utm_medium=cpc&amp;utm_campaign=emea-emea-all-en-dr-bkws-all-all-trial-e-gcp-1007176&amp;utm_content=text-ad-none-any-DEV_c-CRE_169885627348-ADGP_Hybrid+%7C+AW+SEM+%7C+BKWS+~+EXA_1:1_EMEA_EN_General_Cloud_TOP_googlecloud-KWID_43700037579444434-kwd-6458750523-userloc_9063089&amp;utm_term=KW_google%20cloud-ST_google+cloud&amp;ds_rl=1242853&amp;ds_rl=1245734&amp;ds_rl=1245734&amp;gclid=CjwKCAiA58fvBRAzEiwAQW-hzRq3Kl8Ptm49t5UUdLXW9PIE9vg_a-5eBBTTN21iA4PoeymITP7dxhoCDgcQAvD_BwE\">Google Cloud<\/a> and the push our images there. Then we can run the containers from there. Read a little about <a href=\"https:\/\/www.kindsonthegenius.com\/dockers-containers-and-kubernetes-a-simple-explanation\/\" target=\"_blank\" rel=\"noopener noreferrer\">Kubernetes here.<\/a><\/p>\n<p>This is really fun to do! That is what we&#8217;ll cover in the next series!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are learning microservices,then you should also know about docker and container. In this tutorial, I will teach you how to create a docker &hellip; <\/p>\n","protected":false},"author":1,"featured_media":149,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[39,50,22,25],"class_list":["post-143","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-docker-and-containers","tag-container","tag-docker","tag-kubernetes","tag-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/comments?post=143"}],"version-history":[{"count":7,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":239,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/143\/revisions\/239"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/media\/149"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}