如何通过docker compose(docker堆栈)运行Elasticsearch并安装按查询删除插件

时间:2018-08-24 17:59:36

标签: bash docker elasticsearch docker-compose sudo

问题陈述

我有一个docker-compose.yml文件(v3),如下所示:

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

当我运行docker-compose up elastic时,似乎插件已正确安装,但是我收到消息“不要以root身份运行elasticsearch”。

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

问题

如何安装插件并以elasticsearch用户而不是root用户身份运行?

1 个答案:

答案 0 :(得分:1)

根据docker-compose体系结构和清理策略,您无法运行docker-compose命令来启动子外壳。 您可以在当前docker-compose.yml文件中进行一些bash和docker更改,如下所示:

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

我在上述user: ${MY_USER_ID}文件中添加了一行docker-compose.yml。之后,您需要使用以下命令启动容器并启动elasticsearch:

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

测试一下,让我知道反馈。