ตัวอย่างการเขียน Spring-boot Reactive + Dockerfile
- มีความรู้เรื่อง Docker
- ถ้ายังไม่แม่น สามารถอ่านเพิ่มเติมได้ที่ พื้นฐาน Docker
- ในเครื่องมีการติดตั้ง Docker แล้ว
- สำหรับ Ubuntu Server สามารถทำตามนี้ได้ ติดตั้ง Docker บน Ubuntu 18.04
pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
<configuration>
<additionalProperties>
<java.version>${java.version}</java.version>
</additionalProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
@SpringBootApplication
@ComponentScan(basePackages = {"me.jittagornp"})
public class AppStarter {
public static void main(String[] args) {
SpringApplication.run(AppStarter.class, args);
}
}
@RestController
public class HomeController {
@GetMapping({"", "/"})
public Mono<String> hello() {
return Mono.just("Hello world.");
}
}
ไว้ที่ root ของ project /Dockerfile
FROM openjdk:11-jre-slim
EXPOSE 8080
ADD target/*.jar /app.jar
ENTRYPOINT java $JAVA_OPTS -jar /app.jar
cd ไปที่ root ของ project จากนั้น
$ mvn clean package
$ docker build -t hello-world .
$ docker run -d -p 8080:8080 --name hello-world hello-world
เปิด browser แล้วเข้า http://localhost:8080