简体   繁体   English

在 docker 中运行的应用程序无法与 elasticsearch docker 连接

[英]Application running in docker can't connect with elasticsearch docker

I am new to docker and having a simple DW(dropwizard) application that connects to elasticsearch, Which is already running in docker using the docker-compose.yml, which has the following content. I am new to docker and having a simple DW(dropwizard) application that connects to elasticsearch, Which is already running in docker using the docker-compose.yml, which has the following content.

Docker-compose.yml for elasticsearch Docker-compose.yml 用于 elasticsearch

version: '2.2'

services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.6.0
    container_name: elasticsearch
    environment:
      - xpack.security.enabled=false
      - discovery.type=single-node
    ports:
      - 8200:9200
      - 8300:9300

volumes:
  elasticsearch-data:
    driver: local

Note: I am exposing 8200 and 8300 as ES port on my host(local mac system)注意:我将 8200 和 8300 暴露为主机上的 ES 端口(本地 mac 系统)

Now everything works fine when I simply run my DW application which connects to ES in 8200 on localhost, but now I am trying to dockerize my DW application and facing few issues.现在,当我在 localhost 上简单地运行连接到 8200 中的 ES 的 DW 应用程序时,一切正常,但现在我正在尝试对我的 DW 应用程序进行 dockerize 并面临一些问题。

Below is my Dockerfile for DW application下面是我的 Dockerfile 用于 DW 应用程序

COPY target/my.jar my.jar
COPY config.yml config.yml
ENTRYPOINT ["java" , "-jar" , "my.jar", "server", "config.yml"]

When I run my above DW docker image, it immediately stops, using docker logs <my-container-id> , it throws below exception:当我运行上面的 DW docker 图像时,它立即停止,使用docker logs <my-container-id> ,它抛出以下异常:

*java.io.IOException: elasticsearch: Name does not resolve*
    org.elasticsearch.client.IndicesClient.exists(IndicesClient.java:827)
**Caused by: java.net.UnknownHostException: elasticsearch: Name does not resolve**

Things I have tried我尝试过的事情

  1. The error message clearly mentions my DW app docker instance is not able to connect to elasticsearch, which I verified running fine.错误消息清楚地提到了我的 DW 应用程序 docker 实例无法连接到 elasticsearch,我验证它运行良好。
  2. Also checked the network of Elaticsearch docker and it has the network alias as elasticsearch as shown below and n/w as docker-files_default .还检查了 Elaticsearch docker 的网络,它的网络别名为elasticsearch ,如下所示,n/w 为docker-files_default

    "Aliases": [ "elasticsearch", "de78c684ae60" ], “别名”:[“elasticsearch”,“de78c684ae60”],

  3. Checked the n/w of my DW app docker instance and it uses bridge network and doesn't have any network alias.检查了我的 DW 应用程序 docker 实例的 n/w,它使用bridge网络并且没有任何网络别名。

Now, how can I make both my app docker and elasticsearch docker use the same network so that they can connect with each other, I guess this would solve the issue?现在,如何让我的应用程序 docker 和 elasticsearch docker 使用相同的网络,以便它们可以相互连接,我想这会解决问题吗?

Two ways to solve this: First is to check what network docker-compose created for your elasticsearch setting ( docker network ls ) and then run your DW app with解决此问题的两种方法:首先是检查 docker-compose 为您的 elasticsearch 设置创建的网络( docker network ls ),然后运行您的 DW 应用程序

docker run --network=<name of network>...

Second way is to create a network docker network create elastic and use it as external network in your docker compose file as well as in your docker run command for the DW app.第二种方法是创建网络docker network create elastic并将其用作 docker 撰写文件以及 DW 应用程序的 docker 运行命令中的外部网络。

Docker compose file could then look like Docker 撰写文件可能看起来像

...
services:
  elasticsearch:
     networks:
       elastic:
...
networks:
    elastic:
        external: true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM