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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
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 :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM openjdk:8-alpine | |
ADD target/spring-boot.jar spring-boot.jar | |
EXPOSE 8080 | |
ENTRYPOINT ["java", "-jar", "spring-boot.jar" ] |
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:
Post a Comment