JetBrains が Spring と 戦略的提携 して DevEx の向上させるらしいので、Spring Boot 4.0.0-M1 (プレビュー) リリースで リアクティブ Web アプリケーション の開発環境を IntelliJ IDEA と Dev Container で構築する
今回の環境
TL;DR
1. 新規プロジェクトの作成
Spring Boot 4.0.0-M1, Kotlin を選択
GraalVM Native Support, Spring Boot DevTools, Spring Reactive Web, Spring Data R2DBC, Spring PostgreSQL Driver, Spring Boot Actuator を追加
2. .devcontainer の追加
// .devcontainer/devcontainer.json
// Default Ubuntu-based DevContainer Template.
// For more customization options, see https://containers.dev/implementors/json_reference
{
- name: "Default",
+ name: "spring-boot-demo",
image: "ubuntu:latest",
customizations : {
// Configure JetBrains IDE-specific properties
jetbrains : {
backend : "IntelliJ",
settings : {
// IDE settings can be added via “Show context actions”|“Add currently modified settings from IDE”.
// A complete list of supported settings is also available through auto-completion
},
plugins: [
// A set of plugin IDs.
// Plugin ID can be found on the corresponding plugin’s page at https://plugins.jetbrains.com.
]
}
},
features: {
"ghcr.io/devcontainers/features/git" : {},
// Add additional features to your project using auto-completion.
+ "ghcr.io/devcontainers/features/docker-outside-of-docker:1": {},
+ "ghcr.io/devcontainers/features/java:1": {
+ "version": "23.1.8.r21",
+ "jdkDistro": "nik"
+ }
},
// Comment out to connect as the root user.
remoteUser: "ubuntu"
}
3. postgres の設定
// .devcontainer/devcontainer.env
+ POSTGRES_PASSWORD=postgres
+ POSTGRES_USER=postgres
+ POSTGRES_DB=postgres
+ POSTGRES_HOSTNAME=host.docker.internal
+
# .devcontainer/compose.yaml
+ services:
+ app:
+ container_name: javadev
+ image: ubuntu:latest
+ env_file: devcontainer.env
+ # NOTE: POSTGRES_DB/USER/PASSWORD should match values in db container
+
+ volumes:
+ - ../..:/workspaces:cached
+
+ # Overrides default command so things don't shut down after the process ends.
+ command: sleep infinity
+
+ db:
+ container_name: postgresdb
+ image: postgres:latest
+ restart: unless-stopped
+ volumes:
+ - postgres-data:/var/lib/postgresql/data
+ env_file: devcontainer.env
+ # NOTE: POSTGRES_DB/USER/PASSWORD should match values in app container
+
+ # Runs db on the same network as the app container, allows "forwardPorts" in devcontainer.json function.
+ network_mode: service:app
+
+ # Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
+ # (Adding the "ports" property to this file will not forward from a Codespace.)
+
+ volumes:
+ postgres-data:
+
// .devcontainer/devcontainer.json
// Default Ubuntu-based DevContainer Template.
// For more customization options, see https://containers.dev/implementors/json_reference
{
name: "spring-boot-demo",
- image: "ubuntu:latest",
+ dockerComposeFile: "compose.yaml",
+ service: "app",
+
+ // Use 'forwardPorts' to make a list of ports inside the container available locally.
+ // This can be used to network with other containers or with the host.
+ "forwardPorts": [5432],
...
4. Dev Conainer で開く
5. Debug and Health Check
6. Hello World
// ./src/main/kotlin/com/example/demo/controller/HelloController.kt
+ package com.example.demo.controller
+
+ import org.springframework.web.bind.annotation.GetMapping
+ import org.springframework.web.bind.annotation.RestController
+
+ @RestController
+ class HelloController {
+
+ @GetMapping("/hello")
+ fun handle() = "Hello WebFlux"
+ }
+