kitz
kitz

Reputation: 889

Connect from docker container to host elasticsearch runing instance

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

Upvotes: 4

Views: 4371

Answers (3)

CubeBot88
CubeBot88

Reputation: 1110

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.

Upvotes: 0

herm
herm

Reputation: 16315

Elastic search uses port 9200. Now you want to publish port 9200 from a container to localhost. You can not have two applications listening on the same port of localhost.

Upvotes: 0

Oluwafemi Sule
Oluwafemi Sule

Reputation: 38922

You need to remove the EXPOSE 9200 directive in the Dockerfile because port number 9200 is already taken by the elastic search service.

You should curl with the ip address of your host machine. Docker attaches containers to the bridge network to start with so you need a way to get the ip address of the host.

You may need to set an alias depending on whether or not your host is connected to a wider network. I set this alias for my bridge0 interface.

sudo ifconfig bridge0 alias <ip_address>

If your host connected to a wider network, use the inet address of assigned to your ethernet device. You can get the inet address by running:

ifconfig en0 | grep "inet " | cut -d " " -f2

You can either way pass the inet address of your network interface as an environment variable to docker:

docker run -e MY_HOST_IP=$(ip_address) -it my-simple-image /bin/bash
# or
docker run -e MY_HOST_IP=$(ifconfig en0 | grep "inet " | cut -d " " -f2) -it my-simple-image /bin/bash

curl $MY_HOST_IP:8000

See this thread for more information about your question

Upvotes: 1

Related Questions