Connect from docker container to host elasticsearch runing instance

Viewed 3494

I have this simple setup: A Dockerfile:

FROM centos:latest
RUN yum update -y
RUN yum install -y epel-release
RUN yum install -y java-1.8.0-openjdk
RUN yum install -y curl
RUN mkdir /var/totest
EXPOSE 9200
EXPOSE 9300

And on host machine I have a running instance of elasticsearch 5.4.2.

When I do on host curl http://localhost:9200 I get the correct response:

  {
  "name" : "T8apV_J",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "YFkkaM8dSJCnXbDFRFD_aw",
  "version" : {
    "number" : "5.4.2",
    "build_hash" : "929b078",
    "build_date" : "2017-06-15T02:29:28.122Z",
    "build_snapshot" : false,
    "lucene_version" : "6.5.1"
  },
  "tagline" : "You Know, for Search"
}

but, if i build the docker image and I run

docker run -p 9200:9200 -it my-simple-image /bin/bash

I get this error:

docker: Error response from daemon: driver failed programming external connectivity on endpoint infallible_brown (71c6fae1275d149b2708a1aa9b737278d340c4e7b073d858b4222eb0268ef285): Error starting userland proxy: listen tcp 0.0.0.0:9200: bind: address already in use.

How can I connect from inside the docker container to the host running elasticsearch instance?

All I need is to be able to perform from inside the container

curl http://localhost:9200/index_name/_search

3 Answers

Here is a quick hack to get it working. With Elastic accessible on your host with curl -X GET 'http://0.0.0.0:9200', I think docker run --net host --name your_app will allow the container'd app to query the Elastic instance running on your host. So, you can omit any EXPOSE or -p settings. But be aware, according to their docs:

--network="host" gives the container full access to local system services such as D-bus and is therefore considered insecure.

Related