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