Opensearch
AWS Opensearch Search

Setup AWS OpenSearch for Docker with LocalStack

OpenSearch is a distributed, open-source search and analytics suite used for a broad set of use cases like real-time application monitoring, log analytics, and website search.

To use AWS OpenSearch Service, we will require LocalStack installed in our Local Development Environment.

LocalStack is a cloud service emulator that runs in a single container on our local development environment. With LocalStack, you can run your AWS services such as SQS, S3, etc entirely on our local dev environment without connecting to a remote service.

Following is an example of a basic LocalStack setup for your docker-compose yaml file

    localstack:
        hostname: localstack
        container_name: '${APP_NAME}-localstack'
        image: localstack/localstack
        ports:
            - "127.0.0.1:3510-3559:4510-4559"  # External service port range
            - "127.0.0.1:4567:4566"            # LocalStack Edge Proxy
        environment:
            - AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION}
            - DEBUG=${DEBUG-}
            - DATA_DIR=${DATA_DIR-}
            - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR-}
            - OPENSEARCH_ENDPOINT_STRATEGY=path
            - HOST_TMP_FOLDER=${TMPDIR:-/tmp/}localstack
            - DOCKER_HOST=unix:///var/run/docker.sock
        volumes:
            - 'volume-localstack:/var/lib/localstack'
            - '/var/run/docker.sock:/var/run/docker.sock'
            - ./docker/localstack:/docker-entrypoint-initaws.d/
        networks:
            - app-network

Now to add Opensearch we need to use the following Docker image

opensearchproject/opensearch:latest

Here is an example of Opensearch Container for docker-compose yaml file

opensearch:
    container_name: '${APP_NAME}-opensearch'
    image: 'opensearchproject/opensearch'
    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:
        - 'volume-opensearch:/usr/share/opensearch/data'
    networks:
        - app-network

The following is a complete Docker compose file

version: '3'
services:
...
-- Other container definitions --
...

    opensearch:
        container_name: '${APP_NAME}-opensearch'
        image: 'opensearchproject/opensearch'
        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:
            - 'volume-opensearch:/usr/share/opensearch/data'
        networks:
            - app-network

    localstack:
        hostname: localstack
        container_name: '${APP_NAME}-localstack'
        image: localstack/localstack
        ports:
            - "127.0.0.1:3510-3559:4510-4559"  # External service port range
            - "127.0.0.1:4567:4566"            # LocalStack Edge Proxy
        environment:
            - AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION}
            - DEBUG=${DEBUG-}
            - DATA_DIR=${DATA_DIR-}
            - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR-}
            - OPENSEARCH_ENDPOINT_STRATEGY=path
            - HOST_TMP_FOLDER=${TMPDIR:-/tmp/}localstack
            - DOCKER_HOST=unix:///var/run/docker.sock
        volumes:
            - 'volume-localstack:/var/lib/localstack'
            - '/var/run/docker.sock:/var/run/docker.sock'
            - ./docker/localstack:/docker-entrypoint-initaws.d/
        networks:
            - app-network

networks:
  app-network:
    driver: bridge
volumes:
  volume-localstack:
    driver: local
  volume-opensearch:
    driver: local

Once you have updated your docker-compose use the following to build the containers

docker compose build
docker compose up -d

Now you will be able to access the Opensearch using the following:-

http://localhost:9200

To start using OpenSearch, First we need to create a domain name using the awslocal CLI:

awslocal opensearch create-domain --domain-name app-search-domain

To view the created domain, you can use the following command:-

awslocal opensearch describe-domain --domain-name app-search-domain

From the describe-domain, you will be able to retrieve the domain URL. You can either use the domain url or the Opensearch direct URL from the Opensearch constainer:-

curl localhost:9200/_cluster/health | jq

The above will output insights into the cluster’s health and version information.

Finally create an index using the following command:

curl -X PUT localhost:9200/app-search-index

Now you have a complete setup for local OpenSearch development.

Spread the love
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments