Deploy Spring Boot Application on Docker

In this tutorial, we will learn how to create a docker image for the Spring boot application and deploy a docker image in a docker container.

Note that we are going to Spring boot 3 to create a Spring boot application.

Development Steps

  1. Create a Spring boot application
  2. Build a simple REST API
  3. Create Dockerfile
  4. Build Docker Image
  5. Run Docker Image in a Container
  6. Demo

Create a Spring boot application

Spring Boot provides a web tool called Spring Initializer to bootstrap an application quickly. Just go to https://start.spring.io/ and generate a new spring boot project.

Use the below details in the Spring boot creation:

Project Name: springboot-docker-demo

Project Type: Maven

Choose dependencies: Spring Web

Package name: net.javaguides.springboot

Packaging: Jar

Here is the pom.xml file for your reference:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.0.0</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>net.javaguides</groupId>
	<artifactId>springboot-docker-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-docker-demo</name>
	<description>Demo project for Spring Boot and Docker</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Project Structure

Here is the project structure for your reference:


Build a simple REST API

Let's build a simple REST API to test Spring boot application deployment in a Docker container:
package net.javaguides.springboot.controller;

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

@RestController
public class DockerController {

    @GetMapping("/docker")
    public String dockerDemo(){
        return "Dockerizing Spring Boot Application";
    }
}

Use the following command to maven build this project:
mvn clean package
Once maven builds success, go target folder and you will be able to see the springboot-docker-demo-0.0.1-SNAPSHOT.jar generated:



Create Dockerfile

Docker builds images automatically by reading the instructions from a Dockerfile. The Dockerfile is a text file that contains all commands, in order, needed to build a given image. 

Let's go to the project root directory and create a file named Dockerfile and the following content to it:
FROM eclipse-temurin:17

LABEL mentainer="javaguides.net@gmail.com"

WORKDIR /app

COPY target/springboot-docker-demo-0.0.1-SNAPSHOT.jar /app/springboot-docker-demo.jar

ENTRYPOINT ["java", "-jar", "springboot-docker-demo.jar"]

FROM: A docker image can use another image available in the docker registry as its base or parent image. In the above example, we use the openjdk:11 image as our base image.

LABEL: The LABEL instruction is used to add metadata to the image. In the above Dockerfile, we have added some info about the maintainer of the image through LABEL instruction.

WORKDIR: This instruction creates a working directory in a docker container.

COPY: The COPY instruction copies new files or directories and adds them to the filesystem of the container at the path.

ENTRYPOINT: This is where you configure how the application is executed inside the container.

Build Docker Image

Now that we have defined the Dockerfile, let’s build a docker image for our application.

Before building the docker image, you need to make sure that you’ve packaged the application in the form of a jar file using maven. 

Let’s now build the docker image by typing the following command:

docker build -t springboot-docker-demo .

The file path defines the location of the Dockerfile in the current directory, and the -t argument tags the resulting image, where the repository name is the springboot-docker-demo and the tag is the latest.

After the build is successfully finished, we can check to see if it appears in the list of docker images available locally. To do so, we can execute the below command.
docker images

Output:

rameshfadatare@Rameshs-MacBook-Air springboot-docker-demo % docker images
REPOSITORY               TAG       IMAGE ID       CREATED          SIZE
springboot-docker-demo   latest    8d61ab5ecfb1   36 seconds ago   667MB
docker-demo              latest    09c04b790f98   3 hours ago      667MB

Run Docker Image in a Container

Once you have a docker image, you can run it using the docker run command like so:
docker run -p 8080:8080 springboot-docker-demo
With the -p option, we expose the container's 8080 port to the host's 8080. (The host value is first.)

By default, when you run a container using docker run, it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, or to Docker containers that are not connected to the container’s network, use the --publish or -p flag.

Running the docker image in the background, in detached mode. You can use the -d option in the docker run command to run the container in the background -

 docker run -d -p 8080:8080 springboot-docker-demo

The above command starts the container in the background and gives you the container ID.

You can see the list of all containers running in your system using the following command:

docker container ls

Demo

Once the docker image running in a container. Open the browser and hit this link in the browser http://localhost:8080/docker
You will see the below REST API response message in a browser:

Dockerizing Spring Boot Application

Conclusion

In this tutorial, we have seen step-by-step how to dockerize the Spring boot application. 

Here are the docker commands, we have used in this tutorial:
Build Docker image:
docker build -t springboot-docker-demo .
List docker images:
docker images
Run image in a docker container:
docker run -p 8080:8080 springboot-docker-demo
Run the docker container in a detached mode (background):
 docker run -d -p 8080:8080 springboot-docker-demo
Running containers:
docker container ls

Comments