Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set up mapping in Elasticsearch during Docker run

Tags:

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

like image 786
Albert Tobac Avatar asked Aug 17 '16 16:08

Albert Tobac


People also ask

Can I do read only mapping in docker?

The readonly option, if present, causes the bind mount to be mounted into the container as read-only. May be specified as readonly or ro . The volume-opt option, which can be specified more than once, takes a key-value pair consisting of the option name and its value.


1 Answers

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.

like image 123
Albert Tobac Avatar answered Sep 23 '22 12:09

Albert Tobac