Elasticsearch using Docker

Elasticsearch is a distributed RESTFul search tool over the HTTP protocol. And we are going to use Docker to spin up multiple nodes in the cluster. First we need a server node running Docker. I’m using a Debian server so the command I need is apt-get:

# apt-get install docker.io

After installing the package make sure the docker command is available:

# docker version
Client version: 1.3.1
Client API version: 1.15
Go version (client): go1.3.2
Git commit (client): 4e9bbfa
OS/Arch (client): linux/amd64
Server version: 1.3.1
Server API version: 1.15
Go version (server): go1.3.2
Git commit (server): 4e9bbfa

Excellent we now have docker, lets start by downloading a image , the below command will download the latest Debian docker image:

# docker create debian
Unable to find image 'debian' locally
debian:latest: The image you are pulling has been verified
511136ea3c5a: Pull complete
f10807909bc5: Pull complete
f6fab3b798be: Pull complete
Status: Downloaded newer image for debian:latest
6cf83ed03695134d2a606f63c494dcf6e9dedcb2fe0db2768d8d5a95baac50c1

We can verify the debian image is available using the docker command images :

# docker images
REPOSITORY   TAG     IMAGE ID      CREATED      VIRTUAL SIZE
debian       latest  f6fab3b798be  8 days ago   85.1 MB

Next I will shell into the debian image and install some packages:

# docker run -t -i f6fab3b798be /bin/bash
root@85bcc90e1983:/#

You will want to take note of the running process id (

85bcc90e1983

) from the prompt.

Next lets update your apt repository cache and installed java runtime environment:

root@85bcc90e1983:/# apt-get update
root@85bcc90e1983:/# apt-get install openjdk-7-jre

From here we can get the latest statically compiled binaries from the elasticsearch download page :

root@85bcc90e1983:~# apt-get install wget
root@85bcc90e1983:~# wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.0.tar.gz
root@85bcc90e1983:~# tar -zxvf elasticsearch-1.4.0.tar.gz

From here lets test starting up the elasticsearch process:

root@85bcc90e1983:~# elasticsearch-1.4.0/bin/elasticsearch
[2014-11-15 00:18:30,616][INFO ][node ] [Ape-Man] version[1.4.0], pid[6482], build[bc94bd8/2014-11-05T14:26:12Z]
[2014-11-15 00:18:30,617][INFO ][node ] [Ape-Man] initializing ...
[2014-11-15 00:18:30,620][INFO ][plugins ] [Ape-Man] loaded [], sites []
[2014-11-15 00:18:32,805][INFO ][node ] [Ape-Man] initialized
[2014-11-15 00:18:32,805][INFO ][node ] [Ape-Man] starting ...
[2014-11-15 00:18:32,893][INFO ][transport ] [Ape-Man] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/172.17.0.2:9300]}
[2014-11-15 00:18:32,905][INFO ][discovery ] [Ape-Man] elasticsearch/-LrLApD4RhyPpz8VYbDAnQ
[2014-11-15 00:18:36,671][INFO ][cluster.service ] [Ape-Man] new_master [Ape-Man][-LrLApD4RhyPpz8VYbDAnQ][85bcc90e1983][inet[/172.17.0.2:9300]], reason: zen-disco-join (elected_as_master)
[2014-11-15 00:18:36,700][INFO ][http ] [Ape-Man] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/172.17.0.2:9200]}
[2014-11-15 00:18:36,700][INFO ][node ] [Ape-Man] started
[2014-11-15 00:18:36,711][INFO ][gateway ] [Ape-Man] recovered [0] indices into cluster_state

Everything looks good, lets CTRL+C out of the elasticsearch process and CTRL+C out of our docker process.

We then need to commit our change we made to the debian image, but we will save it as a new image name, we need the docker process id mentioned previously:

# docker commit -a 'jness' -m 'Elasticsearch v1.4.0' 85bcc90e1983 jness/elasticsearch:v1

It is now time to run a elasticsearch process using our new image, we will need to make sure to map our network ports (transport process, and http process). Lets first find the IMAGE ID:

# docker images
REPOSITORY          TAG    IMAGE ID      CREATED             VIRTUAL SIZE
jness/elasticsearch v1     2a523f874a5c  About a minute ago  612.7 MB
debian              latest f6fab3b798be  8 days ago          85.1 MB

Using the above IMAGE ID we can start a process using the elasticsearch binary:

# docker run -d -p 9200:9200 -p 9300:9300 2a523f874a5c /root/elasticsearch-1.4.0/bin/elasticsearch

We should now have a running docker process, lets check using the docker ps command:

# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b621c107a1fb jness/elasticsearch:v1 "/root/elasticsearch 36 seconds ago Up 35 seconds 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp stoic_pike

Looks like we have our process running, lets make sure we can access it from the host using curl:

# curl -XGET localhost:9200
{
 "status" : 200,
 "name" : "Franklin Storm",
 "cluster_name" : "elasticsearch",
 "version" : {
 "number" : "1.4.0",
 "build_hash" : "bc94bd81298f81c656893ab1ddddd30a99356066",
 "build_timestamp" : "2014-11-05T14:26:12Z",
 "build_snapshot" : false,
 "lucene_version" : "4.10.2"
 },
 "tagline" : "You Know, for Search"
}

Sweet we have a response! lets have it store some data shall we?

# curl -XPOST localhost:9200/ness/nessy/1/ -d '
{
 "full_name" : "nessy"
}
'
{"_index":"ness","_type":"nessy","_id":"1","_version":1,"created":true}

And we should be able to retrieve that same piece of data:

 curl -XGET localhost:9200/ness/nessy/1/
{"_index":"ness","_type":"nessy","_id":"1","_version":1,"found":true,"_source":
{
 "full_name" : "nessy"
}
}

And finally lets see the true power of elasticsearch by adding a couple more nodes, we will need to make sure we map the ports to unused ports on the host:

# docker run -d -p 9201:9200 -p 9301:9300 2a523f874a5c /root/elasticsearch-1.4.0/bin/elasticsearch

# docker run -d -p 9202:9200 -p 9302:9300 2a523f874a5c /root/elasticsearch-1.4.0/bin/elasticsearch

And without doing anything these two additional nodes should return the same data:

# curl -XGET localhost:9201/ness/nessy/1/
{"_index":"ness","_type":"nessy","_id":"1","_version":1,"found":true,"_source":
{
 "full_name" : "nessy"
}
}
# curl -XGET localhost:9202/ness/nessy/1/
{"_index":"ness","_type":"nessy","_id":"1","_version":1,"found":true,"_source":
{
 "full_name" : "nessy"
}
}

And there you have it! A single server running three docker processes of elasticsearch.

Hope you enjoyed this little walk-through  !