Building Your First Docker Image for a Java Spring Boot REST service
Posted on September 14, 2024 • 5 min read • 903 wordsDiscover how to build your first Docker image for a Java Spring Boot REST service. This step-by-step guide covers writing Dockerfiles, building and running Docker images, optimizing for performance, and deploying your Spring Boot application using Docker
In the modern development landscape, Docker has become a vital tool for containerizing applications, making it easier to develop, ship, and run applications in isolated environments. Java Spring Boot is one of the most popular frameworks for building REST services, and with Docker, you can deploy your Spring Boot applications quickly and efficiently. In this blog post, we’ll walk through the steps of building your first Docker image for a Java Spring Boot REST service.
Before we dive into Dockerizing a Spring Boot application, let’s briefly understand the core concepts of Docker and Spring Boot.
By containerizing a Spring Boot application, you ensure it can run on any system that has Docker installed, regardless of its environment.
Let’s start by creating a simple Spring Boot REST service. If you haven’t already set up a Spring Boot application, follow these steps:
HelloController.java
:package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String helloWorld() {
return "Hello, Docker!";
}
}
mvn clean package
This will generate a JAR file located in the target
directory.
Once your Spring Boot REST service is ready, the next step is to containerize it using Docker. To do this, we need to write a Dockerfile — a text document containing all the commands required to assemble the image.
The Dockerfile serves as the blueprint for building the Docker image. It specifies the base image, copies the necessary files, and defines how the application should run inside the container. Here’s an example of a Dockerfile for a Spring Boot application:
# Use an official OpenJDK runtime as a parent image
FROM openjdk:17-jdk-alpine
# Set the working directory inside the container
WORKDIR /app
# Copy the JAR file from the target directory to the container
COPY target/demo-0.0.1-SNAPSHOT.jar app.jar
# Expose the port on which the application will run
EXPOSE 8080
# Define the command to run the Spring Boot application
ENTRYPOINT ["java", "-jar", "app.jar"]
In this Dockerfile:
Now that the Dockerfile is written, you can proceed with building the Docker image and running the container.
docker build -t springboot-docker-demo .
The -t
flag assigns a name (springboot-docker-demo
) to the image. The .
indicates that the Dockerfile is located in the current directory.
docker run -p 8080:8080 springboot-docker-demo
This command maps port 8080 on the host machine to port 8080 inside the container. After running the command, the application will be accessible at http://localhost:8080/hello
.
If you want to share your Docker image or use it in a CI/CD pipeline, you need to push it to a Docker repository. The most commonly used repository is Docker Hub, but you can also use other repositories like GitHub Container Registry or Amazon ECR.
Here’s how to push the image to Docker Hub:
docker login
docker tag springboot-docker-demo <your-username>/springboot-docker-demo
docker push <your-username>/springboot-docker-demo
Once pushed, you can pull and run the image on any machine with Docker installed.
By following the steps outlined in this guide, you’ve successfully created your first Docker image for a Java Spring Boot REST service. Dockerizing your applications ensures that they are portable and can be run consistently across different environments, making deployment and scaling much easier.
To summarize:
With these foundational skills, you can now explore more advanced Docker concepts, such as multi-stage builds, Docker Compose, and Kubernetes for orchestrating containerized applications.