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

这是我的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
Elasticsearch\Common\Exceptions\NoNodesAvailableException: 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.

相关文章

文章浏览阅读8.8k次,点赞2次,收藏7次。本文介绍Docker Com...
文章浏览阅读1.5w次,点赞7次,收藏76次。原网提供的教程需要...
文章浏览阅读940次,点赞20次,收藏20次。通过 docker run 命...
文章浏览阅读1k次,点赞20次,收藏20次。Podman 是一个开源的...
文章浏览阅读2.1k次。请注意,这些命令需要在 Docker 主机上...
文章浏览阅读1.1k次,点赞37次,收藏40次。nacos搭建集群连接...