LoginSignup
1
0

More than 1 year has passed since last update.

Spring Boot WEBサービスを Azure App Service で起動する (Docker Hub カスタムコンテナイメージ)

Last updated at Posted at 2023-02-27

Spring Boot WEBサービスを Azure App Service で起動する (Docker Hub カスタムコンテナイメージ)

目的

Spring Boot WEBサービスAzure App Service 環境で起動して理解を深めます。

実現すること

Microsoft Azure App Service に Spring Boot WEBアプリのカスタムコンテナイメージをデプロイします。

この記事では、Docker Hub にプュシュしたパブリックアクセスのカスタムコンテナイメージを使用しています。 実際のシステムを構築される際にはご注意ください。

Microsoft Azure Container Registry (ACR) を使用した構成の例として、以下の記事もございます。

技術背景

Microsoft Azure App Service とは?

こちらを展開してご覧いただけます。

Microsoft Azure App Service

Microsoft Azure App Service は、Microsoft Azure の PaaS (Platform as a Service) サービスの一つで、WEB アプリケーションやモバイルアプリケーション、API アプリケーションのホスティングに特化したサービスです。

Azure App Service の主な特徴とメリットは以下のとおりです。

簡単なデプロイ

Azure App Service は、Azure Portal や Azure CLI、Azure DevOps などのツールを使って、WEB アプリケーションを簡単にデプロイできます。また、多数のプログラミング言語やフレームワークに対応しており、柔軟な開発が可能です。

スケーラビリティ

Azure App Service は、自動スケーリングや手動スケーリングをサポートしており、アプリケーションの負荷に応じて柔軟にスケールできます。

高可用性

Azure App Service は、グローバルな負荷分散やフェイルオーバー機能を備えており、高い可用性を実現できます。

統合

Azure App Service は、Azure の他のサービスとシームレスに統合できます。例えば、Azure SQL Database や Azure Storage などのストレージサービスと連携して、アプリケーションのデータ管理を容易にします。

セキュリティ

Azure App Service は、ネットワークセキュリティグループ (NSG) や WEB アプリケーションファイアウォール (WAF) などのセキュリティ機能を提供しており、アプリケーションのセキュリティを高めることができます。

まとめ

これらの特徴とメリットにより、Azure App Service は、開発者や企業にとって、高速かつ柔軟な WEB アプリケーションの開発とデプロイを支援するプラットフォームとして利用されています。

開発環境

  • Windows 11 Home 22H2 を使用しています。
  • WSL の Ubuntu を操作していきますので macOS の方も参考にして頂けます。

WSL (Microsoft Store アプリ版)

> wsl --version
WSL バージョン: 1.0.3.0
カーネル バージョン: 5.15.79.1
WSLg バージョン: 1.0.47

Ubuntu

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.1 LTS
Release:        22.04

Java JDK ※ Java JDK の導入と Hello World!

$ java -version
openjdk version "11.0.17" 2022-10-18
OpenJDK Runtime Environment (build 11.0.17+8-post-Ubuntu-1ubuntu222.04)
OpenJDK 64-Bit Server VM (build 11.0.17+8-post-Ubuntu-1ubuntu222.04, mixed mode, sharing)

Maven ※ Maven の導入と Hello World!

$ mvn -version
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.17, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64

Docker Desktop

Version 4.16.3 (96739)
$ docker --version
Docker version 20.10.22, build 3a2c30b
$ docker-compose --version
Docker Compose version v2.15.1

※ この記事では基本的に Ubuntu のターミナルで操作を行います。

"Hello World" を表示する手順

Spring Boot WEBサービスの作成

こちらを参照して頂けます。

プロジェクトフォルダに移動

※ ~/tmp/hello-spring-boot をプロジェクトフォルダとします。

$ cd ~/tmp/hello-spring-boot

Java アプリをビルド(※参考)

※ target/app.jar が作成されます。
※ 下の コンテナイメージを作成する操作でも同時に target/app.jar が作成されるので必須ではありません。

$ mvn clean install

コンテナイメージを作成

Dockerfile を編集
※ 検証時点では素の spring-boot:build-image では動作せず、明示的に EXPOSE 8080 設定を付加する必要がありました。

$ vim Dockerfile

ファイルの内容

Dockerfile
EXPOSE 8080

コンテナイメージをビルド

※ ローカルの Docker 環境 (Docker Desktop) に app-hello-spring-boot コンテナイメージが作成されます。
※ コンテナイメージの作成時間が Unix エポックになるのは spring-boot:build-image の仕様です。

$ mvn spring-boot:build-image \
    -Dspring-boot.build-image.imageName=app-hello-spring-boot \
    -Dspring-boot.build-image.dockerfile=Dockerfile

コンテナイメージの確認

$ docker images | grep app-hello-spring-boot
app-hello-spring-boot   latest   e37cc77f2b36   43 years ago   262MB

ローカルでコンテナイメージを起動して確認

$ docker run --name app-local -p 8080:8080 app-hello-spring-boot

別ターミナルから curl コマンドで確認

※ 8080 ポートでリッスン出来ているか確認します。

$ curl -v http://localhost:8080/api/data
*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /api/data HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Mon, 27 Feb 2023 02:15:38 GMT
<
* Connection #0 to host localhost left intact
{"message":"Hello World!"}

