styfle
styfle

Reputation: 24610

How to run elasticsearch via docker compose or swarm mode and install plugin with command

Problem Statement

I have a docker-compose.yml file (v3) that looks like the following:

version: '3'

services:
    elastic:
        restart: always
        image: elasticsearch:2.3.1
        command: ["sh", "-c", "./bin/plugin install delete-by-query && ./bin/elasticsearch"]
        volumes:
            - /home/styfle/esdata:/usr/share/elasticsearch/data
        ports:
            - 9200:9200
    kibana:
        restart: always
        image: kibana:4.5.4
        ports:
            - 5601:5601
        links:
            - elastic:elasticsearch

When I run docker-compose up elastic it appears that the plugin installed correctly, but I get the message "don't run elasticsearch as root".

Creating dev_elastic_1 ... done
Attaching to dev_elastic_1
elastic_1  | -> Installing delete-by-query...
elastic_1  | Trying https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/delete-by-query/2.3.1/delete-by-query-2.3.1.zip ...
elastic_1  | Downloading ..DONE
elastic_1  | Verifying https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/delete-by-query/2.3.1/delete-by-query-2.3.1.zip checksums if available ...
elastic_1  | Downloading .DONE
elastic_1  | Installed delete-by-query into /usr/share/elasticsearch/plugins/delete-by-query
elastic_1  | Exception in thread "main" java.lang.RuntimeException: don't run elasticsearch as root.
elastic_1  |    at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:93)
elastic_1  |    at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:144)
elastic_1  |    at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:270)
elastic_1  |    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:35)
elastic_1  | Refer to the log for complete error details.
dev_elastic_1 exited with code 74

Question

How can I install the plugin and run as the elasticsearch user instead of root user?

Upvotes: 1

Views: 2652

Answers (1)

Samit
Samit

Reputation: 615

As per the docker-compose architecture and cleanup policies, you cannot run a docker-compose command to initiate a subshell. You can do some bash and docker changes in your current docker-compose.yml file as below:

version: '3'

services:
    elastic:
        restart: always
        image: elasticsearch:2.3.1
        user: ${MY_USER_ID}
        command: ["sh", "-c", "./bin/plugin install delete-by-query && ./bin/elasticsearch"]
        volumes:
            - /home/styfle/esdata:/usr/share/elasticsearch/data
        ports:
            - 9200:9200
    kibana:
        restart: always
        user: ${MY_USER_ID}
        image: kibana:4.5.4
        ports: 
            - 5601:5601
        links:
            - elastic:elasticsearch

I have added a line user: ${MY_USER_ID} in the above docker-compose.yml file. After this, you need to use the below command to spin up the containers and start elasticsearch:

MY_USER_ID=$(id -u):$(id -g) docker-compose up elastic

Test it and let me know the feedback.

Upvotes: 3

Related Questions