Forkmohit
Forkmohit

Reputation: 753

Docker: Unable to connect to elastic search form spring boot docker image

Dockerized Spring Boot App is not able to connect to Elastic Search server running locally at default address and port. Following exception is thrown:

2018-01-20 07:10:38.529  INFO 1 --- [           main] org.elasticsearch.client.transport       : [Gemini] failed to connect to node [{#transport#-1}{localhost}{127.0.0.1:9300}], removed from nodes list

org.elasticsearch.transport.ConnectTransportException: [][127.0.0.1:9300] connect_timeout[30s]
    at org.elasticsearch.transport.netty.NettyTransport.connectToChannelsLight(NettyTransport.java:967) ~[elasticsearch-2.4.6.jar!/:2.4.6]
    at org.elasticsearch.transport.netty.NettyTransport.connectToNode(NettyTransport.java:933) ~[elasticsearch-2.4.6.jar!/:2.4.6]
    at org.elasticsearch.transport.netty.NettyTransport.connectToNodeLight(NettyTransport.java:906) ~[elasticsearch-2.4.6.jar!/:2.4.6]
    at org.elasticsearch.transport.TransportService.connectToNodeLight(TransportService.java:267) ~[elasticsearch-2.4.6.jar!/:2.4.6]
    at org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler.doSample(TransportClientNodesService.java:390) ~[elasticsearch-2.4.6.jar!/:2.4.6]

Docker File:

FROM openjdk:8
ADD target/CryptoAPI-1.0.0.jar CryptoAPI.jar
EXPOSE 80
ENTRYPOINT ["java", "-jar", "CryptoAPI.jar"]

Docker command to run the image:

docker run --net=host cryptoapi

Upvotes: 0

Views: 886

Answers (1)

Sasha Shpota
Sasha Shpota

Reputation: 10280

If you access 127.0.0.1 within a docker container - it tries to connect to itself (in your case container with spring boot tries to access its 9300 port). The container doesn't know and shouldn't know anything about surrounding environment - that's dockers core principle.

There are some workarounds, see this thread on docker forum. But general idea is that you shouldn't try to access host machine from within a container.

The proper way would be to start elastic search within a container, establish docker network and communicate between the two containers by their names (domain names within the docker network).

Upvotes: 1

Related Questions