12

Here is my 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"

When I try to access Elasticsearch by going to localhost:9200 it works.

But when I try to create an index using PHP I get the following error:

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

Here is the client code:

<?php

namespace App\Elasticsearch;

use Elasticsearch\ClientBuilder;

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

Code to instantiate an Elasticsearch object:

<?php

require 'vendor/autoload.php';

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

$es = Client::getClient();

If I var_dump($es), it dumps the Elasticsearch client object.

But when I attempt to create an index it throws an error.

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

Update

From enter link description here

this page. I tried to

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

and now it shows me Curl error i.e

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

4

1 Answer 1

9

You tried to connect to localhost, but you need to connect to "elastic" host. try to connect to elasticsearch from php like this:

$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.

Sign up to request clarification or add additional context in comments.

5 Comments

That didn't help. Actually app: is my data only container (not sure if i am using right term, just started learning docker). If you see php: i already have link to elastic.
yes, my mistake, a thought app container is executable, but it is only container for php files.
May be issue is ommision setting elastic host in factory Client::getClient(); try add setHost() calling. return ClientBuilder::create()->setHost(['elastic']])->build(); if it does't work show /etc/hosts of php container, please!
That works man ! can you explain what you have just suggested.
Docker creates a virtual network between containers. You defined in your docker-compose.yml file 4 services (containers) with names nginx, php, app, elastic. When 2 services is linked docker compose adds route information about other service into /etc/hosts file. hostname of service is name of service into docker-compose file. (there is a "elastic")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.