无法连接到本地主机端口9200

时间:2020-06-28 19:31:39

标签: docker elasticsearch sylius

当我在容器外调用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相关的某些命令。

1 个答案:

答案 0 :(得分:2)

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