[JAVA] Spring Boot gradle build with Docker

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

Creating a Spring Boot project

Spring Initializr https://start.spring.io/ Create a template for your Spring Boot project using

Project
Change to Gradle Project
Dependencies
Select Spring Web from ADD DEPENDENCIES ...

1.png

Click GENERATE to start downloading the zip file The unzipped contents are the project file

Open project

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

Controller added

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

Dockerfile creation

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

Docker build & launch

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

Reference material

https://spring.io/guides/gs/spring-boot/

Recommended Posts

Spring Boot gradle build with Docker
Build Spring Boot project by environment with Gradle
(IntelliJ + gradle) Hello World with Spring Boot
Download with Spring Boot
Spring Boot environment construction with Docker (January 2021 version)
Create a website with Spring Boot + Gradle (jdk1.8.x)
Run Scala applications with Spring Boot through Gradle
Build Java x Spring x VSCode x Gradle on Docker (1)
I wanted to gradle spring boot with multi-project
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Spring Boot 2 multi-project in Gradle
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Build Ubuntu 18.04.5 with dual boot
Hello World with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
Build DynamoDB local with Docker
How to build docker environment with Gradle for intelliJ
Build Couchbase local environment with Docker
◆ Spring Boot + gradle environment construction memo
Build a Node.js environment with Docker
spring × docker
Create an app with Spring Boot 2
Database linkage with doma2 (Spring boot)
Build PlantUML environment with VSCode + Docker
Spring Boot programming with VS Code
Build environment with vue.js + rails + docker
Until "Hello World" with Spring Boot
Get validation results with Spring Boot
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
Implement Spring Boot application in Gradle
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
Build docker + laravel environment with laradock
Message cooperation started with Spring Boot
Set Spring profile when executing bootRun task with Spring Boot Gradle Plugin
Image Spring Boot app using jib-maven-plugin and start it with Docker
Processing at application startup with Spring Boot
Build a PureScript development environment with Docker
Hello World with Eclipse + Spring Boot + Maven
Send regular notifications with LineNotify + Spring Boot
Perform transaction confirmation test with Spring Boot
CICS-Run Java applications-(3) Build management with Gradle
Create Spring Boot-gradle-mysql development environment with Docker
Try using Spring Boot with VS Code
Start web application development with Spring Boot
Launch Nginx + Spring Boot application with docker-compose
Build a Wordpress development environment with Docker
Implement CRUD with Spring Boot + Thymeleaf + MySQL