0
0

Azure Container Apps に war ファイルを直接デプロイする

Posted at

Azure Container Apps に war ファイルを直接デプロイする

ちょっと前に、 war ファイルを直接 Azure Container Apps にデプロイできるような機能が追加された話をTwitterで見つけたので、備忘録代わりに試してみました。

MS Learn にも記事があるので、そちらも参考にしてください。

Azure Container Apps 内の Tomcat に WAR ファイルをデプロイする | Microsoft Learn

事前準備

以下で適当な Spring Boot のプロジェクトのひな形を作成し、

Spring Initializr

適当に Hello World を返すだけの エンドポイントを追加しておきます。

@RestController
public class HelloController {

    @GetMapping("/")
    public String hello() {
        return "Hello, Azure Container Apps! " + java.time.LocalDateTime.now().toString();
    }
}

プロジェクトをビルドしますが、念のためローカルでも実行しておきましょう。

$ mvn clean package spring-boot:run -DskipTests
$ curl http://localhost:8080
Hello, Azure Container Apps! 2024-04-10T11:52:14.595400093

target ディレクトリに war ファイルが生成されていると思いますが、demo.wardemo.war.original の2つが生成されすが、前者は Spring boot が生成する単独で実行可能な war ファイルです(Tomcatなどが組み込まれています)今回、Azure Container Apps にデプロイするのは、後者の demo.war.original ですので、こちらを使います。前者をデプロイして動かなくてしばらくハマりました。

Azure Container Apps にデプロイする

適当にリソースグループを作成しておきます。

az group create -g testcontainerapp -l japaneast

次に以下のコマンドで、Azure Container Appsに一気に war ファイルをデプロイします。

$ az containerapp up \
  --name testwarapp \
  --resource-group testcontainerapp \
  --subscription ${SUBSCRIPTION_ID} \
  --location japaneast \
  --environment testwarappenv \
  --artifact demo.war \
  --build-env-var BP_TOMCAT_VERSION="10.*" \
  --ingress external \
  --target-port 8080 \
  --query properties.configuration.ingress.fqdn

...
...


  Preparing the Container Apps Cloud Build environment

    () Done: Listing the builders available in the Container Apps environment (0.0s)
              No builder available in environment testwarappenv
    () Done: Creating the builder in the Container Apps environment (101.0s)
              Builder created: builder557b2

  Building the application

    () Done: Starting the Container Apps Cloud Build agent (0.5s)
              Cloud Build agent started: build557b2b6
    () Done: Retrieving the authentication token (0.3s)
    () Done: Uploading data (3.3s)
    () Done: Waiting for the Cloud Build agent to report status (2.7s)
    () Done: Streaming Cloud Build logs (9.5s)
    () Done: Buildpack: Initializing (0.3s)
    () Done: Buildpack: Executing the detect phase (2.9s)
    () Done: Buildpack: Executing the restore phase (0.8s)
    () Done: Buildpack: Executing the extend phase (4.4s)
    () Done: Buildpack: Executing the export phase (15.9s)
              Successfully built and containerized image: default/build557b2b6
              Full logs: /tmp/build557b2b6.txt

挙動の説明は後回しにして、 エンドポイントを叩いて応答があればデプロイ成功です。

$ curl https://testwarapp.jollymushroom-xxxxxxxxx.japaneast.azurecontainerapps.io
Hello, Azure Container Apps! 2024-04-10%

作成されるリソースなど

az コマンドで一気にデプロイすると、初回に必要なリソースは適宜作成してくれます。作成してくれるリソースは以下

  • Container Apps 環境
  • コンテナーアプリ(そのもの)
  • Log Analytics ワークスペース

および、非表示のリソースとして以下が作成されてます(ポータルから非表示のリソースを表示すると見えます)

  • microsoft.app/builders

このリソースは何ぞや?ということで Github Copilot に聞いてみましょう。

この `microsoft.app/buildersはAzure Container Appsの一部で、アプリケーションのビルドプロセスを管理します。具体的には、ソースコードからコンテナイメージをビルドするための環境を提供します。このリソースは、Cloud Native Buildpacksというテクノロジーを使用して、ソースコードをコンテナイメージに変換します。Buildpacksは、ランタイム環境(例えばJava、Node.js、Pythonなど)を自動的に検出し、適切な依存関係をインストールし、アプリケーションをビルドして実行可能なコンテナイメージを作成します。microsoft.app/buildersリソースは通常、ユーザーが直接操作するものではなく、Azure Container Appsのデプロイメントプロセスの一部として自動的に作成および管理されます。

とのことです。通常は自分でコンテナイメージ作り、コンテナレジストリに登録し、それをデプロイするという流れが一般的ですが、このデプロイ方法では、この microsoft.app/builders がその一部を代行してくれるようです。

また、ログから分かるように、 Buildpack を使ってコンテナイメージを作成しているようです(Buildpack は使ったことがないので、なにも分かりません)さらに詳細なログは、/tmp に吐き出されるので、そちらを見ると、どのようなビルドプロセスが行われているかが分かります。

もう少し細かい構成設定など

BP_ で構成設定を行うことができます。例えば、BP_TOMCAT_VERSION で Tomcat のバージョンを指定することができます。他にも、BP_JAVA_VERSION などがあります。

詳細は以下のリンクを参照してください。

Azure Container Apps で Java 用の環境変数を構築する | Microsoft Learn

雑感

コンテナイメージ作成など、面倒臭い作業を自動化してくれるのはありがたいのですが、どのようなイメージが使われているのかとか、どんなビルドプロセスになっているかがあまり見えないのが、ちょっと不安です(多分調べれば分かると思いますが)

自前でビルドしてイメージ作ったり、ACR作ったりしないのは楽なのですが。時間があったらもう少し深掘りしたい感じです。

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