Home > Software design >  Faraday::ConnectionFailed when using elastic search in rails using docker
Faraday::ConnectionFailed when using elastic search in rails using docker

Time:12-02

I'm trying to make a search function in message model and evrey thing works well without docker but when compile the whole stack using docker it gives me

"status": 500,
"error": "Internal Server Error",
"exception": "#<Faraday::ConnectionFailed wrapped=#<Errno::EADDRNOTAVAIL: Failed to open TCP connection to localhost:9200 (Cannot assign requested address - connect(2) for [::1]:9200)>>",

my dockercompose.yml `

 version: "3.7"

 services:
    app:
      build: .
      command: bundle exec rails s -p 3000 -e development -b '0.0.0.0'
      ports:
        - 3000:3000
      working_dir: /app
      volumes:
        - ./:/app
      environment:
            DB_USER: root
            DB_NAME: insta-api
            DB_PASSWORD: 123456789
            DB_HOST: mysql

    mysql:
      image: mysql:5.7
      volumes:
        - insta-mysql-data:/var/lib/mysql
      environment:
            MYSQL_ROOT_PASSWORD: 123456789
            MYSQL_DATABASE: insta-api

    elasticsearch:
      image: elasticsearch:7.17.6
      hostname: "elasticsearch"
      environment:
          - discovery.type=single-node
      ports:
            - "9200:9200"
      ulimits:
        memlock:
          soft: -1
          hard: -1
        nofile:
          soft: 65536
          hard: 65536
      mem_limit: 1g
      cap_add:
        - IPC_LOCK

      
 volumes:
      insta-mysql-data:

` elasticsearch.rb

#./config/initializers 
config = {
    host: "http://localhost:9200/",
    transport_options: {
      request: { timeout: 5 }
    }
  }
  
  if File.exists?("config/elasticsearch.yml")
    config.merge!(YAML.load_file("config/elasticsearch.yml")[Rails.env].deep_symbolize_keys)
  end
  
  Elasticsearch::Model.client = Elasticsearch::Client.new(config)

.yml

#./config
development: &default
  host: 'http://localhost:9200/'
  transport_options:
    request:
      timeout: !!integer 300
test:
  <<: *default
staging:
  <<: *default
production:
  host: 'http://10.50.11.50/'
  transport_options:
    request:
      timeout: !!integer 300

I'm trying to use docker to run rails mysql and elastic search all stack. i tried too many versions of elastic search and the same error raised.

CodePudding user response:

I solved the problem it was because of the hostname inside elasticsearch.yml and elasticsearch.rb I needed to convert the host to ENV['ELASTIC_HOST'] which I had identified in environmental parameters in docker services.app here's dockercompose file

 version: "3.7"

 services:
    app:
      build: .
      command: bundle exec rails s -p 3000 -e development -b '0.0.0.0'
      ports:
        - 3000:3000
      working_dir: /app
      volumes:
        - ./:/app
      environment:
            DB_USER: root
            DB_NAME: insta-api
            DB_PASSWORD: 123456789
            DB_HOST: mysql
            ELASTIC_HOST: elasticsearch

    mysql:
      image: mysql:5.7
      volumes:
        - insta-mysql-data:/var/lib/mysql
      environment:
            MYSQL_ROOT_PASSWORD: 123456789
            MYSQL_DATABASE: insta-api

    elasticsearch:
      image: elasticsearch:7.17.6
      hostname: "elasticsearch"
      environment:
          - discovery.type=single-node
      ports:
         - 9200:9200 
 volumes:
      insta-mysql-data:

and elasticsearch.rb

config = {
    host: ENV["ELASTIC_HOST"]
}

if File.exists?("config/elasticsearch.yml")
  config.merge!(YAML.load_file("config/elasticsearch.yml")[Rails.env].deep_symbolize_keys)
end

Elasticsearch::Model.client = Elasticsearch::Client.new(config)

and elasticsearch.yml

development: &default
  host: 'elasticsearch'
test:
  <<: *default
production:
  <<: *default

it may be easy but this was my first time working with docker and stuff.

  • Related