1

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.

Muhammad
  • 11
  • 3
  • I can see you have added one more whitespace in ports . can you please correct the indetation and try it again? and please remove the double quotes from ports value – Vishal Nov 28 '22 at 10:57
  • same error :/ do you think it might be because of the configuration file of elastic search? – Muhammad Nov 28 '22 at 11:16
  • You seem to have included a link to an image in place of the error message. Can you [edit] the question to make sure you include the error, as text and not an image, directly in the question and not behind a link? It's also likely some additional details like your client configuration are needed; nothing in the Compose file shows for example what host name and port you're connecting to. – David Maze Nov 28 '22 at 11:28

1 Answers1

0

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.

Muhammad
  • 11
  • 3