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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With