【问题标题】:Failed to connect to localhost port 9200无法连接到 localhost 端口 9200
【发布时间】:2020-06-28 19:31:39
【问题描述】:

当我在容器外调用 curl http://localhost:9200 时,它工作正常,但是当我在 php 容器内调用它时,出现错误:curl: (7) Failed to connect to localhost port 9200: Connection refused

docker-compose.yml:

version: '3.4'

services:
    php:
        build:
            context: .
            target: sylius_php
            cache_from:
                - quay.io/sylius/php:latest
                - quay.io/sylius/nodejs:latest
                - quay.io/sylius/nginx:latest
        image: quay.io/sylius/php:latest
        container_name: php
        environment:
            USER_ID: 1000
            APP_ENV: ${APP_ENV}
            APP_DEBUG: ${APP_DEBUG}
            APP_SECRET: ${APP_SECRET}
            PHP_DATE_TIMEZONE: ${PHP_DATE_TIMEZONE:-UTC}
        volumes:
            - .:/srv/sylius:rw,cached
            - ./public:/srv/sylius/public:rw,delegated
            - public-media:/srv/sylius/public/media:rw

    elasticsearch:
        image: docker.elastic.co/elasticsearch/elasticsearch:6.4.3
        container_name: elasticsearch
        environment:
            - cluster.name=docker-cluster
            - bootstrap.memory_lock=true
            - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
        ulimits:
            memlock:
                soft: -1
                hard: -1
        volumes:
            - esdata1:/usr/share/elasticsearch/data
        ports:
            - 9200:9200
        networks:
            - esnet

    elasticsearch2:
        image: docker.elastic.co/elasticsearch/elasticsearch:6.4.3
        container_name: elasticsearch2
        environment:
            - cluster.name=docker-cluster
            - bootstrap.memory_lock=true
            - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
            - "discovery.zen.ping.unicast.hosts=elasticsearch"
        ulimits:
            memlock:
                soft: -1
                hard: -1
        volumes:
            - esdata2:/usr/share/elasticsearch/data
        networks:
            - esnet

volumes:
    esdata1:
        driver: local
    esdata2:
        driver: local

networks:
    esnet:

问题是我需要从我的 php 容器访问 elasticsearch 服务器才能执行某些与 php 相关的命令。

【问题讨论】:

    标签: docker elasticsearch sylius


    【解决方案1】:

    每个容器在内部网络上都有不同的 IP,但可以在与其服务名称对应的主机下访问,在您的情况下,它将是 http://elasticsearch:9200 和 http://elasticsearch2:9200。

    【讨论】:

    • 它不起作用curl: (6) Could not resolve host: elasticsearch
    • 您的 PHP 容器不在同一个网络上。您有网络 esnet 并向其中添加两个 ES 容器,但没有添加 PHP 容器。
    猜你喜欢
    • 2015-10-19
    • 2016-10-18
    • 2017-07-20
    • 2019-07-29
    • 2020-01-24
    • 1970-01-01
    • 2015-12-03
    • 2016-03-24
    • 1970-01-01
    相关资源
    最近更新 更多