Getting started

Hi,

Please go easy on me here, My experience with ElasticSearch is pretty much solely with use of SearchKick and my own Rails app.

I have a Rails app which I have been running locally on my machine using SearchKick and ElasticSearch for quite some time now.

Recently I’ve been reading about how to convert my dev environment into a Dockerised form.

I have got my Rails app and it’s database working nicely but then I came to add Search back in and I noticed that OpenSearch has sort of replaced ElasticSearch (Yes yes, I know that’s not 100% correct but i’d prefer to use something open source if possible) so I thought i’d install that instead of ElasticSearch.

My Docker Compose file is below:

version: '3.7'
services:
    app:
        build: .
        command: bundle exec rails s -p 3000 -b '0.0.0.0'
        volumes:
            - .:/app
            - bundle-volume:/usr/local/bundle
            - yarn-volume:/app/node_modules
        ports:
            - "3000:3000"
        depends_on:
            - db
            - search
    db:
        image: 'postgres:9.6'
        env_file:
            - .env.development
        volumes:
            - db-volume:/var/lib/postgresql/data
        ports:
            - "5432:5432"
        # environment:
        #     - POSTGRES_HOST_AUTH_METHOD=trust
    search:
        image: 'opensearchproject/opensearch'
        env_file:
            - .env.development
        volumes:
            - 'search-volume:/usr/share/opensearch/data'
        ports:
            - '9200:9200'
volumes:
    bundle-volume:
    yarn-volume:
    db-volume:
    search-volume:

When I start the container and head to the Rails console to try and run <Model>.reindex I get the following error:

Traceback (most recent call last):
1: from (irb):1
Faraday::ConnectionFailed (Failed to open TCP connection to localhost:9200 (Cannot assign requested address - connect(2) for “localhost” port 9200))

I have set an Environment var like so in my .env.development file:

OPENSEARCH_URL=http://search:9200

Any ideas as to how I can get this working?

Thanks,
Neil

First thing that jumps out at me is the security layer. OpenSearch defaults to TLS (e.g. https) and requires a username and password (default is admin/admin, but you should change this :wink: )

I just tried changing the URL to https but it made no difference.

The error states it’s trying to connect to localhost but I thought when using Docker Compose localhost won;t work and it needs to use the name of the service, in this case “search”.

It looks as though the URL is not working.

Is the name of the environment variable “OPENSEARCH_URL=https://search:9200” correct?

I can’t confidently answer the question about Docker Compose, I’m not an expert. However, comparing your yaml to the opensearch docker compose sample file, I’m not seeing networks defined.

I should note also that your connection string should be https://<ip>:9200/ and you have to use a username/password for basic auth.

From everything i’ve seen, I don’t think i need networks setup. Docker uses the names of each service for networking, in my case, using http://search:9200 should be enough for my app container to find the search container.

The error shows it’s trying to connect to localhost which is exactly where I think the issue lays.

It doesn’t look like the variable i’m setting in my .env.development file is being used in the way it should be.

Anyone with an indepth knowledge of Docker able to help me out here?

Surely someone who works on OpenSearch should know?