2016-07-30 9 views
5

Oto mój docker-compose.ymlNie można połączyć się Elasticsearch PHP wewnątrz Döcker

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" 

Kiedy próbuję uzyskać dostęp Elasticsearch przechodząc do localhost:9200 to działa.

Ale gdy próbuję utworzyć indeks za pomocą PHP pojawia się następujący błąd:

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

Oto kod klienta:

<?php 

namespace App\Elasticsearch; 

use Elasticsearch\ClientBuilder; 

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

Kod instancję obiektu Elasticsearch:

<?php 

require 'vendor/autoload.php'; 

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

$es = Client::getClient(); 

Jeśli I var_dump($es), zrzuca obiekt klienta Elasticsearch.

Ale kiedy próbuję utworzyć indeks, generuje błąd.

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

Aktualizacja

Od enter link description here

tej stronie. Próbowałem

$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()); 
} 

a teraz pokazuje mi błąd Curl tj

["curl"] array(2) { ["error"] "Failed to connect to localhost port 9200: Connection refused" ["errno"] 7

+0

Wygląda na numerze portu rzeczywiście (trochę jak w https://github.com/elastic/elasticsearch-php/issues/408#issuecomment -207045443) – VonC

+0

Również trochę jak https://github.com/elastic/elasticsearch-php/issues/425#issuecomment-231548912 – VonC

+0

jak uzyskać dostęp do elastycznego z php? – opHASnoNAME

Odpowiedz

2

Próbowano połączyć się z localhost, ale trzeba podłączyć do „elastyczny” gospodarza. spróbować połączyć się elasticsearch z php tak:

$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

To nie pomogło. Właściwie to aplikacja: jest kontenerem tylko dla moich danych (nie jestem pewien, czy używam terminu, dopiero zacząłem się uczyć dokera). Jeśli widzisz php: mam już link do elastycznego. –

+0

Tak, mój błąd, kontener aplikacji myślowej jest wykonywalny, ale jest to tylko kontener dla plików php. –

+1

Może to być problem polegający na ustawieniu elastycznego hosta w fabryce Client :: getClient(); spróbuj dodać wywołania setHost(). return ClientBuilder :: create() -> setHost (['elastic']]) -> build(); jeśli to nie działa show/etc/hosts kontenera php, proszę! –