Connect Spring with Elasticsearch in Docker

I have to connect my Spring client with Elasticsearch whose image have been taken by the official ES 2.4.6 image that Elastic has in Docker Hub but when I try to run the containers, the Docker console reports me this error:

[Glitch] failed to connect to node [{#transport#-1}{localhost}{127.0.0.1:9300}], removed from nodes list

The application.properties of my Spring project is:

spring.data.elasticsearch.cluster-nodes=localhost:9300
index.v = default
server.port = 8443

And the docker-compose.yml is:

version: "2.2"

services:
    elk:
        image: cvazquezlos/elk:2.4.6
        ports:
            - 5000:5000
            - 5601:5601
            - 9200:9200
            - 9300:9300
        volumes:
            - elk-data:/var/lib/elasticsearch

    testloganalyzer:
        image: cvazquezlos/testloganalyzer
        ports:
            - 8443:8443

volumes: 
    elk-data:

If I run the backend without Docker it works as expected, but when I run the backend with Docker, reports me the above error. The complete error is:

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]
        at org.elasticsearch.client.transport.TransportClientNodesService$NodeSampler.sample(TransportClientNodesService.java:336) [elasticsearch-2.4.6.jar!/:2.4.6]
        at org.elasticsearch.client.transport.TransportClientNodesService$ScheduledNodeSampler.run(TransportClientNodesService.java:369) [elasticsearch-2.4.6.jar!/:2.4.6]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_151]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_151]
        at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151]
Caused by: java.net.ConnectException: Connection refused: localhost/127.0.0.1:9300
        at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_151]
        at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[na:1.8.0_151]
        at org.jboss.netty.channel.socket.nio.NioClientBoss.connect(NioClientBoss.java:152) ~[netty-3.10.6.Final.jar!/:na]
        at org.jboss.netty.channel.socket.nio.NioClientBoss.processSelectedKeys(NioClientBoss.java:105) ~[netty-3.10.6.Final.jar!/:na]
        at org.jboss.netty.channel.socket.nio.NioClientBoss.process(NioClientBoss.java:79) ~[netty-3.10.6.Final.jar!/:na]
        at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:337) ~[netty-3.10.6.Final.jar!/:na]
        at org.jboss.netty.channel.socket.nio.NioClientBoss.run(NioClientBoss.java:42) ~[netty-3.10.6.Final.jar!/:na]
        at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108) ~[netty-3.10.6.Final.jar!/:na]
        at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42) ~[netty-3.10.6.Final.jar!/:na]
        ... 3 common frames omitted

Upvotes: 2

Views: 2116

Answers (2)

Val
Val

Reputation: 217554

I think you're missing the networks section in your docker-compose configuration, try this:

version: "2.2"

services:
    elk:
        image: cvazquezlos/elk:2.4.6
        ports:
            - 5000:5000
            - 5601:5601
            - 9200:9200
            - 9300:9300
        networks:
            - elk-network
        volumes:
            - elk-data:/var/lib/elasticsearch

    testloganalyzer:
        image: cvazquezlos/testloganalyzer
        ports:
            - 8443:8443
        networks:
            - elk-network

volumes: 
    elk-data:

networks:
    elk-network:
        driver: bridge

Upvotes: 1

Adam Lesiak
Adam Lesiak

Reputation: 501

When the SpringBoot service works in docker it cannot resolve the localhost as a host of the ES. In this case you can also use the links property like following:

In testloganalyzer section add the links option:

testloganalyzer:
    image: cvazquezlos/testloganalyzer
    ports:
        - 8443:8443
    links:
        - elk:elk

First is the service and second is an alias.

Next change it reference in the application.properties:

spring.data.elasticsearch.cluster-nodes=elk:9300

Upvotes: 3

Related Questions