How Dockerize a Spring Boot Java Application or Run Spring Boot Application on Docker

Hello folks,

In this post, we are going to create a spring boot application and run it inside a Docker container.

1. Create a spring boot application 


First, we need a simple spring boot application. We'll use "spring initializr" --> https://start.spring.io/ 


- Just choose WEB dependency and Generate the Project.
- Unzip the project files
- Open the pom.xml via IntelliJ (or if you are using eclipse import the project)



Now we may create a simple rest API. And run it.

code:
package com.nurettinyakit.dockerexample.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hi")
private String sayHi() {
return "Hello";
}
}

Test it, open the endpoint on your browser.
http://localhost:8080/hi


That's it. Now, you have your spring boot application.

Before building the application (or exporting the jar file). Just add this to you pom.xml to rename it.

spring-boot

Full pom.xml

Now you may create your jar file (mvn clean install)


<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nurettinyakit</groupId>
<artifactId>docker-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>docker-example</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</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>
<finalName>spring-boot</finalName>
</build>
</project>
view raw pom.xml hosted with ❤ by GitHub

TL;DR 

  • Create a Dockerfile (this will copy your jar application to the image)
  • Build your image : docker build . -t spring-boot-example
  • Run it. : docker run -i -p 8080:8080 -t spring-boot-example
You may find the project in here : 

2. Create a Dockerfile 

I'm using OpenJDK alpine Java 8 for base. Use alpine versions whenever you may :)
FROM openjdk:8-alpine
ADD target/spring-boot.jar spring-boot.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "spring-boot.jar" ]
view raw Dockerfile hosted with ❤ by GitHub

3. Run Docker! 

- First open your terminal in the project directory.
- Then build the Docker image by this command :
docker build . -t spring-boot-example


And Just Run it!

docker run -i -p 8080:8080 -t spring-boot-example




If you face any issue while running it... it may be related with name of image so you may want to check the name of the image by running the command

docker images


You may check the browser again :)


No comments: