无法使用 Docker-Compose 中的图像连接到 Elasticsearch

Unable to connect to Elasticsearch using images in Docker-Compose

当我从 Docker Compose 运行 项目时,我无法将它连接到 ElasticSearch。 ElasticSearch 无法读取日志,但是当我 运行 在本地它工作正常。

docker-撰写: 版本:'3.9'

services:

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.11.1
    ports:
      - "9200:9200"
    networks:
      - network-elastic
    environment:
      discovery.type: single-node
      ES_JAVA_OPTS: "-Xms1g -Xmx1g"

  clinicaonline:
    build: .
    ports:
      - "5005:80"
    depends_on:
      - elasticsearch

networks: 
  network-elastic:
    driver: bridge

连接字符串:

"Elasticsearch": {
    "Uri": "http://elasticsearch:9200"
  }

您需要将相同的网络(network-elastic) 添加到clinicaonline 服务。这应该有效:

services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.11.1
    ports:
      - "9200:9200"
    networks:
      - network-elastic
    environment:
      discovery.type: single-node
      ES_JAVA_OPTS: "-Xms1g -Xmx1g"
  clinicaonline:
    build: .
    ports:
      - "5005:80"
    networks:
      - network-elastic
    depends_on:
      - elasticsearch
networks: 
  network-elastic:
    driver: bridge