Enterprise Java

Containerize a Spring Boot Application With Podman Desktop

Containerization is a critical aspect of modern software development, enabling developers to package applications with all their dependencies into isolated environments. Let us delve into understanding the process of containerizing a Spring Boot application using Podman Desktop.

1. Introduction

Podman is a daemonless container engine that allows you to manage containers, pods, and images. Podman Desktop provides a graphical interface to manage these resources, making it easier for developers to work with containers.

1.1 Comparison between Podman and Docker Desktop

FeaturePodmanDocker Desktop
Container Engine TypeDaemonless (No central daemon process)Daemon-based (Requires Docker daemon)
Rootless ModeSupports rootless mode, allowing non-root users to run containersSupports rootless mode, but traditionally runs with root privileges
CompatibilityCompatible with Docker CLI and Dockerfile syntaxStandard Docker CLI and Dockerfile syntax
Platform SupportLinux, macOS, Windows (with WSL2)Linux, macOS, Windows
Graphical InterfacePodman Desktop (Graphical UI for managing containers and images)Docker Desktop (Full-featured GUI for managing containers, images, volumes, etc.)
Kubernetes IntegrationIntegrated with Kubernetes through CRI-OBuilt-in Kubernetes support (Single-node Kubernetes cluster)
SecurityHigh security with rootless containers and separation from system daemonSecurity is managed through Docker daemon; which traditionally runs with root privileges
LicensingOpen-source (Apache License 2.0)The community edition is open-source; enterprise features are proprietary
Community SupportGrowing community, strong support in the Red Hat ecosystemLarge and established community, widespread adoption
Pod SupportSupports pod-based deployments nativelySupports pods through Docker Compose or Kubernetes

2. Installing Podman Desktop

To get started with Podman Desktop, you first need to install it on your system. Follow the steps below to install Podman Desktop on your preferred operating system:

  • Linux: Download the Podman Desktop package from the official Podman Desktop website and install it using your package manager.
  • macOS: Use Homebrew to install Podman Desktop with the command: brew install --cask podman-desktop.
  • Windows: Download the Podman Desktop installer from the official website and follow the installation instructions.

After installation, launch Podman Desktop to ensure it is properly installed and running.

3. Creating a Spring Boot Application

Next, we will create a simple Spring Boot application that we will containerize using Podman Desktop. To create a Spring Boot application, follow these steps:

  • Navigate to the Spring Initializr website.
  • Choose the following settings:
    • Project: Maven
    • Language: Java
    • Spring Boot: 3.0.0 or later
    • Packaging: Jar
    • Java Version: 17 or later
  • Add the following dependencies: Spring Web
  • Click Generate to download the project.
  • Extract the downloaded project and open it in your preferred IDE.

In the src/main/java/com/example/demo directory, create a new class named HelloworldController and add the following code:

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class HelloworldController {

    @GetMapping("/hello-world")
    public String welcome() {
        return "hello world";
    }
}

This simple REST controller defines an endpoint hello-world that returns a greeting message.

4. Containerfile

A Containerfile (also known as a Dockerfile) defines the steps required to package your application into a container image. Create a file named Containerfile in the root directory of your Spring Boot project with the following content:

FROM openjdk:17-alpine

# Set the working directory inside the container
WORKDIR /app

# Copy the Spring Boot jar file to the container
COPY target/*.jar app.jar

# Expose the port on which the app will run
EXPOSE 8080

# Run the Spring Boot application
CMD ["java", "-jar", "app.jar"]

4.1 Explanation

The given code is a Dockerfile that is used to containerize a Spring Boot application.

  • FROM openjdk:17-alpine: This line specifies the base image for the container, which is the OpenJDK 17 runtime on an Alpine Linux distribution. Alpine is a lightweight, security-focused Linux distribution, making it ideal for creating small container images.
  • WORKDIR /app: This command sets the working directory inside the container to /app. All subsequent commands, such as copying files or running commands, will be executed from this directory.
  • COPY target/*.jar app.jar: This line copies the compiled Spring Boot jar file from the target directory on the host machine to the /app directory in the container, renaming it to app.jar. The wildcard *.jar is used to match any jar file in the target directory.
  • EXPOSE 8080: This command informs Docker that the container will listen on port 8080 at runtime. It does not publish the port; it simply indicates that the application inside the container expects to receive traffic on port 8080.
  • CMD [“java”, “-jar”, “app.jar”]: This line defines the command that will be executed when the container starts. In this case, it runs the Spring Boot application using the Java command, telling Java to run the app.jar file.

5. Building an Image Using Podman Desktop

With Podman Desktop, you can build the container image directly from the graphical interface. Follow these steps:

  • Open Podman Desktop and navigate to the Images tab.
  • Click on Build and select the directory containing your Spring Boot project and the Containerfile.
  • Specify a name for your image, e.g., hello-world-from-podman-demo.
  • Click Build to start the image creation process.

Podman Desktop will execute the steps defined in the Containerfile and build the container image. If everything goes well the image will be created as shown below.

Spring boot podman desktop-image1
Fig. 1: Image created successfully

6. Running a Container

After building the image, you can run it as a container using Podman Desktop:

  • Navigate to the Containers tab in Podman Desktop.
  • Click on Run and select the image hello-world-from-podman-demo you built in the previous step.
  • Specify the required variables (if any).
  • Click Run to start the container.

Once the container is running, you can access the Spring Boot application by navigating to http://localhost:8080/api/hello-world in your web browser. You should see the message “hello world” displayed.

Spring boot podman desktop-img2
Fig. 2: Application output

7. Conclusion

In this article, we covered the process of containerizing a Spring Boot application using Podman Desktop. We started by installing Podman Desktop, creating a Spring Boot application, and defining a Containerfile. We then built the container image and ran the application as a container. Podman Desktop simplifies the management of containers and images, making it a great tool for developers looking to containerize their applications.

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button