Deploy Spring Boot Application Using Docker with Real Examples

One of Docker's biggest advantages is making application deployment simple and consistent. Modern Spring Boot applications are commonly packaged into Docker containers before being deployed to cloud platforms, Kubernetes clusters and production servers.

Learn how to deploy a Spring Boot application using Docker. Understand Dockerfile creation, image building, container deployment and port mapping with real examples.

In this article, we will learn how to containerize a Spring Boot application, create a Docker Image and run it inside a Docker Container.

Why Deploy Spring Boot Using Docker?

Traditionally, deploying a Spring Boot application requires installing Java, configuring environments and managing dependencies on the target server.

Install Java

Install Dependencies

Configure Environment

Deploy Application

Docker simplifies this process by packaging everything into a single container.

Spring Boot App

+

Java Runtime

+

Dependencies

=

Docker Container

Spring Boot Deployment Workflow

Source Code

|

v

Maven Build

|

v

JAR File

|

v

Docker Image

|

v

Docker Container

|

v

Production

This workflow ensures the application behaves consistently across all environments.


Step 1: Create a Spring Boot Application

Suppose we have a simple REST API.


@RestController

public class WelcomeController {

    @GetMapping("/")

    public String welcome() {

        return "Welcome to JavaCompass";

    }
}

This application will be packaged and deployed using Docker.


Step 2: Build the Application JAR

Generate the executable JAR file using Maven.


mvn clean package

After a successful build:


target/

|

v

app.jar

The JAR file will be used inside the Docker Image.


Step 3: Create Dockerfile

Create a Dockerfile in the project root directory.


FROM eclipse-temurin:21

WORKDIR /app

COPY target/app.jar app.jar

EXPOSE 8080

CMD ["java","-jar","app.jar"]

Understanding the Dockerfile

FROM

Defines the base Java image.


FROM eclipse-temurin:21

WORKDIR

Sets the working directory.


WORKDIR /app

COPY

Copies the Spring Boot JAR to the image.


COPY target/app.jar app.jar

EXPOSE

Documents application port.


EXPOSE 8080

CMD

Starts the Spring Boot application.


CMD ["java","-jar","app.jar"]

Step 4: Build Docker Image

Execute:


docker build -t springboot-app .

Docker reads the Dockerfile and creates an image.


Verify image creation:


docker images

Step 5: Run Docker Container

Start a container using the image.


docker run -p 8080:8080 springboot-app

Explanation:


Host Port     8080

|

v

Container Port 8080

Requests sent to localhost:8080 are forwarded to the application running inside the container.


Running in Detached Mode

Run the container in the background.


docker run -d

-p 8080:8080

springboot-app

This is the preferred approach for production workloads.


Verify Running Application

Check active containers:


docker ps

Example:


CONTAINER ID

IMAGE

STATUS

PORTS

Open browser:


http://localhost:8080

Response:


Welcome to JavaCompass

Container Logs

View application logs:


docker logs container-id

Logs help developers troubleshoot runtime issues.


Stop the Container

List running containers:


docker ps

Stop container:


docker stop container-id

Container Lifecycle

Docker Image

|

v

Container Created

|

v

Container Running

|

v

Container Stopped

|

v

Container Removed

Real World Architecture

A modern Spring Boot deployment often looks like:


Spring Boot

|

v

Docker Image

|

v

Docker Container

|

v

Kubernetes

|

v

Production Environment

This architecture is widely adopted in enterprise systems.


Benefits of Using Docker for Spring Boot

  • Consistent Deployment
  • Easy Scaling
  • Environment Independence
  • Simplified DevOps Workflows
  • Cloud Native Compatibility

Common Deployment Mistakes

  • Wrong JAR file path
  • Incorrect port mapping
  • Missing Dockerfile
  • Building outdated code
  • Using incompatible Java versions

Common Interview Questions

Why deploy Spring Boot using Docker?

Docker provides portability, consistency and simplified deployment across environments.


What does EXPOSE 8080 mean?

It documents the application port used inside the container.


What is the purpose of port mapping?

Port mapping allows external systems to access services running inside containers.


Which command builds a Docker image?

Use:


docker build -t image-name .

Which command starts a container?

Use:


docker run image-name

Best Practices

  • Use lightweight JDK base images.
  • Use fixed image versions.
  • Keep Dockerfiles simple.
  • Monitor application logs.
  • Store configuration outside images.

Frequently Asked Questions (FAQ)

Can Docker run multiple Spring Boot applications?

Yes. Multiple applications can run in separate containers using different ports.


Can Docker containers be moved to another server?

Yes. The same image can be deployed anywhere Docker is installed.


Can Kubernetes use Docker Images?

Yes. Kubernetes commonly deploys applications packaged as Docker Images.


Conclusion

Deploying Spring Boot applications with Docker is one of the most important skills for modern Java developers. Docker simplifies packaging, deployment and scaling while ensuring consistent behaviour across development, testing and production environments. Understanding this workflow prepares developers for Kubernetes, cloud computing and microservices deployments.


Keywords

Spring Boot Docker, Deploy Spring Boot Using Docker, Docker Spring Boot Tutorial, Dockerfile Spring Boot, Spring Boot Containerization, Docker Build, Docker Run, Docker Deployment, Java Docker Tutorial, Spring Boot Microservices, Docker Interview Questions, Cloud Native Java, DevOps Tutorial, Containerized Applications, Kubernetes Preparation



Deploy Spring Boot Application Using Docker with Real Examples Deploy Spring Boot Application Using Docker with Real Examples Reviewed by 13thGhost on 4:00 PM Rating: 5

Recommended Posts

Powered by Blogger.