Application running in docker can't connect with elasticsearch docker

☆樱花仙子☆ 提交于 2020-12-26 04:29:24

问题


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

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)

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.

Below is my Dockerfile for DW application

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:

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

    "Aliases": [ "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.

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?


回答1:


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=<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 compose file could then look like

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


来源:https://stackoverflow.com/questions/61171399/application-running-in-docker-cant-connect-with-elasticsearch-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!