使用 Spring Boot 和 Docker 搭建 Java 开发环境
总结
使用Docker构建Spring Boot的Java开发环境,使用Java 11和gradle。
你好,世界!直到显示出”Hello World”为止。(更新:添加了远程调试的方法)
完工产品
执行方法 (shí fǎ)
$ 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(已在homebrew中安装)
本地操作步骤 dì
创建项目
如果没有IntelliJ
请在此网址进行同样的选择并进行生成,然后以zip格式下载,在使用此zip文件。
如果有IntelliJ的话
最近的 IntelliJ 若要使用 spring initializer,src 文件夹及以下的目录不会自动创建。
即使已经安装了 IntelliJ,可能最好还是下载上述的 zip 文件并导入进来。
似乎存在某些问题,已经删除了选项。

你好,世界的创造
请修改位于 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);
}
}

由于在依赖项中添加了太多内容,导致程序运行浪费,因此需要进行注释处理。
将 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'
}
请重新执行以下操作:
打开http://localhost:8080,在页面上确认是否显示了”Hello World”。
确认后,使用停止按钮停止。
创建 Dockerfile 文件和 docker-compose.yml 文件
创建一个docker目录,并在其下创建Dockerfile和docker-compose.yml文件。使用docker-compose的原因是因为计划未来在此处建立MySQL和nginx等服务。
FROM openjdk:11
ENV APP_ROOT /app
COPY . $APP_ROOT
WORKDIR $APP_ROOT
ENTRYPOINT ["sh", "./gradlew", "bootRun"]
version: '3'
services:
web:
build:
# Dockerfile の app のコピーのために必要なパス指定
context: ../
# context のパスから見た Dockerfile の場所
dockerfile: docker/Dockerfile
volumes:
# :より左の部分は自分の環境に合わせる
- ~/Dev/java/spring-boot:/app
ports:
- "8080:8080"
启动Docker环境
$ cd docker
$ docker-compose up
确认启动并再次确认 http://localhost:8080 能正常显示。
热部署 (Rè
由于使用了Spring Boot DevTools,HotDeploy得以应用。
尝试更改Application.java中的“Hello World”文本时,
可以在几秒后确认Java已被重新启动(通过docker-compose up的日志)。
刷新浏览器后,可以确认已更改的文本已生效。

在IntelliJ中进行远程调试
IntelliJ的设置

在 build.gradle 文件中追加以下内容。
bootRun {
// for remote debug
jvmArgs "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"
}
docker-compose.yml的更改
因为在远程调试中使用5005端口,故需将其记录下来。
ports:
- "8080:8080"
# for remote debug
- "5005:5005"
启动 Docker
再次运行 docker-compose up,并确保进入待命状态。
web_1 | > Task :bootRun
web_1 | Listening for transport dt_socket at address: 5005
在IntelliJ中,使用之前设置的远程调试来启动调试。

最后
我总结了一个简单的构建方法。