|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Dockerizing Spring Boot Apps" |
| 4 | +date: 2024-10-26 10:00:00 +0000 |
| 5 | +author: Simon Taylor |
| 6 | +categories: spring docker |
| 7 | +permalink: /spring/docker/2024/10/26/dockerizing-spring-boot-apps |
| 8 | +excerpt: | |
| 9 | + Creating a Docker image for a Spring Boot application allows you to containerize your app, making it easier to run in various environments without |
| 10 | + worrying about dependencies. In this blog, I'll guide you through creating a Docker image for a Spring Boot app using the |
| 11 | + [spt-development-demo project](https://github.com/spt-development/spt-development-demo) as a reference. |
| 12 | + {: class="lead"} |
| 13 | +news: false |
| 14 | +--- |
| 15 | +{{ page.excerpt | remove: '<span class="d-inline d-md-none d-xl-inline">' | remove: '</span>' }} |
| 16 | + |
| 17 | +## Steps to Create a Docker Image for Spring Boot Application |
| 18 | +{: class="fs-4" } |
| 19 | + |
| 20 | +### 1. Clone the Demo Project |
| 21 | +{: class="fs-5" } |
| 22 | + |
| 23 | +First, clone the `spt-development-demo` project from GitHub: |
| 24 | + |
| 25 | +{% gist 5255265b3e4f0c6d2e7adc61c81b7438 %} |
| 26 | + |
| 27 | +### 2. Build the Application with Spring Boot’s Built-in Docker Support |
| 28 | +{: class="fs-5" } |
| 29 | + |
| 30 | +Spring Boot’s `build-image` goal uses Cloud Native Buildpacks to package the application into a Docker image without needing a `Dockerfile`. Simply run the following command: |
| 31 | + |
| 32 | +{% gist d56b35801afef1e49957680bcaf0d123 %} |
| 33 | + |
| 34 | +This command builds the Docker image with the tag spt-development-demo using the JAR created by Spring Boot. |
| 35 | + |
| 36 | +### 3. Run the Docker Container |
| 37 | +{: class="fs-5" } |
| 38 | + |
| 39 | +Once the image is built, run it using Docker. Map port 8080 on your local machine to port 8080 in the container: |
| 40 | + |
| 41 | +{% gist b3168e2443c57f08c6f42c0853f1b722 %} |
| 42 | + |
| 43 | +After running this command, your Spring Boot application will be accessible at `http://localhost:8080`. |
| 44 | + |
| 45 | +### Optional: Push the Docker Image to a Registry |
| 46 | +{: class="fs-5" } |
| 47 | + |
| 48 | +If you want to share your Docker image, you can tag and push it to Docker Hub (or another container registry): |
| 49 | + |
| 50 | +1. Tag the image with your Docker Hub username: |
| 51 | +{% gist 4cfd14d2d1241a07b1a3e399b9385639 %} |
| 52 | +2. Push the image to Docker Hub: |
| 53 | +{% gist 92817e14676f6a5c689b5bdc96248708 %} |
| 54 | + |
| 55 | +## Conclusion |
| 56 | +{: class="fs-4" } |
| 57 | + |
| 58 | +Using Spring Boot’s built-in `build-image` support simplifies the Docker build process. With just a single Maven command, you can package your Spring Boot application as a Docker image without a `Dockerfile`. The `spt-development-demo` project demonstrates this easy method, making it a great choice for deploying Spring Boot apps in Docker. |
| 59 | + |
| 60 | +Happy containerizing! |
0 commit comments