[Microservices] First project of Spring Boot and Docker
Share one very simple project, which I did as my first project for learning microservices and docker. Thanks.
Prerequisite
Following softwares must be installed, and they would better be the latest version
- JDK
- Maven
- Docker
Spring boot project, helloworld
A very simple java web application built with spring boot. It only contains 2 files, pom and one java class.
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.b5wang.cloud</groupId>
<artifactId>helloword</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<properties>
<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>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application.java
package com.b5wang.cloud.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class Application {
@RequestMapping("/")
public String home() {
return "Hello cloud World!";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Now run maven build, you can get helloworld.jar
Build docker image, helloworld
Create a new folder at another place, named helloworld-image. Then copy helloworld.jar into it.
Create a empty file called Dockerfile.
Dockerfile
FROM openjdk:8-jdk-alpine
COPY helloword.jar /app/helloword.jar
ENTRYPOINT ["java","-jar","/app/helloword.jar"]
Build image
> cd helloworld-image
> docker build --tag=helloworld .
Sending build context to Docker daemon 16.79MB
Step 1/3 : FROM openjdk:8-jdk-alpine
---> a3562aa0b991
Step 2/3 : COPY helloword.jar /app/helloword.jar
---> 3f25eef0547b
Step 3/3 : ENTRYPOINT ["java","-jar","/app/helloword.jar"]
---> Running in 0307850d2c03
Removing intermediate container 0307850d2c03
---> a8aedecd490e
Successfully built a8aedecd490e
Successfully tagged helloworld:latest
Run image
> docker run -p 4000:8080 helloworld
Spring boot is using port 8080 by default.
Now open browser, access http://localhost:4000/. You should see “Hello cloud World!”
Reference
[1] https://spring.io/guides/gs/spring-boot-docker/
[2] https://docs.docker.com/get-started/part2/