23

I am going to use official Elasticsearch image. What I want to do is set up the mapping of indexes during Docker run.

So I need to execute right after the container is started.

curl -XPUT localhost:9200/_template/http_request -d {.....}

So I can not do it in Dockerfile or can I?

Thank you

Vladislav Kysliy
  • 3,488
  • 3
  • 32
  • 44
Thor Samsberg
  • 2,219
  • 5
  • 22
  • 30

1 Answers1

9

I ended up doing this

docker-composer.yml

version: '2'
services:
  elasticsearch:
    image: elasticsearch:2.3
    command: elasticsearch -Des.network.host=0.0.0.0
    ports:
      - "9200:9200"
      - "9300:9300"
  elasticsearch-mapping-init:
    build: elasticsearch-mapping-init
    links:
      - elasticsearch
    depends_on:
      - elasticsearch

and here is my elasticsearch-mapping-init/Dockerfile:

FROM ubuntu

# Install packages
RUN apt-get update && \
apt-get install -y curl

COPY docker-entrypoint.sh /

ENTRYPOINT ["/docker-entrypoint.sh"]

and here is my elasticsearch-mapping-init/docker-entrypoint.sh

#!/bin/bash

for i in {30..0}; do
    if curl elasticsearch:9200; then
        curl -XPUT elasticsearch:9200/_template/log -d '
            {
                "template" : "log-*",
                "settings": {
                   "number_of_shards": 1
                },
                "mappings" : {


                }
            }';
            break;
    fi
    sleep 2
done

I believe this is not perfect, and I'm still looking for a better solution.

Thor Samsberg
  • 2,219
  • 5
  • 22
  • 30
  • 2
    You can do all that in the single official elasticsearch image. You can add to their docker-entrypoint.sh in your own dockerfile with FROM elasticsearch. Here's what they do now for latest es: https://github.com/docker-library/elasticsearch/blob/ffaaf3283e47dcb732e90288a58757b87a8439a7/5/docker-entrypoint.sh – Bret Fisher Feb 07 '17 at 01:29
  • @BretFisher Could you give me more infomation with it? I still don't know how to do within official ES image. – J.Done Feb 27 '17 at 06:57
  • It might not be perfect answer since 'depend_on' will not wait for elasticsearch service to be ready. It will be wait until elasticsearch has been started. Therefore init-service could be started while elasticsearch is lunching, but not completely run yet. – J.Done Feb 27 '17 at 07:04
  • 2
    so you would replace the docker-entrypoint.sh in official es. It would start es up, run your curl, then stop it, then finish by passing in CMD. This repo does the same for postgres where it creates users/replication/db's, then stops and passes exec at end. look at their docker-entrypoint.sh and runtime/functions file https://github.com/sameersbn/docker-postgresql – Bret Fisher Feb 27 '17 at 08:34
  • 1
    Here's the entrypoint script that's in the es official 2.4. you'll want to, at bottom before exec, 1. start es, 2. run your script above, 3. stop es. https://github.com/docker-library/elasticsearch/blob/8f00ad520c1c33b879a715700905d3c25c526331/2.4/docker-entrypoint.sh – Bret Fisher Feb 27 '17 at 08:36
  • Is there by chance an updated method / answer to this problem? The solution provided seems kluge. – Bryce Sep 07 '17 at 14:58
  • Still a bit awkward but some better answers here: https://stackoverflow.com/questions/35526532/how-to-add-an-elasticsearch-index-during-docker-build – Carasel Sep 30 '19 at 09:23