docker-composeでlocalstack + opensearchの環境構築

Amazon OpenSearch Serviceとの連携を行う要件に対して開発を行う場合、クラウド環境との疎通を行う前に、docker開発環境内でlocalstackを用いて検証することができる。

docs.localstack.cloud

設定ファイル

docker-compose.yml

version: '3.7'
services:
  opensearch:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - node.name=opensearch
      - cluster.name=opensearch-docker-cluster
      - discovery.type=single-node
      - bootstrap.memory_lock=true
      - "OPENSEARCH_JAVA_OPTS=-Xms512m -Xmx512m"
      - "DISABLE_SECURITY_PLUGIN=true"
    ports:
      - "9200:9200"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - opensearch:/usr/share/opensearch/data
  localstack:
    image: localstack/localstack:latest
    ports:
      - "4566:4566"
    depends_on:
      - opensearch
    environment:
      - OPENSEARCH_CUSTOM_BACKEND=http://opensearch:9200
      - DEBUG=${DEBUG- }
      - PERSISTENCE=${PERSISTENCE- }
      - DOCKER_HOST=unix:///var/run/docker.sock
      - HOSTNAME_EXTERNAL=localstack
    volumes:
      - ./docker/localstack/:/docker-entrypoint-initaws.d # 起動時に実行する初期化ファイル
volumes:
  opensearch:
    driver: local

Dockerfile

  • command でのインストールは不可なので Dockerfile でインストール
  • opensearch-plugin で日本語全文検索プラグインをインストール
FROM opensearchproject/opensearch:latest
RUN /usr/share/opensearch/bin/opensearch-plugin install analysis-kuromoji
RUN /usr/share/opensearch/bin/opensearch-plugin install analysis-icu

カスタムドメイン作成用シェル

  • ./docker/localstack/init.sh ファイルを作成
    • ./docker/localstack/:/docker-entrypoint-initaws.d の設定で起動時に実行したい内容を記載
  • docker-compose終了時に作成したドメインが消えてしまうため、起動時に毎回ドメインを作成する
awslocal opensearch create-domain \
  --domain-name my-domain \
  --domain-endpoint-options '{ "CustomEndpoint": "http://localstack:4566/my-custom-endpoint", "CustomEndpointEnabled": true }'

起動確認

docker exec -it [localstackコンテナ名] /bin/bash

curl http://localhost:4566/my-custom-endpoint/_cluster/health?pretty
{
  "cluster_name" : "opensearch-docker-cluster",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "discovered_master" : true,
  "discovered_cluster_manager" : true,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}