无法使用Docker内部的PHP连接到Elasticsearch
这是我的无法使用Docker内部的PHP连接到Elasticsearch,php,
elasticsearch,docker,docker-compose,Php,
elasticsearch,Docker,Docker Compose,这是我的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
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"
当我尝试通过进入localhost:9200
来访问Elasticsearch时,它可以工作
但是,当我尝试使用PHP创建索引时,会出现以下错误:
致命错误:未捕获
Elasticsearch\Common\Exceptions\NoNodeAvailableException:无活动
在中找到群集中的节点
以下是客户端代码:
<?php
namespace App\Elasticsearch;
use Elasticsearch\ClientBuilder;
class Client
{
public static function getClient()
{
return ClientBuilder::create()
->build();
}
}
更新
从
这一页。我试着
$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”]数组(2){[“error”]”无法连接到本地主机端口
9200:连接被拒绝“[“errno”]7
您试图连接到本地主机,但需要连接到“弹性”主机。 尝试从php连接到elasticsearch,如下所示:
$hosts = [
'elastic', // elastic host was added to you hosts file automatically
];
$client = ClientBuilder::create()
->setHosts($hosts)
->build();
链接服务的容器将可在与别名相同的主机名处访问,如果未指定别名,则可在与服务名称相同的主机名处访问
看起来确实是一个端口问题(有点像中的),也有点像如何从php访问弹性体?@opHASnoNAME我更新了问题中的那部分,但没有帮助。实际上app:是我的数据唯一容器(不确定我是否使用了正确的术语,刚刚开始学习docker)。如果您看到php:我已经有到elastic的链接。是的,我的错误是,Think app容器是可执行的,但它是php文件的唯一容器。可能的问题是在factory Client::getClient()中设置elastic host的ommision;尝试添加setHost()调用。返回ClientBuilder::create()->setHost(['elastic']])->build();如果它不起作用,请显示/etc/hosts of php container!这很有效!你能解释一下你刚才的建议吗?Docker在容器之间创建了一个虚拟网络。你在Docker-compose.yml文件4服务(容器)中定义了名称为nginx、php、app、elastic。链接两个服务时,docker compose会将其他服务的路由信息添加到/etc/hosts文件中。服务的主机名是docker compose文件中的服务名称。(有一个“elastic”)
$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());
}
$hosts = [
'elastic', // elastic host was added to you hosts file automatically
];
$client = ClientBuilder::create()
->setHosts($hosts)
->build();