{"id":3780,"date":"2025-04-17T13:00:59","date_gmt":"2025-04-17T11:00:59","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=3780"},"modified":"2025-04-17T13:07:58","modified_gmt":"2025-04-17T11:07:58","slug":"how-to-package-a-spring-boot-app-in-docker-and-run-locally","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2025\/04\/17\/how-to-package-a-spring-boot-app-in-docker-and-run-locally\/","title":{"rendered":"How to Package a Spring Boot App in Docker and Run Locally"},"content":{"rendered":"\n<p>In this detailed guide, we&#8217;ll explore how to package a Spring Boot application into a Docker container and run it locally. Each step will be explained thoroughly to ensure clarity and understanding.<\/p>\n\n\n\n<h4>Prerequisites<\/h4>\n\n\n\n<ul><li><strong>Java Development Kit (JDK)<\/strong> installed<\/li><li><strong>Docker<\/strong> installed<\/li><li><strong>Maven<\/strong> installed<\/li><li>Basic understanding of Spring Boot<\/li><\/ul>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4>Step-by-Step Guide<\/h4>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">Step 1: Create a Spring Boot Application<\/span><\/h5>\n\n\n\n<p>First, let&#8217;s create a simple Spring Boot application. You can use Spring Initializr to generate a basic project structure.<\/p>\n\n\n\n<h6>Example <code>pom.xml<\/code><\/h6>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\"\n         xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n         xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\n    &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n    &lt;groupId&gt;com.example&lt;\/groupId&gt;\n    &lt;artifactId&gt;demo&lt;\/artifactId&gt;\n    &lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\n    &lt;name&gt;demo&lt;\/name&gt;\n    &lt;description&gt;Demo project for Spring Boot&lt;\/description&gt;\n    &lt;parent&gt;\n        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n        &lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;\n        &lt;version&gt;3.4.2&lt;\/version&gt;\n    &lt;\/parent&gt;\n    &lt;dependencies&gt;\n        &lt;dependency&gt;\n            &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n            &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\n        &lt;\/dependency&gt;\n    &lt;\/dependencies&gt;\n&lt;\/project&gt;<\/code><\/pre>\n\n\n\n<ul><li><strong><code>artifactId<\/code>:<\/strong> This is the name of our project. It will be used to name the JAR file.<\/li><li><strong><code>version<\/code>:<\/strong> This indicates the version of our application. <code>0.0.1-SNAPSHOT<\/code> is a common version format indicating a development version.<\/li><\/ul>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Step 2: Create a Dockerfile<\/strong><\/span><\/h5>\n\n\n\n<p>Create a <code>Dockerfile<\/code> in the root directory of your Spring Boot project. This file defines how our Docker image will be built.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Dockerfile\nFROM openjdk:17-jdk-slim\nVOLUME \/tmp\nCOPY target\/demo-0.0.1-SNAPSHOT.jar app.jar\nENTRYPOINT &#91;\"java\",\"-jar\",\"\/app.jar\"]<\/code><\/pre>\n\n\n\n<ul><li><strong><code>FROM openjdk:17-jdk-slim<\/code>:<\/strong> This specifies the base image. We are using a slim version of OpenJDK 17 to keep the image lightweight.<\/li><li><strong><code>VOLUME \/tmp<\/code>:<\/strong> This creates a temporary volume to store files during the application runtime.<\/li><li><strong><code>COPY target\/demo-0.0.1-SNAPSHOT.jar app.jar<\/code>:<\/strong> This copies the JAR file generated by Maven into the Docker image. The name <code>demo-0.0.1-SNAPSHOT.jar<\/code> comes from the <code>artifactId<\/code> and <code>version<\/code> specified in the <code>pom.xml<\/code>.<\/li><li><strong><code>ENTRYPOINT [\"java\",\"-jar\",\"\/app.jar\"]<\/code>:<\/strong> This defines the command to run the application when the container starts.<\/li><\/ul>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Step 3: Build the Spring Boot Application<\/strong><\/span><\/h5>\n\n\n\n<p>Use Maven to compile and package your Spring Boot application. This will generate a JAR file in the <code>target<\/code> directory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mvn clean package<\/code><\/pre>\n\n\n\n<ul><li><strong><code>mvn clean package<\/code>:<\/strong> This command does two things:<\/li><li><strong><code>clean<\/code>:<\/strong> Removes the <code>target<\/code> directory to ensure a fresh build.<\/li><li><strong><code>package<\/code>:<\/strong> Compiles the code and packages it into a JAR file.<\/li><\/ul>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Step 4: Build the Docker Image<\/strong><\/span><\/h5>\n\n\n\n<p>Now, build the Docker image using the Dockerfile you created.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker build -t spring-boot-docker-demo .<\/code><\/pre>\n\n\n\n<ul><li><strong><code>docker build<\/code>:<\/strong> This command builds the Docker image.<\/li><li><strong><code>-t spring-boot-docker-demo<\/code>:<\/strong> Tags the image with the name <code>spring-boot-docker-demo<\/code>.<\/li><li><strong><code>.<\/code>:<\/strong> Specifies the current directory as the build context.<\/li><\/ul>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Step 5: Run the Docker Container<\/strong><\/span><\/h5>\n\n\n\n<p>Run the Docker container using the image you just built.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -p 8080:8080 spring-boot-docker-demo<\/code><\/pre>\n\n\n\n<ul><li><strong><code>docker run<\/code>:<\/strong> Starts a new container from the specified image.<\/li><li><strong><code>-p 8080:8080<\/code>:<\/strong> Maps port 8080 of the Docker container to port 8080 on your local machine, allowing you to access the application via <code>http:\/\/localhost:8080<\/code>.<\/li><\/ul>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Step 6: Test the Application<\/strong><\/span><\/h5>\n\n\n\n<p>Open a web browser and navigate to <code>http:\/\/localhost:8080<\/code>. You should see your Spring Boot application running.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5><strong>Conclusion<\/strong><\/h5>\n\n\n\n<p>You&#8217;ve successfully packaged a Spring Boot application in a Docker container and run it locally. Docker simplifies the deployment process by ensuring that your application runs consistently across different environments.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this detailed guide, we&#8217;ll explore how to package a Spring Boot application into a Docker container and run it locally. Each step will be explained thoroughly to ensure clarity and understanding. Prerequisites Java Development Kit (JDK) installed Docker installed Maven installed Basic understanding of Spring Boot Step-by-Step Guide Step 1: Create a Spring Boot [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1019,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[533,868],"tags":[1009,259,6,1008],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3780"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3780"}],"version-history":[{"count":3,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3780\/revisions"}],"predecessor-version":[{"id":3783,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3780\/revisions\/3783"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1019"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}