Build a Java development environment with Spring Boot with Docker. Use Java 11 and gradle.
Up to the point where Hello World! Is displayed. (Update: Added remote debugging method)
https://github.com/tomoyuki-mikami/spring-boot-docker
Execution method
$ cd docker
$ docker-compose up -d
MacOS Catalina 10.15.2 Docker for Mac 2.1.0.5 IntelliJ IDEA Ultimate 2019.3.1 OpenJDK 11 (installed with homebrew)
https://start.spring.io/ If you make the same selection here and Generate, it will be downloaded as a zip, so use that.
Modify Application.java under src / main / java / com.mika.app
Application.java
package com.mika.app;
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;
@RestController
@SpringBootApplication
public class Application {
@RequestMapping("/")
String Index() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Try running it in IntelliJ at this stage → Maybe it will fail. Press the run button next to public class Application {.
I need to comment out because I put too many things in dependencies and it works in vain.
build.gradle
dependencies {
// implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// implementation 'org.springframework.boot:spring-boot-starter-security'
// implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
// runtimeOnly 'mysql:mysql-connector-java'
// annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
testImplementation 'org.springframework.security:spring-security-test'
}
Now run again Display http: // localhost: 8080 and confirm that Hello World is displayed. After confirming, stop with the stop button.
Create a docker directory and create Dockerfile and docker-compose.yml under it The reason for using docker-compose is that we plan to build MySQL and nginx here in the future.
Dockerfile
FROM openjdk:11
ENV APP_ROOT /app
COPY . $APP_ROOT
WORKDIR $APP_ROOT
ENTRYPOINT ["sh", "./gradlew", "bootRun"]
docker-compose.yml
version: '3'
services:
web:
build:
#Specify the path required for copying the app in the Dockerfile
context: ../
#The location of the Dockerfile as seen from the context path
dockerfile: docker/Dockerfile
volumes:
# :The left part fits your environment
- ~/Dev/java/spring-boot:/app
ports:
- "8080:8080"
$ cd docker
$ docker-compose up
Confirm that it is started, and confirm that http: // localhost: 8080 is displayed again.
HotDeploy Since we are using spring-boot-devtools, HotDeploy is applied. If you change the characters of Hello World in Application.java You can see Java restart after a few seconds (from the docker-compose up log) If you reload the browser, you can see that the characters have changed.
Since I want to set a breakpoint in development and stop it, I set remote debugging. First, open Run → Edit Configurations ... from the IntelliJ menu. Select Remote from the + button in the upper left. With the following settings, press OK to close.
build.gradle
bootRun {
// for remote debug
jvmArgs "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"
}
Since port 5005 is used for remote debugging, it is described.
docker-compose.yml
ports:
- "8080:8080"
# for remote debug
- "5005:5005"
Do docker-comose up again and check that it is in the standby state.
web_1 | > Task :bootRun
web_1 | Listening for transport dt_socket at address: 5005
Put a breakpoint in Hello World in Application.java If you access http: // localhost: 8080, you can see that it stops at that location.
For the time being, I have summarized how to build it easily.
Recommended Posts