ruby-on-rails - 无法打开到 localhost:9200 的 TCP 连接 - Rails on Docker compose
问题描述
我正在尝试在 docker 上运行弹性搜索。我为与我的数据库容器连接的 rails 应用程序构建了一个容器。我想将此 rails 容器与弹性容器连接,但出现此错误
无法打开到 localhost:9200 的 TCP 连接(无法分配请求的地址 - “localhost”端口 9200 的连接(2))
当我运行 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"
}
解决方案
docker-compose 文件中的“网络”服务没有在其 localhost 端口 9000 上运行任何东西。
但是,它是网络(由 docker-compose 创建)的一部分,还有许多其他服务(elasticsearch 和 db)。在主机“elasticsearch”端口 9200 上的该网络中可以访问弹性搜索服务。更好的说法是:http://elasticsearch:9200/
推荐阅读
- python - Django Views.py 验证电话号码
- android - 如何在不同尺寸的 Android 设备上创建看起来完美缩放像素的 UI
- kdb - 如何将函数应用于 KDB 中的某些列?
- c# - 如何在actin按钮单击时删除toast通知的历史更改事件?
- file - 打开文件资源管理器按日期显示
- java - 如何使Java进程使用多个CPU内核
- sql - 如何在 PL SQL 中使用 Replace/Substitution 函数
- android - 当用户单击我的应用程序中的按钮时,如何自动向自己发送电子邮件?
- macos - 为什么在 Mac 上 Angular 安装问题和权限失败?
- arrays - 将变量列表转换为数组