本文介绍了在Docker中运行的应用程序无法与Elasticsearch Docker连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是docker的新手,有一个简单的DW(dropwizard)应用程序连接到elasticsearch,该应用程序已经在docker中使用docker-compose.yml在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.

用于Elasticsearch的Docker-compose.yml

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

注意:我在主机(本地mac系统)上将8200和8300公开为ES端口

现在,当我简单地运行连接到本地主机上8200上的ES的DW应用程序时,一切工作正常,,但是现在我试图对我的DW应用程序进行docker化,并且遇到了一些问题.

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.

以下是我的DW应用程序的Dockerfile

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

当我运行上面的DW docker映像时,它立即停止运行,使用 docker日志< my-container-id> ,它会引发以下异常:

When I run my above DW docker image, it immediately stops, using docker logs <my-container-id>, it throws below exception:

*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**

我尝试过的事情

  1. 错误消息明确提到我的DW应用程序docker实例无法连接到elasticsearch,经验证运行正常.
  2. 还检查了Elaticsearch docker的网络,它的网络别名为 elasticsearch ,如下所示,n/w为 docker-files_default .

  1. The error message clearly mentions my DW app docker instance is not able to connect to elasticsearch, which I verified running fine.
  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.

别名":["elasticsearch","de78c684ae60"]

"Aliases": [ "elasticsearch", "de78c684ae60" ],

检查了我的DW应用docker实例的n/w,它使用 bridge 网络,并且没有任何网络别名.

Checked the n/w of my DW app docker instance and it uses bridge network and doesn't have any network alias.

现在,我如何才能使我的应用程序docker和elasticsearch docker都使用同一网络,以便它们可以彼此连接,我想这将解决问题?

推荐答案

两种解决方法:首先是检查为您的Elasticsearch设置创建的网络docker-compose( docker network ls ),然后然后使用

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 run --network =<网络名称>...

第二种方法是创建一个网络 docker network create elastic 并将其用作docker compose文件以及DW应用程序的docker run命令中的外部网络.

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撰写文件可能看起来像

Docker compose file could then look like

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

这篇关于在Docker中运行的应用程序无法与Elasticsearch Docker连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 19:14