我想在docker上运行ElasticSearch并与其连接导轨。

这是我的docker-compose.yml文件

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

当我运行docker-compose up并通过浏览器访问localhost:9200时。我得到这个回应
{
  "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"
}

当我尝试创建新的“消息”时,出现此错误

Errno::EADDRNOTAVAIL(无法打开与localhost:9200的TCP连接(无法分配请求的地址-“localhost”端口9200的connect(2)))

message.rb文件
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

我正在使用这种 gem
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'

最佳答案

在您的环境中,您必须添加ELASTICSEARCH_URL=http://elasticsearch:9200,然后重建您的docker compose。您可以将此article作为引用。

关于ruby-on-rails - 无法打开与localhost:9200的TCP连接-Docker上的Rails,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59124103/

10-11 08:47