加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

无法在Docker中使用PHP连接到Elasticsearch

发布时间:2020-12-16 03:53:03 所属栏目:安全 来源:网络整理
导读:这是我的docker-compose.yml nginx: build: ./nginx/ ports: - 80:80 links: - php volumes_from: - appphp: image: php:7.0-fpm expose: - 9000 volumes_from: - app links: - elasticapp: image: php:7.0-fpm volumes: - .:/var/www/html command: "true"e

这是我的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创建索引时,我收到以下错误:

Fatal error: Uncaught
ElasticsearchCommonExceptionsNoNodesAvailableException: No alive
nodes found in your cluster in

这是客户端代码:

用于实例化Elasticsearch对象的代码:

如果我是var_dump($es),??它会转储Elasticsearch客户端对象.

但是当我尝试创建索引时,它会抛出错误.

更新

从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

最佳答案
您尝试连接到localhost,但您需要连接到“弹性”主机.
尝试从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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读