Skip to content

Blog rovingdev

Go back

Guide to : Java Spring Boot Docker Container Image

Edit page

In this tutorial, let’s create a simple Docker container of a spring boot REST java application using a Dockerfile. The process mentioned in the article will also help to create reusable and redistributable code image.

What this tutorial will have ? πŸ€”:

Default alt text


Prerequisites βœ… :

Why spring is used in this example?

Because historically, when there were not many web frameworks, people used to prefer spring MVC/ spring boot. Even when there are plenty of better frameworks available in this era, spring is still used in legacy application as well as it has large community support than most JVM based web framework.

Before we proceed, I would like to gently encourage you to have a basic understanding of Docker and containers. If you feel you’d like to learn more, I have a post that offers some helpful insights. Please feel free to take a look at it when you have the time, here is the link ⬇️ https://rovingdev.com/p/unlocking-docker-a-beginners-guide

Let’s have a look at code. 🧐

I created a really simple REST service in Java with the help of spring boot. This is the directory structure of the code:

Terminal window
.
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ README.md
β”œβ”€β”€ pom.xml
└── src
└── main
β”œβ”€β”€ java
β”‚ └── com
β”‚ └── example
β”‚ └── spring_demo
β”‚ └── SpringDemoApplication.java
└── resources
└── application.properties

The main class SpringDemoApplication.java :

package com.example.spring_demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class SpringDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDemoApplication.class, args);
}
@GetMapping("/hello")
public String hello(@RequestParam(value = "name",
defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
@GetMapping("/")
public String defaultLanding() {
return "Hello from Docker";
}
}

URL mappings:

Dockerfile contents:

Terminal window
FROM docker.io/maven:3.9-eclipse-temurin-17 as base
LABEL org.opencontainers.image.authors="https://github.com/sukumaar"
ARG jar_file_name=*.jar
ARG arg_port=8080
ENV env_port=$arg_port
ENV HOME /
COPY ${jar_file_name} /runnable-jar.jar
ENTRYPOINT ["java","-jar","/runnable-jar.jar","--server.port=${env_port}"]

Commands πŸ’» :

Terminal window
mvn clean compile package
java -jar target/spring-demo-0.0.1-SNAPSHOT.jar --server.port:8010
docker build --build-arg jar_file_name=target/spring-demo-0.0.1-SNAPSHOT.jar -t spring-docker-demo .
docker run -e "env_port=8087" -p 8008:8087 localhost/spring-docker-demo

FYI, I used a Linux machine (Ubuntu 24.04) to run the above commands.

Why you should prefer using Docker images for production ? βš™οΈ


Edit page
Share this post on:

Previous Post
Poetry : A Better Alternative to requirements.txt. The Maven for Python ?
Next Post
Apache Spark Unit Testing Strategies