【问题标题】:Unable to connect to Elasticsearch with PHP inside Docker无法在 Docker 中使用 PHP 连接到 Elasticsearch
【发布时间】:2016-12-04 23:37:17
【问题描述】:

这是我的docker-compose.yml

nginx:
    build: ./nginx/
    ports:
        - 80:80
    links:
        - php
    volumes_from:
        - app

php:
    image: php:7.0-fpm
    expose:
        - 9000
    volumes_from:
        - app
    links:
        - elastic

app:
    image: php:7.0-fpm
    volumes:
        - .:/var/www/html
    command: "true"

elastic:
    image: elasticsearch:2.3
    volumes:
      - ./elasticsearch/data:/usr/share/elasticsearch/data
      - ./elasticsearch/logs:/usr/share/elasticsearch/logs
    expose:
      - "9200"
    ports:
      - "9200:9200"

当我尝试通过转到 localhost:9200 来访问 Elasticsearch 时,它可以工作。

但是当我尝试使用 PHP 创建索引时,出现以下错误:

致命错误:未捕获 Elasticsearch\Common\Exceptions\NoNodesAvailableException:没有活着 在您的集群中找到的节点

这是客户端代码:

<?php

namespace App\Elasticsearch;

use Elasticsearch\ClientBuilder;

    class Client 
    {
        public static function getClient()
        {
            return ClientBuilder::create()
                ->build();
        }
    }

实例化 Elasticsearch 对象的代码:

<?php

require 'vendor/autoload.php';

use App\Elasticsearch\Client;
use App\Elasticsearch\Indices\UserIndex;

$es = Client::getClient();

如果我var_dump($es),它会转储 Elasticsearch 客户端对象。

但是当我尝试创建索引时,它会引发错误。

<?php

namespace App\Elasticsearch\Indices;

use App\Elasticsearch\Indices\AbstractIndex;
use Elasticsearch\Client; 

    class UserIndex extends AbstractIndex
    {
        public function __construct(Client $client)
        {
            $this->client = $client;
        }
    }

    // Create User Index
    $userIndex = new UserIndex($es);
    var_dump($userIndex->createIndex('users'));

更新

来自enter link description here

这个页面。我试过了

$es = Client::getClient();

try {
    // Create User Index
    $userIndex = new UserIndex($es);
    var_dump($userIndex->createIndex('users'));
} catch (Exception $e) {
    var_dump($es->transport->getLastConnection()->getLastRequestInfo());
}

现在它显示了 Curl 错误,即

["curl"] array(2) { ["error"] "连接本地端口失败 9200: 连接被拒绝" ["errno"] 7

【问题讨论】:

标签: php elasticsearch docker docker-compose


【解决方案1】:

您尝试连接到 localhost,但您需要连接到“弹性”主机。 尝试像这样从 php 连接到 elasticsearch:

$hosts = [
    'elastic', // elastic host was added to you hosts file automatically
];
$client = ClientBuilder::create()
   ->setHosts($hosts)
   ->build();

链接服务的容器可以通过与别名相同的主机名访问,如果没有指定别名,则使用服务名。

【讨论】:

  • 这没有帮助。实际上 app: 是我的数据唯一容器(不确定我是否使用正确的术语,刚开始学习 docker)。如果你看到 php:我已经有弹性链接。
  • 是的,我的错误,一个认为应用程序容器是可执行的,但它只是 php 文件的容器。
  • 可能的问题是在工厂 Client::getClient() 中设置弹性主机;尝试添加 setHost() 调用。 return ClientBuilder::create()->setHost(['elastic']])->build();如果它不起作用,请显示 /etc/hosts 的 php 容器!
  • 那行得通!你能解释一下你刚才的建议吗?
  • Docker 在容器之间创建一个虚拟网络。您在 docker-compose.yml 文件中定义了 4 个服务(容器),名称为 nginx、php、app、elastic。当链接 2 个服务时,docker compose 会将有关其他服务的路由信息​​添加到 /etc/hosts 文件中。服务的主机名是 docker-compose 文件中的服务名称。 (有一个“弹性”)
猜你喜欢
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
  • 2020-07-31
  • 2020-05-09
  • 2018-03-12
  • 2017-08-21
  • 2017-03-13
  • 2019-10-25
相关资源
最近更新 更多