Search In A Box With Docker, Elastic Search, Spring Boot, and Selenium


This tutorial will show you how to build a search service in a box. It'll have a graphical front end to search for some funny jokes (well, they made me laugh). Notably, you'll be able to build, test, and package it into a container with a push of a button.

We'll learn about:

As always, you can checkout the code on Github.


You'll need to have installed:

  • Maven,
  • Java (version 7 or later),
  • Docker (or, if you're on a Mac: Boot2Docker).


Lets create a pom.xml file for our project:

<project xmlns="" xmlns:xsi=""



Create The Basic Search Query Page

We're going to build this using Acceptance Test Driven Development (ATDD). As this will be a web application, we're going to use Selenide, a simple DSL that works with Selenium WebDriver to test web apps. Add these dependencies to get started:


We'll create a test that verifies the homepage shows a search box:

package searchinabox;

import org.junit.Test;
import org.openqa.selenium.By;

import static com.codeborne.selenide.Condition.exist;
import static com.codeborne.selenide.Selenide.$;
import static;

public class AppIT {

    public void homepageShowsSearchBox() throws Exception {

Run this test and you'll find it is red. We need to get our search page running!

We're going to create a Spring Boot application. Add these lines to your pom.xml:


This will sort out our dependency versions for us. Next, we'll be using Spring Boot with Thyme-leaf for templates. Add the Spring Boot dependency:


Next, create an application class, this will

  1. Expose the home page,
  2. Boot up the application.
public class App {

    public String home() {
        return "home";

    public static void main(String[] args) {, args);

We need some HTML for the home page, so make src/main/resources/templates/home.html:

<html xmlns:th="">
    <title>Search In A Box</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<form method="post">
    <input name="query"/>
    <input type="submit"/>

When you run the application, you can also run the test and you'll see that it is green. You can also look at the search form manually at http://localhost:8080.

Searching For Jokes

Sticking with ATDD, create the test first:

public void searchingForBearsFindsTheNorthPollJoke() throws Exception {


    $$(By.tagName("td")).find(exactText("The North Poll!")).should(exist);

We need to speak to a client, so create the following Spring Java configuration file:

public class Config {

    @Bean(destroyMethod = "close")
    public Node node() {
        return NodeBuilder.nodeBuilder().node();

    public Client client() {
        return node().client();

Elastic Search creates directory called data in the root of our project. We want this to be cleaned so our test always start with a fresh instance. set-up the Maven Clean Plugin to do so:


We want to make a service for indexing and searching jokes. There's will be quite a bit going on here, so let's break it down:

  1. On start-up create an index called "jokes" to store our jokes in,
  2. Then store two jokes, each with unique IDs,
  3. Finally, provide a method to search for jokes.
public class JokeSearchService {

    private Client client;

    public void indexJokes() throws Exception {
        // create an index name "jokes" to store the jokes in
        try {
        } catch (IndexAlreadyExistsException ignored) {

        storeJoke(1, "Why are teddy bears never hungry? ", "They are always stuffed!");
        storeJoke(2, "Where do polar bears vote? ", "The North Poll!");

    private void storeJoke(int id, String question, String answer) throws IOException {
        // index a document ID  of type "joke" in the "jokes" index
        client.prepareIndex("jokes", "joke", String.valueOf(id))
                                .field("question", question)
                                .field("answer", answer)

    public SearchHit[] search(String query) {
        return client.prepareSearch("jokes")
                .setQuery(QueryBuilders.multiMatchQuery(query, "question", "answer"))

Update the App and add the following lines so the we have can accept POST requests and display results:

private JokeSearchService jokeSearchService;

@RequestMapping(value = "/", method = RequestMethod.POST)
public String search(@RequestParam("query") String query, Model model) {
    return "results";

Next, we need a results page so create src/main/resources/templates/results.html:

<html xmlns:th="">
    <title>Search In A Box - Results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <tr th:each="result : ${results}">
        <td th:text="${result.source.question}"/>
        <td th:text="${result.source.answer}"/>

Restart the application, run the test, and you'll see it's now green.

Putting The Application Into A Container

We want to put the application into a Docker container. Spring Boot can create a standalone jar to put it into a container, so add this plugin to the pom.xml:


You can see this at work by creating a package:

$ mvn package

This replaces the original JAR, with a standalone version.

Next, we'll use a plugin to build the container:

        <!-- only needed if you are using Boot2Docker -->

The plugin needs some files to create the app. Each directory in src/main/docker in treated as a container, so in src/main/docker/searchinabox create a Dockerfile that:

  1. Adds the JAR,
  2. Adds a configuration file,
  3. Exposes the ports - both our app on 8080, and Elastic Search on 9200 and 9300 (so it can join a cluster),
  4. Sets the start-up command.
FROM dockerfile/java:oracle-java7


ADD ${}.jar .

CMD java -jar /${}.jar

We need a conf.yml file in the same directory, this:

  1. Indicates that we want to add the JAR as part of the Docker image,
  2. States the ports it should expose on the host,
  3. A health check URL we can use to smoke test the container,
  4. Finally, a tag for the container so we can easily identify it:
    - target/${}.jar
  - 8080
  - 9200
  - 9300
    - url: http://localhost:9200/
    - url: http://localhost:8080/

Package this and start-up the container:

mvn docker:start

You should see this:

[INFO] Starting searchinabox

The container will be listed by the docker command

$ docker ps
CONTAINER ID        IMAGE                             COMMAND                CREATED             STATUS              PORTS                                                                    NAMES
f673731a9489        searchinabox/searchinabox:1.0.0-SNAPSHOT   "/bin/sh -c 'java  -   6 seconds ago       Up 4 seconds>8080/tcp,>9200/tcp,>9300/tcp   search-in-a-box_app  

We can check we can access Elastic Search by opening the http://localhost:9200 URL, and our application by opening http://localhost:8080.


Packing containers can go wrong. I find it helpful to print/tail the logs of the last started container with this command:

docker logs -f $(docker ps -qa|head -n1)

We often want to start the container up with a shell to debug it, for example I often get the start command wrong, so here's what I'd do:

docker run -t -i  searchinabox/searchinabox:1.0.0-SNAPSHOT bash

Continuous Integration

To complete the picture we want Maven to start the containers and run our acceptance tests. We'll use the Maven Failsafe Plugin to run the tests. Add this plugin as follows:


We need to tell docker-maven-plugin to start and stop the container, so add these lines to it:


Finally, test it.

mvn clean verify


We seen how to create a search service in a box with Elastic Search, Spring Boot and Docker. We've seen how to create a build using Docker Maven Plugin. I hope you enjoyed this. Here are some exercises for you:

  • The UI is pretty drab, how about an attractive Bootstrap front end?
  • We've let Elastic Code leak all over the place. Perhaps we should refactor it behind an abstraction?
  • We might want to re-index out data. Use Spring to schedule the re-indexing.
  • We're not indexing a lot of things? Should we some new indexers for other items?