Add some default data to ES when docker first run

Dear all,
I am new to ES. Now I want to create a docker to run ES, but I faced some errors. Firstly, I can create a docker to run ES and can interact with it. But I want to add some default data to index when docker first run, like "hello world". I tried many times but still get error. Can u help me?
My docker-compose.yml file

  elasticsearch:
    container_name: elasticsearch-container
    image: elasticsearch-image
    restart: always
    build:
      context: ./ElasticSearch
      dockerfile: Dockerfile
      args:
        - ELASTIC_VERSION=${ELASTIC_VERSION}
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=true
      - ELASTIC_PASSWORD=${ELASTIC_PASSWORD}
    ports:
      - ${ELASTIC_PORT}:9200
      - ${ELASTIC_X_PORT}:9300
    volumes:
      - elasticsearch_data:/db/data
    env_file:
      - .env

my .env file

ELASTIC_USERNAME=elastic # default `elastic`
ELASTIC_PASSWORD=trantruongmmcii
ELASTIC_PORT=9201
ELASTIC_X_PORT=9301
ELASTIC_VERSION=8.8.1

my dockerfile

ARG ELASTIC_VERSION
FROM docker.elastic.co/elasticsearch/elasticsearch:${ELASTIC_VERSION}

USER root

COPY insert_data.sh /usr/share/elasticsearch/

RUN chmod +x /usr/share/elasticsearch/insert_data.sh

RUN sed -i -e 's/\r$//' /usr/share/elasticsearch/insert_data.sh

CMD ["/bin/bash", "-c", "/usr/share/elasticsearch/insert_data.sh"]
EXPOSE 9200

my insert_data.sh

#!/bin/sh

while true; do
  curl -sS -X GET -u ${ELASTIC_USERNAME}:${ELASTIC_PASSWORD} "http://localhost:9200/_cluster/health" | grep -q '"status":"green"'
  if [ $? -eq 0 ]; then
    break
  fi
  sleep 1
done

curl -X POST -u ${ELASTIC_USERNAME}:${ELASTIC_PASSWORD} "http://localhost:9200/my_index/_doc" -H 'Content-Type: application/json' -d '{"field": "value"}'

can you help me to edit to add some data when first run the docker, please

Hi @TranTruongMMCII and welcome to the community!

What errors are you receiving when you try to add your data this way?

By default, Elastic > 8.0 is coming with security enabled, plus you're using - xpack.security.enabled=true which changes the base security as well.

Since your CURL commands are not https with a cert or credentials, I would expect them to fail.

I wrote a small blog about getting started with Elastic Stack on Docker which also gives the ability to add data via Filbeat and/or Logstash. Take a look and see if it helps at all: Getting started with the Elastic Stack and Docker-Compose | Elastic Blog

Thank you sir, I will read you blog now.
The error I got curl: (7) Failed to connect to localhost port 9201: Connection refused

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.