How to Deploy a Cloud-Native Spring Boot Application with Docker

Docker

YOUR COSMETIC CARE STARTS HERE

Find the Best Cosmetic Hospitals

Trusted • Curated • Easy

Looking for the right place for a cosmetic procedure? Explore top cosmetic hospitals in one place and choose with confidence.

“Small steps lead to big changes — today is a perfect day to begin.”

Explore Cosmetic Hospitals Compare hospitals, services & options quickly.

✓ Shortlist providers • ✓ Review options • ✓ Take the next step with confidence

Moving a cloud-native Spring Boot application into a Docker container is a practical first step toward a 12-factor deployment model. This walkthrough uses a sample rock-paper-scissors-style application (Spock Lizard) to demonstrate the full workflow: cloning the source, building the JAR with Maven, writing a Dockerfile, building the image, and running the container.

Source: TheServerSide.com

Prerequisites

Before you begin, make sure the following tools are installed on your Linux machine (Ubuntu or equivalent):

  • JDK — to compile and run Java code
  • Git — to clone the sample repository
  • Apache Maven — to build the executable JAR
  • Docker — to build and run container images

Step 1: Clone the Repository

Use Git to pull down the sample application from GitHub:

git clone http://github.com/cameronmcnz/spock-lizard-docker.gitCode language: PHP (php)

This places the full project source tree on your local machine, ready for a Maven build.

Step 2: Build the Executable JAR with Maven

Change into the project directory and run the Maven clean install lifecycle phase. Maven will compile the code, run any unit tests, and package everything — including an embedded Tomcat server — into a single self-contained JAR file.

cd spock-lizard-docker
mvn clean install

Step 3: Verify the Application Runs Locally

Before containerising, confirm the JAR works correctly on your local JVM:

cd target
java -jar spock-lizard-1.0.jarCode language: CSS (css)

Once the application starts, open a browser and navigate to http://localhost:8080. You should see the application’s home page. When you are satisfied it runs correctly, close the terminal window to stop the JVM process before proceeding.

Spring Boot application running locally in a browser at localhost:8080
The Spring Boot application running locally before containerisation.

Step 4: Build the Docker Image

The repository already includes a Dockerfile in the project root, so you do not need to write one from scratch. From the project root directory, run:

sudo docker build -t spock-lizard-cameron .

A few things to note about this command:

  • The -t flag assigns a human-readable tag (name) to the image. The tag name does not need to match the application name — here it is deliberately set to spock-lizard-cameron to illustrate that point.
  • The trailing . (dot) is the build context: it tells Docker where to look for the Dockerfile and any files it references. Omitting it will cause the build to fail.

Step 5: Run the Application Inside a Container

Start a container from the image you just built:

sudo docker run -p 8099:8080 -t spock-lizard-cameronCode language: CSS (css)

The -p 8099:8080 flag configures port mapping between the host and the container:

  • 8099 — the port exposed on your host machine (what you type in the browser)
  • 8080 — the port the Spring Boot application listens on inside the container

Once the container is running, open your browser and navigate to http://localhost:8099 to access the application through the container.

Understanding the Workflow

The deployment pattern demonstrated here reflects a standard Spring Boot containerisation approach:

  1. Build once — Maven produces a fat JAR containing the application and an embedded Tomcat instance.
  2. Package once — Docker wraps that JAR in a lightweight, reproducible image using a Dockerfile.
  3. Run anywhere — The container image can be started on any host with Docker installed, without requiring a separately installed JDK or application server on that host.

This model aligns directly with the 12-factor app methodology, particularly the principles of explicit dependency declaration, environment parity, and stateless processes.

Next Steps

Once you are comfortable building and running a single container locally, consider these natural next steps:

  • Push the image to a container registry (Docker Hub, Amazon ECR, or Google Artifact Registry) so it can be deployed to remote hosts.
  • Use Docker Compose to manage multi-container setups (e.g., adding a database sidecar).
  • Explore Kubernetes or a managed container service (ECS, Cloud Run) for production-grade orchestration and scaling.
  • Integrate the docker build step into a CI/CD pipeline (Jenkins, GitHub Actions, GitLab CI) to automate image creation on every commit.
0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x