プライベートなコンテナイメージを Amazon Lightsailで使う
前提
・ローカルにプライベートなDockerイメージが作成済であること ・AWS CLIがインストールされていること ・lightsailctl プラグインがインストールされていること
手順
IAMを作成する
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "lightsail:CreateContainerServiceRegistryLogin", "lightsail:RegisterContainerImage" ], "Resource": "*" } ] }
AWSコンソールからLigthsailのコンテナを作成する
プライベートなコンテナイメージを動作させるためのコンテナを事前に作成しておく必要がある ここではあらかじめ用意されているサンプル(helloworld)イメージで作成
ローカルPCからプライベートなコンテナイメージをプッシュ
例) aws lightsail push-container-image --region <Region> --service-name <ContainerServiceName> --label <ContainerImageLabel> --image <LocalContainerImageName>:<ImageTag>
C:\>aws lightsail push-container-image --region ap-northeast-1 --service-name container-service-1 --label testlabel --image vue-lightsail-sample:latest WARNING: You are using version 1.0.0 of lightsailctl, but version is available. To download, visit https://lightsail.aws.amazon.com/ls/docs/en_us/articles/amazon-lightsail-install-software [1B91e2ecca: Preparing [1B05b757d2: Preparing [1B697b6f38: Preparing [1Bd149735d: Preparing [1B3049c5e5: Preparing [1B846bc438: Preparing [1B8b32331d: Preparing [1B00332834: Preparing [1Bfd1e3667: Preparing [1B157cbca7: Preparing [7BDigest: sha256:12888b3911787a225d01127a4faa83c50d6d9e2e1eda8256c0c900a6851ccf52B/216.4MB Image "vue-lightsail-sample:latest" registered. Refer to this image as ":container-service-1.testlabel.1" in deployments.
プッシュに成功すると、Lightsail コンソールのImagesタブにプッシュしたイメージが表示される
デプロイ
Lightsail コンソールのDeploymentsタブからModify your deploymentをクリックし、Choose stored imageからプッシュしたイメージを選択
※コンテナで動作するアプリケーションのポートに合わせてポートを指定しないとエラーが発生した
参考
プライベートなコンテナイメージを Amazon Lightsail コンテナサービスで使う | DevelopersIO
Docker for Windows で作成したコンテナは、プライベートなので Amazon Lightsail コンテナにデプロイできないから、対応した記録 - Qiita
AWS CLI & lightsailctl プラグインのインストール
AWS CLI のインストール
下記のコマンドを実行してインストール
C:\>msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi
確認
C:\>aws --version aws-cli/2.6.3 Python/3.9.11 Windows/10 exe/AMD64 prompt/off
lightsailctl プラグインのインストール
下記のファイルをダウンロードしてPATHを通す
https://s3.us-west-2.amazonaws.com/lightsailctl/latest/windows-amd64/lightsailctl.exe
Springのテストで使用するアノテーション
Springのテストで使用するアノテーションを整理する。
@ContextConfiguration (org.springframework.test.context.ContextConfiguration)
Spring Frameworkのテストで使用するアノテーション。 テストクラスと同じパッケージ上に存在する「テストクラス名-context.xml」が読み込まれる。 命名ルールに沿わない場合はlocationで指定する。
@ExtendWith({ SpringExtension.class }) @ContextConfiguration(locations = {"/test-applicationContext.xml", "/test-applicationContext-dao.xml"}) public class AbstractTestCase {
@SpringBootTest (org.springframework.boot.test.context.SpringBootTest)
テスト実行時のコンフィグレーションクラスを自動検出する。 SpringBootを使わない場合は、上記の@ContextConfiguration でコンフィグレーションを明示する必要があるがそれが不要になる。
自動検出の対象は@SpringBootConfigurationが付与されたクラスで、@SpringBootApplicationが付与されたクラスも対象となる。 (@SpringBootApplicationに@SpringBootConfigurationが付与されているため)
自動検出の対象から除外する場合
自動検出の弊害として、特定のテスト用に作成したコンフィグレーションが意図せず読み込まれることがある。 そのようなことを防ぎたい場合は、@Configurationの代わりに@TestConfiguration (後述)を使用することで自動検出の対象から除外される。
@TestConfiguration (org.springframework.boot.test.context.TestConfiguration)
自動検出の対象外としたいコンフィグレーションクラスをマークするために使用する。
//@Configuration @TestConfiguration public class HogeTestConfiguration { @Bean public HogeRepository oracleHogeRepository() { return new OracleHogeRepository(); } }
@ExtendWith (org.junit.jupiter.api.extension.ExtendWith)
JUnit4で使用されていた@RunWithに置き換わるもので、@RunWithと違い複数の拡張クラスを指定できるように変更されている。
@TestExecutionListeners (org.springframework.test.context.TestExecutionListeners)
指定したListenerをTestContextManagerに設定することで、下記のような様々な機能を利用できる。
クラス | 用途 |
---|---|
ServletTestExecutionListener | モックサーブレットAPIを設定する機能を提供 |
DirtiesContextBeforeModesTestExecutionListener | テストで使用するDIコンテナのライフサイクル管理機能を提供 |
DependencyInjectionTestExecutionLiLstener | テストで使用するインスタンスへDI機能を提供 |
DirtiesContextTestExecutionListener | テストで使用するDIコンテナのライフサイクル管理機能を提供 |
TransactionalTestExecutionListener | テスト実行時のトランザクション機能を提供 |
SqlScriptsTestExecutionListener | @Sqlアノテーションで指定されているSQLを実行する |
Relaxed Bindingについて
Circuit Breakerについて調べていた時、サイトによって設定ファイル(application.yml)に定義するプロパティがキャメルケースだったり、ケバブケースだったりどちらが正しいんだろって疑問を持ってたんだけど、どっちも正しかった。
Relaxed Binding docs.spring.io
Redisinsightに触る
RedisinsightはGUIでRedisを操作できるWebアプリケーション
操作はブラウザから行う
セットアップ
docker-compose.yml
version: '3.8' services: redis: container_name: redis image: "redis:latest" ports: - "16379:6379" volumes: - "./redis/data:/data" redisinsight: container_name: phptips_redisinsight image: redislabs/redisinsight:latest ports: - 18001:8001 volumes: - ./redis/insight:/db
実行
docker-compose up -d
ブラウザからアクセス
下記へアクセス
http://localhost:18001/
接続先を登録
dockerコンテナ上で動作しているRedisを接続先に登録する
管理コンソール
POST処理を非同期で実行する
FormでSubmitされたリクエストを非同期に切り替えるサンプル
$(function () { $('#userForm').submit(function (event) { // HTMLでの送信をキャンセル event.preventDefault(); // 操作対象のフォーム要素を取得 var $form = $(this); $("#overlay").fadeIn(300); // 送信 $.ajax({ url: $form.attr('action'), type: $form.attr('method'), dataType: "html", data: $form.serialize(), // 通信成功時の処理 success: function (result, textStatus, xhr) { //alert(result); $("#embeddedContainer").html(result); $("#overlay").fadeOut(300); }, // 通信失敗時の処理 error: function (xhr, textStatus, error) { alert('NG...'); $("#overlay").fadeOut(300); } }); }); });