【问题标题】:Failed to open TCP connection to localhost:9200 - Rails on Docker compose无法打开到 localhost:9200 的 TCP 连接 - Rails on Docker compose
【发布时间】:2021-11-03 09:08:06
【问题描述】:

我正在尝试在 docker 上运行弹性搜索。我为与我的数据库容器连接的 rails 应用程序构建了一个容器。我想将此 rails 容器与弹性容器连接,但出现此错误

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

当我运行 docker-compose up 时,这些 url 工作正常 0.0.0.0:3000, 0.0.0.0:9200

这是我的 docker-compose.yml

version: "3.9"
services:
  db:
    restart: always
    image: postgres:latest
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: password
    ports:
      - "5432:5432"
  web:
    build:
      context: .
      dockerfile: docker/Dockerfile
    command:  bundle exec rails server -b 0.0.0.0
    environment: 
      POSTGRES_HOST: db
      POSTGRES_PASSWORD: password
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    depends_on:
      - db
    entrypoint: docker/entrypoint.sh
  elasticsearch:
    image: elasticsearch:7.14.0
    container_name: elasticsearch
    environment:
      - "ELASTICSEARCH_URL=http://elasticsearch:9200"
      - node.name=elasticsearch
      - cluster.name=es-docker-cluster
      - "ELASTIC_HOST=elasticsearch"
      - cluster.initial_master_nodes=elasticsearch
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    networks:
      - elastic
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65536
        hard: 65536
    ports:
      - "9200:9200"

networks:
  elastic:
    driver: bridge

当我运行 docker-compose up 并通过浏览器访问 0.0.0.0:9200 时。我收到了这个回复

{
  "name" : "elasticsearch",
  "cluster_name" : "es-docker-cluster",
  "cluster_uuid" : "eYnzMPJAQsGJ_QGco2mF-A",
  "version" : {
    "number" : "7.14.0",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1",
    "build_date" : "2021-07-29T20:49:32.864135063Z",
    "build_snapshot" : false,
    "lucene_version" : "8.9.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

【问题讨论】:

  • "ELASTICSEARCH_URL=http://elasticsearch:9200" 应该为web 容器设置此值,而不是为elasticsearch 容器设置

标签: ruby-on-rails ruby docker elasticsearch docker-compose


【解决方案1】:

您在 docker-compose 文件中的“网络”服务没有在其 localhost 端口 9000 上运行任何东西。

但是,它是网络的一部分(由 docker-compose 创建),还有许多其他服务(elasticsearch 和 db)。 在主机“elasticsearch”端口 9200 上的该网络中可以访问弹性搜索服务。更好的说法是:http://elasticsearch:9200/

【讨论】:

    猜你喜欢
    • 2020-03-26
    • 2021-05-22
    • 2017-07-20
    • 2020-01-24
    • 2018-07-30
    • 1970-01-01
    • 2015-10-19
    • 2016-10-18
    • 1970-01-01
    相关资源
    最近更新 更多