无法使用Docker中的PHP连接到Elasticsearch

这是我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" 

当我尝试访问Elasticsearch的localhost:9200它的作品。

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

致命错误:未捕获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客户端对象。

但是,当我试图创build一个索引,它会引发一个错误。

 <?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')); 

更新

从这里input链接描述

这一页。 我尝试过了

 $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”]数组(2){[“错误”]“无法连接到本地端口9200:连接被拒绝”[“errno”] 7

您尝试连接到本地主机,但您需要连接到“弹性”主机。 尝试连接到这样的PHP elasticsearch:

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

链接服务的容器将可以在与别名相同的主机名上访问,如果没有指定别名,则服务名称是可访问的。