Buildpacksを使用したSpring BootアプリケーションのDockerイメージの作成
Sep 08, 2024 pm 08:31 PM導入
Spring Boot アプリケーションが作成されました。ローカル マシンでは問題なく動作していますが、今度はアプリケーションを別の場所にデプロイする必要があります。一部のプラットフォームでは、jar ファイルを直接送信すると、それがデプロイされます。場所によっては、仮想マシンを起動し、そこにソース コードをダウンロードしてビルドし、実行することができます。ただし、ほとんどの場合、コンテナーを使用してアプリケーションをデプロイする必要があります。ほとんどの場合、Docker はコンテナー内でイメージを構築して実行するために使用されます。また、jar ファイルを一部のプラットフォームにアップロードすると、アプリケーションは内部のコンテナ内で実行されます。
このブログでは、特定の Spring Boot アプリケーション用の Docker イメージを構築する 3 つの異なる方法を見ていきます。始めましょう:
基本的なコンテナイメージ
任意のアプリケーションの Docker イメージを構築する単純かつ不十分な方法は、イメージ内の jar ファイルをコピーし、java -jar コマンドを使用して実行する単純な Dockerfile を使用することです。
Dockerfileの作成
プロジェクトのルートに配置できる Dockerfile は次のとおりです。
FROM eclipse-temurin:21-jre-ubi9-minimal ARG JAR_FILE COPY ${JAR_FILE} application.jar ENTRYPOINT ["java", "-jar", "/application.jar"]
使用する jar ファイルの場所である 1 つの引数 JAR_FILE を指定しました。
Docker イメージの構築
上記の Dockerfile を作成した後、以下の手順を使用して Docker イメージを作成します。
-
Spring Boot プロジェクトの jar ファイルをビルドします:
./gradlew bootJar # For Gradle build system
ログイン後にコピーまたは
./mvnw spring-boot:build-jar # For Maven build system
ログイン後にコピー -
Dockerfile を使用して、最新の jar ファイルを使用して Docker イメージを構築します。以下のコマンドでは、{IMAGE_NAME} を必要なイメージ名に置き換え、{JAR_FILE} を生成された jar ファイルへのパスに置き換えます。イメージ名には、mycompany/product-service:0.0.1-SNAPSHOT:
のようなタグも含まれています。docker build --build-arg JAR_FILE={JAR_FILE} --tag {IMAGE_NAME} .
ログイン後にコピー -
次のコマンドを使用して、Docker イメージがビルドされたかどうかを確認します。上記のコマンドで指定した名前の画像が表示されるはずです:
docker images
ログイン後にコピー
レイヤード Jar を使用した効率的なコンテナ イメージ
Spring Boot uber jar を Docker イメージとしてパッケージ化することは可能かつ簡単ですが (前の方法で説明したように)、Docker イメージ内で Fat jar をそのままコピーして実行することには多くの欠点があります。たとえば、
- 解凍せずに uber jar を実行すると、余分なオーバーヘッドが発生します。
- アプリケーションのコードとそのすべての依存関係を単一のレイヤーに配置することは最適ではありません。
Spring Boot バージョンをアップグレードするよりもコードをコンパイルする頻度が高いため、処理をもう少し分離することをお勧めします。これらの jar ファイル (めったに変更されない) をアプリケーション層の前の層に配置すると、Docker は多くの場合、最下層のみを変更する必要があり、残りはキャッシュから選択できます。
階層化された Jar を有効にする
階層化された Docker イメージを作成するには、まず階層化された jar を作成する必要があります。現在、Gradle と Maven ではデフォルトで有効になっています。次の設定を使用して、階層化された jar の動作を有効または無効にできます:
// build.gradle tasks.named("bootJar") { layered { enabled = false } }
// build.gradle.kts tasks.named<BootJar>("bootJar") { layered { enabled.set(false) } }
<!-- pom.xml --> <project> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <layers> <enabled>true</enabled> </layers> </configuration> </plugin> </plugins> </build> </project>
レイヤーの作成方法を調整することもできます。 Gradle または Maven の構成についてはドキュメントを参照してください。
Dockerfileの作成
以下は Dockerfile です。これは、階層化された jar を利用し、Spring Boot アプリケーションの階層化された Docker イメージを作成するために使用できます。
# Perform the extraction in a separate builder container FROM eclipse-temurin:21-jre-ubi9-minimal AS builder WORKDIR /builder # This points to the built jar file in the target folder # Adjust this to 'build/libs/*.jar' if you're using Gradle ARG JAR_FILE=target/*.jar # Copy the jar file to the working directory and rename it to application.jar COPY ${JAR_FILE} application.jar # Extract the jar file using an efficient layout RUN java -Djarmode=tools -jar application.jar extract --layers --destination extracted # This is the runtime container FROM eclipse-temurin:21-jre-ubi9-minimal WORKDIR /application # Copy the extracted jar contents from the builder container into the working directory in the runtime container # Every copy step creates a new docker layer # This allows docker to only pull the changes it really needs COPY --from=builder /builder/extracted/dependencies/ ./ COPY --from=builder /builder/extracted/spring-boot-loader/ ./ COPY --from=builder /builder/extracted/snapshot-dependencies/ ./ COPY --from=builder /builder/extracted/application/ ./ # Start the application jar - this is not the uber jar used by the builder # This jar only contains application code and references to the extracted jar files # This layout is efficient to start up and CDS friendly ENTRYPOINT ["java", "-jar", "application.jar"]
Docker イメージの構築
階層化された Docker イメージを構築する手順は、基本的な Docker イメージを構築する手順と同じです。そちらをご参照ください。
クラウドネイティブビルドパック
Dockerfile を作成せずに Docker イメージを作成できると言ったらどうなるでしょうか? Cloud Native Buildpack を使用して、Gralde または Maven プラグインから直接 Docker イメージをビルドできます。一部のプラットフォーム (Heraku や Cloud Foundry など) は、Buildpack を使用して、提供された jar ファイルを実行可能なイメージに変換します。
Spring Boot には、Maven および Gradle に対する直接のビルドパック サポートが含まれています。追加のプラグインを含める必要はありません。以下のコマンドを実行するだけです:
./gradlew bootBuildImage # For gradle build system
または
./mvnw spring-boot:build-image # For maven build system
上記のコマンドは、デフォルト名 {PROJECT_NAME}:${PROJECT_VERSION} のイメージを生成します。生成されたイメージの名前を構成したい場合は、以下の手順に従ってください:
Gradle ビルド システムのイメージ名を構成する
次のように、bootBuildImage タスクを構成してイメージの名前を設定できます。
// For build.gradle.kts val imagePrefix = "javarush" val dockerImageName = "docker-example" tasks.named<BootBuildImage>("bootBuildImage") { imageName.set("${imagePrefix}/${dockerImageName}:${version}") }
// For build.gradle def imagePrefix = "javarush" def dockerImageName = "docker-example" tasks.named("bootBuildImage") { imageName = "${imagePrefix}/${dockerImageName}:${version}" }
Maven ビルド システムのイメージ名の構成
次のように、別のイメージ名を使用するように spring-boot-maven-plugin を設定できます。
<properties> <imagePrefix>javarush</imagePrefix> </properties> ... <project> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <image> <name>${imagePrefix}/${project.artifactId}:${project.version}</name> </image> </configuration> </plugin> </plugins> </build> </project>
コマンドの実行中にイメージ名を構成します
イメージを構築するコマンドの実行中にイメージの名前を定義することもできます。
./gradlew bootBuildImage --imageName=javarush/docker-example:1.0.0 # For grade build system ./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=javarush/docker-example:1.0.0 # For maven build system
You can see the documentation to further configure Gradle or Maven plugin.
This is my go-to method to create a Docker image for any Spring Boot application.
Running Docker Container
Once you create a docker image, you need to make sure that it works as expected. After you make sure that the image is created, you can directly run it using the docker run command. For example,
docker run -p "8080:8080" {IMAGE_NAME}
But, this is not how images are used in production applications. Docker Compose is used to run and manage multiple docker images.
Conclusion
In this blog, we have seen how to build Docker images for Spring Boot applications using different methods. Being able to build docker images for your apps is a must skill to know because the image is what gets delivered. Thanks for reading the article till the end. I appreciate it. I will meet you in the next one. As always, all feedback and suggestions are welcome.
以上がBuildpacksを使用したSpring BootアプリケーションのDockerイメージの作成の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

人気の記事

人気の記事

ホットな記事タグ

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











2025年のトップ4 JavaScriptフレームワーク:React、Angular、Vue、Svelte

Javaのクラスロードメカニズムは、さまざまなクラスローダーやその委任モデルを含むどのように機能しますか?

高度なJavaプロジェクト管理、自動化の構築、依存関係の解像度にMavenまたはGradleを使用するにはどうすればよいですか?

カフェインやグアバキャッシュなどのライブラリを使用して、Javaアプリケーションにマルチレベルキャッシュを実装するにはどうすればよいですか?

Spring Boot Snakeyaml 2.0 CVE-2022-1471問題修正