Docker Hub にコンテナイメージを登録

※ $USER の部分はご自身のコンテナレジストリに読み替えて下さい。

タグ付け

$ docker tag app-hello-spring-boot $USER/app-hello-spring-boot:latest

プュシュ

$ docker push $USER/app-hello-spring-boot:latest

Docker Hub に $USER/app-hello-spring-boot:latest としてカスタムコンテナイメージが公開出来ました。

Azure のアカウントを取得

公式 Azure の無料アカウントを使ってクラウドで構築

Azure CLI でサインイン

こちらを参照して頂けます。

ログイン

$ az login

※ 最新バージョンに更新する場合

$ az upgrade

Azure 環境

リソースグループ

リソースグループを作成

Microsoft.Resources/resourceGroups
$ az group create \
    --name rg-hello \
    --location japaneast

リソースグループ一覧表示

$ az group list

※ リソースグループを削除する場合

$ az group delete -n <group>

App Service プラン

App Service プランを作成

Microsoft.Web/serverfarms
$ az appservice plan create \
    --resource-group rg-hello \
    --name asp-hello \
    --sku Free \
    --is-linux

App Service プランの一覧表示

$ az appservice plan list

※ App Service プランを削除する場合

$ az appservice plan delete -n <plan> -g <group>

App Service WEB アプリ

※ Azure App Service に Spring Boot WEBアプリのコンテナイメージをデプロイする方法はいくつかありますが、今回は Azure CLI で Docker Hub 上のパブリックなカスタムコンテナイメージをデプロイする方法を紹介します。

Web アプリの作成とデプロイ
※ $USER の部分はご自身のコンテナレジストリに読み替えて下さい。

Microsoft.Web/sites
$ az webapp create \
    --resource-group rg-hello \
    --plan asp-hello \
    --name app-hello-spring-boot \
    --deployment-container-image-name $USER/app-hello-spring-boot:latest

Web アプリの作成の設定を構成
※ App Service Web アプリはデフォルトで 80 番ポートをリッスンする仕様です。

$ az webapp config appsettings set \
    --resource-group rg-hello \
    --name app-hello-spring-boot \
    --settings WEBSITES_PORT=8080

Web アプリのログ記録の構成

$ az webapp log config \
    --resource-group rg-hello \
    --name app-hello-spring-boot \
    --docker-container-logging filesystem \
    --application-logging filesystem \
    --web-server-logging filesystem 

WEBブラウザーでアクセス

$ az webapp browse \
    --resource-group rg-hello \
    --name app-hello-spring-boot

※ 立ち上がりに少し時間が掛かりますがページが表示されます。 しかし表示されたページ "/" にはコンテンツを設定していません。

改めてWEBブラウザで確認
※ URLは環境で読み替えて下さい。

https://app-hello-spring-boot.azurewebsites.net/api/data

WEBブラウザに {"message":"Hello World!"} と表示され、JSON データを取得することが出来ました。

※ 別ターミナルから curl コマンドで確認

$ curl -v https://app-hello-spring-boot.azurewebsites.net/api/data
*   Trying 20.43.67.36:443...
* Connected to app-hello-spring-boot.azurewebsites.net (20.43.67.36) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
*  CAfile: /etc/ssl/certs/ca-certificates.crt
*  CApath: /etc/ssl/certs
* TLSv1.0 (OUT), TLS header, Certificate Status (22):
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS header, Finished (20):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS header, Finished (20):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: C=US; ST=WA; L=Redmond; O=Microsoft Corporation; CN=*.azurewebsites.net
*  start date: Dec 27 21:12:39 2022 GMT
*  expire date: Dec 22 21:12:39 2023 GMT
*  subjectAltName: host "app-hello-spring-boot.azurewebsites.net" matched cert's "*.azurewebsites.net"
*  issuer: C=US; O=Microsoft Corporation; CN=Microsoft Azure TLS Issuing CA 05
*  SSL certificate verify ok.
* Using HTTP2, server supports multiplexing
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* Using Stream ID: 1 (easy handle 0x55b794ea9e80)
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
> GET /api/data HTTP/2
> Host: app-hello-spring-boot.azurewebsites.net
> user-agent: curl/7.81.0
> accept: */*
>
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
< HTTP/2 200
< content-type: application/json
< date: Mon, 27 Feb 2023 02:51:05 GMT
< set-cookie: ARRAffinity=ae907ce840c6b95b786870ca4c1362dc92f4733f1b7145fe24a6c8a07a0d5ff4;Path=/;HttpOnly;Secure;Domain=app-hello-spring-boot.azurewebsites.net
< set-cookie: ARRAffinitySameSite=ae907ce840c6b95b786870ca4c1362dc92f4733f1b7145fe24a6c8a07a0d5ff4;Path=/;HttpOnly;SameSite=None;Secure;Domain=app-hello-spring-boot.azurewebsites.net
<
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* Connection #0 to host app-hello-spring-boot.azurewebsites.net left intact
{"message":"Hello World!"}

ターミナルに {"message":"Hello World!"} と表示され、JSON データを取得することが出来ました。

※ Azure 側で SSL/TLS対応と HTTP/2プロトコルを使用してくれます。

まとめ

参考

[Microsoft Learn] Azure App Service コマンド リファレンス

1
0
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
1
0