Persisting data

The last essential step before we complete our migration to Docker is to persist the data inside our Elasticsearch container(s).

Docker containers, by their nature, are ephemeral, which means after they are removed, the data contained inside them are lost.

To persist data, or to allow containers to use existing data, we must use Volumes. Let’s use the docker CLI to create it now:

$ docker volume create --name esdata

We can use the -v flag to instruct Docker to mount this named volume into the /usr/share/elasticsearch/data directory inside the elasticsearch container:

$ docker run \  --name elasticsearch \  -e "discovery.type=single-node" \  -d \  -p 9200:9200 -p 9300:9300 \   -v esdata:/usr/share/elasticsearch/data \ docker.elastic.co/elasticsearch/elasticsearch-oss:6.2.4 ...

Get Building Enterprise JavaScript Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.