Fady Yousry
Fady Yousry

Reputation: 171

Failed to open TCP connection to localhost:9200 - Rails on Docker

I 'm want to run ElasticSearch on docker and connect rails with it.

this is my docker-compose.yml file

version: '3'
services:
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: P@ssw0rd
      MYSQL_DATABASE: chatsystem
      MYSQL_USER: root
      MYSQL_PASSWORD: P@ssw0rd
    ports:
      - "3307:3306"

  es01:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.4.2
    container_name: es01
    environment:
      - node.name=es01
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es02,es03
      - cluster.initial_master_nodes=es01,es02,es03
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data01:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - elastic

  es02:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.4.2
    container_name: es02
    environment:
      - node.name=es02
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es01,es03
      - cluster.initial_master_nodes=es01,es02,es03
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data02:/usr/share/elasticsearch/data
    networks:
      - elastic
  es03:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.4.2
    container_name: es03
    environment:
      - node.name=es03
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es01,es02
      - cluster.initial_master_nodes=es01,es02,es03
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data03:/usr/share/elasticsearch/data
    networks:
      - elastic

  app:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - ".:/ChatSystem"
    depends_on:
      - db
      - es01
      - es02
      - es03
    ports:
      - "3000:3000"
    links:
      - db
      - es01
      - es02
      - es03
    environment:
      DB_USER: root
      DB_NAME: chatsystem
      DB_PASSWORD: P@ssw0rd
      DB_HOST: db

volumes:
  data01:
    driver: local
  data02:
    driver: local
  data03:
    driver: local

networks:
  elastic:
    driver: bridge

When I run docker-compose up and access localhost:9200 through the browser. I get this response

{
  "name": "es01",
  "cluster_name": "es-docker-cluster",
  "cluster_uuid": "fNDAvsI6QUyHkzy919PHhw",
  "version": {
    "number": "7.4.2",
    "build_flavor": "default",
    "build_type": "docker",
    "build_hash": "2f90bbf7b93631e52bafb59b3b049cb44ec25e96",
    "build_date": "2019-10-28T20:40:44.881551Z",
    "build_snapshot": false,
    "lucene_version": "8.2.0",
    "minimum_wire_compatibility_version": "6.8.0",
    "minimum_index_compatibility_version": "6.0.0-beta1"
  },
  "tagline": "You Know, for Search"
}

When i try create new "message" i get this error

Errno::EADDRNOTAVAIL (Failed to open TCP connection to localhost:9200 (Cannot assign requested address - connect(2) for "localhost" port 9200))

message.rb file

class Message < ApplicationRecord
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  settings do
    mappings dynamic: false do
      indexes :message, type: :text
    end
  end

end

Message.import force: true

I 'm using this gems

gem 'elasticsearch-model', git: 'git://github.com/elastic/elasticsearch-rails.git', branch: 'master'
gem 'elasticsearch-rails', git: 'git://github.com/elastic/elasticsearch-rails.git', branch: 'master'

Upvotes: 3

Views: 3828

Answers (1)

Stevan O.
Stevan O.

Reputation: 161

On your environment you must add ELASTICSEARCH_URL=http://elasticsearch:9200 and then rebuild your docker compose. You can take this article as reference.

Upvotes: 4

Related Questions