0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Spring Boot × Docker】WSL環境でWebサービスを構築するサンプル手順とコード

Posted at

【Spring Boot × Docker】WSL環境でWebサービスを構築するサンプル手順とコード

はじめに

Windows11 + WSL Ubuntu 24.04 上で、Spring Boot アプリケーションを Docker コンテナで起動してみた手順をまとめました。

Spring Boot アプリは公式チュートリアル「Spring Boot with Docker」を参考に作成しています。


環境

項目 バージョン
OS Windows 11
WSL Ubuntu 24.04
Java OpenJDK 17
Spring Boot 3.x(Spring Initializr)
Docker 24.x(WSL 経由)

1. プロジェクト作成

Spring Initializr または curl でテンプレートを作成します。

curl https://start.spring.io/starter.tgz \
  -d dependencies=web \
  -d name=demo \
  -d packageName=hello \
  | tar -xzvf -

ディレクトリ構成(一部)

demo/
├── build.gradle
├── src/
│   └── main/
│       └── java/
│           └── hello/
│               └── Application.java

2. Javaファイルの中身

// src/main/java/hello/Application.java
package hello;

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;

@SpringBootApplication
@RestController
public class Application {

  @RequestMapping("/")
  public String home() {
    return "Hello Docker World";
  }

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

3. Dockerfile の作成

プロジェクトルートに Dockerfile を作成:

# ベースイメージ
FROM eclipse-temurin:17-jdk-jammy

# 作業ディレクトリ作成
WORKDIR /app

# Jar ファイルをコピー(ビルド後のもの)
COPY target/*.jar app.jar

# ポート開放
EXPOSE 8080

# アプリケーション起動
ENTRYPOINT ["java", "-jar", "app.jar"]

4. build.gradle の設定

build.gradle に以下のように記述します(Maven の場合は pom.xml)。

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.2.0'
    id 'io.spring.dependency-management' version '1.1.0'
}

group = 'hello'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

bootJar {
    archiveFileName = 'app.jar'
}

5. アプリのビルドと Docker イメージ作成

./gradlew bootJar
docker build -t springboot-docker-demo .

6. コンテナ起動と動作確認

docker run -p 8080:8080 springboot-docker-demo

以下にアクセス:

http://localhost:8080

表示:

Hello Docker World

7. WSL 環境での注意点

  • Docker Desktop をインストールし、「WSL2 統合」を有効にしておく。
  • docker コマンドは WSL 上から直接使用可能。
  • ブラウザは Windows 側のものでOK(localhostは共通)。

おわりに

WSL環境でも、DockerとSpring Bootを使えばローカルで簡単にWebサービスを構築できます。

今後は docker-compose でのDB連携や、ホットリロード対応の開発環境構築なども検討していきたいです。


0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?