Running Elasticsearch in Docker

Pulse - Elasticsearch Operations Done Right

On this page

Prerequisites Step 1: Pull the Elasticsearch Docker Image Step 2: Run Elasticsearch as a Docker Container Step 3: Verify Elasticsearch is Running Step 4: Running Elasticsearch with Docker Compose Step 5: Managing and Stopping Elasticsearch Additional Considerations

Elasticsearch is a powerful search and analytics engine, and running it in Docker makes deployment and scaling much easier. This guide will walk you through setting up Elasticsearch in a Docker environment, whether you're testing locally or preparing for production.

Prerequisites

Before getting started, ensure you have:

  • Docker installed on your system (Download Docker)
  • At least 2GB of RAM available (recommended for Elasticsearch)
  • Basic knowledge of Docker commands

Step 1: Pull the Elasticsearch Docker Image

First, pull the official Elasticsearch image from Docker Hub:

docker pull docker.elastic.co/elasticsearch/elasticsearch:8.5.1

Replace 8.5.1 with the latest version available if needed.

Step 2: Run Elasticsearch as a Docker Container

To quickly spin up an Elasticsearch instance, use the following command:

docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:8.5.1

Explanation:

  • -d: Runs the container in detached mode
  • --name elasticsearch: Assigns a name to the container
  • -p 9200:9200: Maps port 9200 (Elasticsearch API) to the host machine
  • -p 9300:9300: Maps port 9300 (for internal cluster communication)
  • -e "discovery.type=single-node": Runs in single-node mode for local testing

Step 3: Verify Elasticsearch is Running

Check if the container is running:

docker ps

You should see elasticsearch in the list of running containers. Now, test the Elasticsearch instance by sending a request using curl:

curl -X GET "http://localhost:9200/"

You should receive a JSON response with details about your Elasticsearch instance.

Step 4: Running Elasticsearch with Docker Compose

For a more structured setup, use Docker Compose. Create a docker-compose.yml file:

version: '3.7'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.5.1
    container_name: elasticsearch
    environment:
      - discovery.type=single-node
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ports:
      - "9200:9200"
      - "9300:9300"
    ulimits:
      memlock:
        soft: -1
        hard: -1

Then, start Elasticsearch using:

docker-compose up -d

Step 5: Managing and Stopping Elasticsearch

To stop the container:

docker stop elasticsearch

To remove the container:

docker rm elasticsearch

For Docker Compose:

docker-compose down

Additional Considerations

Persistent Storage

By default, data stored inside a Docker container is lost when the container stops. To persist data, mount a volume:

volumes:
  - elasticsearch-data:/usr/share/elasticsearch/data

Security

  • Use authentication and TLS in production environments.
  • Configure user roles with Elasticsearch’s built-in security features.

Subscribe to the Pulse Newsletter

Get early access to new Pulse features, insightful blogs & exclusive events , webinars, and workshops.