DEV Community

Alireza Razinejad
Alireza Razinejad

Posted on

Setting Up Elasticsearch with Docker: A Quick Guide

If you're looking to get started with Elasticsearch and want an easy way to set it up for development purposes, Docker is your friend. In this guide, we'll walk you through the steps of creating a Docker container with Elasticsearch and provide usage instructions.

Prerequisites

Before you start, make sure you have Docker installed on your system. You can download it from the official Docker website.

Creating the Docker Container

To set up Elasticsearch using Docker, follow these steps:

  1. Create a Dockerfile: Create a new file named Dockerfile in a directory of your choice.

  2. Paste the following content into the Dockerfile:

# Use the official Elasticsearch Docker image as the base image
FROM docker.elastic.co/elasticsearch/elasticsearch:7.15.0

# Expose the port that Elasticsearch listens on
EXPOSE 9200

# Set environment variables for Elasticsearch configuration
ENV discovery.type=single-node \
    network.host=0.0.0.0 \
    transport.host=0.0.0.0

# Provide a helpful comment for usage instructions
# To build the Docker image:
# docker build -t my-elasticsearch-image .
#
# To run the Docker container:
# docker run -d --name my-elasticsearch-container -p 9200:9200 my-elasticsearch-image
#
# The Elasticsearch server will be accessible at http://localhost:9200
# The container's name is 'my-elasticsearch-container'
# You can customize these values as needed.

# Start Elasticsearch when the container starts
CMD ["elasticsearch"]
Enter fullscreen mode Exit fullscreen mode
  1. Build the Docker Image: Open a terminal and navigate to the directory containing the Dockerfile. Run the following command:
   docker build -t my-elasticsearch-image .
Enter fullscreen mode Exit fullscreen mode
  1. Run the Docker Container: After the image is built, run a Docker container with the following command:
   docker run -d --name my-elasticsearch-container -p 9200:9200 my-elasticsearch-image
Enter fullscreen mode Exit fullscreen mode

Accessing Elasticsearch

Elasticsearch will now be running in the Docker container. You can access it by opening your web browser and navigating to http://localhost:9200. This URL will display Elasticsearch's API response.

Conclusion

Using Docker to set up Elasticsearch for development purposes is a quick and convenient way to get started. With a few simple commands, you can have Elasticsearch up and running in no time. Make sure to customize the container name and port as needed for your project.

Happy coding!

Top comments (0)