2016-07-30 112 views
5

这里是我的docker-compose.yml无法连接用PHP Elasticsearch内泊坞

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,我得到以下错误创建索引:

Fatal error: Uncaught Elasticsearch\Common\Exceptions\NoNodesAvailableException: No alive nodes found in your cluster in

下面是客户端代码:

<?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"] array(2) { ["error"] "Failed to connect to localhost port 9200: Connection refused" ["errno"] 7

+0

看起来像一个端口的问题确实(有点在https://github.com/elastic/elasticsearch-php/issues/408#issuecomment -207045443) – VonC

+0

也有点像https://github.com/elastic/elasticsearch-php/issues/425#issuecomment-231548912 – VonC

+0

你如何从PHP获取弹性? – opHASnoNAME

回答

2

你试图连接到本地主机显示我恶心的错误,但你需要连接到“弹性”的主机。 尝试连接从PHP到elasticsearch这样的:

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

Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified.

+0

这没有帮助。其实应用程序:是我的数据唯一容器(不知道如果我使用正确的术语,刚开始学习码头)。如果你看到php:我已经有链接到弹性。 –

+0

是的,我的错误,一个思想应用程序容器是可执行的,但它只是PHP文件的容器。 –

+1

可能是工厂Client :: getClient()中设置弹性主机的问题。 尝试添加setHost()调用。 return ClientBuilder :: create() - > setHost(['elastic']]) - > build(); 如果不能显示php容器的/ etc/hosts,请! –