Running SpringBoot app on Docker

Example

pom.xml

<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>
	<groupId>com.demo</groupId>
	<artifactId>hellospring</artifactId>
	<version>1</version>
	<name>HelloSpring</name>
	<description>HelloSpring</description>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.9</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<properties>
		<java.version>1.8</java.version>
		<maven.compiler.source>${java.version}</maven.compiler.source>
		<maven.compiler.target>${java.version}</maven.compiler.target>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<start-class>com.hellospring.App</start-class>
	</properties>
	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-maven-plugin</artifactId>
				</plugin>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-dependency-plugin</artifactId>
					<executions>
						<execution>
							<goals>
								<goal>sources</goal>
								<goal>resolve</goal>
							</goals>
							<configuration>
								<classifier>javadoc</classifier>
							</configuration>
						</execution>
					</executions>
				</plugin>
				<plugin>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-maven-plugin</artifactId>
					<configuration>
						<mainClass>com.hellospring.App</mainClass>
						<layout>JAR</layout>
					</configuration>
					<executions>
						<execution>
							<goals>
								<goal>repackage</goal>
							</goals>
						</execution>
					</executions>
				</plugin>
			</plugins>
		</pluginManagement>
		<finalName>hello-spring</finalName>
	</build>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<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>
</project>

Main class

package com.hellospring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

Controller Class

package com.hellospring;

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

@RestController()
@RequestMapping("/hello")
public class AppController {
	@GetMapping
	public String helloGradle() {
		System.out.println("Hello !");
		return "Hello !";
	}
}

Dockerfile

FROM maven AS build
MAINTAINER suraj kutteri <gill.tyson332@gmail.com>
WORKDIR /opt/build
COPY . .
#RUN mvn -B package --file pom.xml
RUN mvn -B package --file pom.xml spring-boot:repackage
FROM openjdk
#RUN groupadd --gid 1200 tyson332 && adduser --disabled-password --home /app --gecos "" --uid 1200 --gid 1200 tyson332
RUN groupadd --gid 1200 tyson332 && adduser --home /app --uid 1200 --gid 1200 tyson332
WORKDIR /app
COPY --from=build /opt/build/target/*.jar app.jar
COPY start.sh start.sh
RUN chmod +x start.sh
# RUN chown -R atomuser:atomuser /app
#ENTRYPOINT ["/app/start.sh"]
CMD ["java","-jar","app.jar"]

Create an Image of Docker

docker build -t spring_build_test:1 .

Run the Docker Image

#First Find the Id of docker image using below command
docker images
#Run the docker image by using below command
docker run <image_id>
docker run 86b3f95d7c91

Basics of k8 Deployment

  • You describe a desired state in deployment
  • Traditionally deployment a usually done by shell scripts which execute line by line from top to bottom
  • In k8 deployment, you don’t write that but instead, we describe how the deployment will be with the help of a YAML file (desired state) and k8 takes care of the reset
  • The deployment controller is the component that takes the deployment file and relays the information to POD and replica set about the deployment
  • Control loop components are the objects which which compare the actual state and the desired state and scales the application according to the need mentioned in the deployment yaml file

Benefits :

  • You can scale the application with n number of pods on demand

Deployment.yaml for a Image which is already present on the local system (Docker Desktop)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: spring-boot-test
spec:
  replicas: 2
  selector:
    matchLabels:
      app: spring-boot-test
  template:
    metadata:
      labels:
        app: spring-boot-test
    spec:
      containers:
      - name: spring-boot-test
        image: spring_build_test:1
        ports:
        - containerPort: 9090

Running the deployment.yaml using kubectl

kubectl apply -f k8\deployment.yaml

Checking the deployment status

kubectl get deploy

Check the deployment description (Step1 – Debugging)

kubectl describe deployment spring-boot-test

Check the pods description (Step2 – Debugging)

kubectl get pods
kubectl describe pod spring-boot-test-7c9d4947dd-4lz9n

Check the logs of pods (Step3 – Debugging)

kubectl get pods
kubectl logs spring-boot-test-7c9d4947dd-4lz9n | tail -20

Summary :

  • How to Start a deployment
    • kubectl apply -f k8\deployment.yaml
  • How to delete a deployment
    • kubectl delete deploy spring-boot-test
  • How to check the status of deployment
    • kubectl get deploy
  • Details for debugging
    • Check Deployment
      • kubectl get deployments
      • kubectl describe deployments <deployment_name>
    • Check Pods
      • kubectl get pods
      • kubectl describe pods <pod_name>
    • Check Logs of pods
      • kubectl logs <pod_name>