Running Elasticsearch with Docker

I installed Elasticsearch in my image based on ubuntu:16.04.

And start the service using

RUN service elasticsearch start

but, it was not started.

If I go into the container and run it, it starts.

I want to run the service and dump the index when I create the image, below is a part of my Dockerfile.

How do I start Elasticsearch in the Dockerfile?

#install OpenJDK-8
RUN apt-get update && apt-get install -y openjdk-8-jdk && apt-get install -y ant && apt-get clean

RUN apt-get update && apt-get install -y ca-certificates-java && apt-get clean
RUN update-ca-certificates -f

ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME

#download ES
RUN wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | apt-key add -
RUN apt-get install -y apt-transport-https
RUN echo "deb https://artifacts.elastic.co/packages/6.x/apt stable main" | tee -a /etc/apt/sources.list.d/elastic-6.x.list
RUN apt-get update && apt-get install -y elasticsearch

RUN service elasticsearch start

Source: StackOverflow