For a long time the build was maven, but as a reminder I needed to use gradle Gradle build Spring Boot using docker The procedure for making a multi-stage build and deploying to Cloud Run or ECS will be released at a later date
Spring Initializr https://start.spring.io/ Create a template for your Spring Boot project using
Click GENERATE to start downloading the zip file The unzipped contents are the project file
Open a project with a text editor or IDE This time I will describe the procedure of IntelliJ IDEA which I do not usually use
File → New → Project from Existing Source → Select the unzipped folder → Open Check Import project from external model → Select Gradle → Finish
Since there is no controller in the initial state, we will add a controller that returns a simple character string Create HelloController.java in the same package as ~ Application.java under project/src/main ...
HelloController.java
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Hello Spring Boot!";
}
}
If you are using the IDE, you can run the Spring Boot application at this point and check the operation. Run the application and access localhost: 8080 Hello Spring Boot! Is displayed, it is operating normally.
In case of IntelliJ IDEA, it can be executed from Gradle on the far right
Create a Dockerfile directly under the project A simple image that just copies the file and executes the build command
Dockerfile
FROM gradle:6.7.1-jdk11
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
CMD ["gradle", "build", "bootJar"]
Using gradle: 6.7.1-jre11 as base image causes nullpo during build The image of jdk is big, so I want to avoid it if it is true
In the console, change to the project directory and execute the following command
docker build -t bootbuilder:v0.1 .
docker run -it --rm -v "$PWD":/home/gradle/src bootbuilder:v0.1
Build complete if jar is created in ~ build/libs
https://spring.io/guides/gs/spring-boot/
Recommended Posts