无法启动弹性搜索,因为找不到JAVA_HOME

时间:2019-08-21 21:57:29

标签: docker elasticsearch docker-compose dockerfile

我正在使用docker compose旋转两个弹性搜索服务。所以我有一个名为elasticsearch.yml的文件。我使用命令执行文件(docker-compose -f src / main / docker / elasticsearch.yml up)。但是,当我运行时,第二个服务elasticsearch2却出现错误。

Building elasticsearch2
Step 1/5 : FROM docker.elastic.co/elasticsearch/elasticsearch:6.6.1
 ---> c6ffcb0ee97e
Step 2/5 : ADD elasticsearch/scripts/initializeElasticSearch.sh /usr/local/bin/initializeElasticSearch
 ---> 8bd9e8821c4e
Step 3/5 : RUN chmod 755 /usr/local/bin/initializeElasticSearch
 ---> Running in 71184d2e0af6
Removing intermediate container 71184d2e0af6
 ---> bc015460473c
Step 4/5 : ADD elasticsearch/initial-data/cars.json  /tmp/cars.json
 ---> b7140ea04924
Step 5/5 : ENTRYPOINT ["initializeElasticSearch"]
 ---> Running in f29015d87c1d
Removing intermediate container f29015d87c1d
 ---> 5faf42e80d33
Successfully built 5faf42e80d33
Successfully tagged docker_elasticsearch2:latest
WARNING: Image for service elasticsearch2 was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Starting elasticsearch  ... done
Creating elasticsearch2 ... done
Attaching to elasticsearch, elasticsearch2
elasticsearch2    | /opt/jdk-11.0.1
elasticsearch2    | which: no java in (/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/share/elasticsearch/.local/bin:/usr/share/elasticsearch/bin)
elasticsearch2    | could not find java; set JAVA_HOME or ensure java is in PATH
elasticsearch     | OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
elasticsearch2    | 
elasticsearch2    |   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
elasticsearch2    |                                  Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed connect to 0.0.0.0:9200; Connection refused

elasticsearch.yml

version: '2.0'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.6.1
    container_name: elasticsearch
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata1:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - esnet
  elasticsearch2:
    build:
        context: .
        dockerfile: elasticsearch/elasticsearch-Migration.Dockerfile
    container_name: elasticsearch2
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "discovery.zen.ping.unicast.hosts=elasticsearch"
      - network.host=0.0.0.0
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata2:/usr/share/elasticsearch/data
    networks:
      - esnet

volumes:
  esdata1:
    driver: local
  esdata2:
    driver: local

networks:
  esnet:

第二个弹性搜索容器是使用上下文和带有docker文件的路径构建的(elasticsearch-Migration.Dockerfile)。所以这是我的docker文件

FROM docker.elastic.co/elasticsearch/elasticsearch:6.6.1

# script to orchestrate the automatic index creation.

ADD elasticsearch/scripts/initializeElasticSearch.sh /usr/local/bin/initializeElasticSearch
RUN chmod 755 /usr/local/bin/initializeElasticSearch

# 
ADD elasticsearch/initial-data/cars.json  /tmp/cars.json


ENTRYPOINT ["initializeElasticSearch"]

我的initializeElasticSearch.sh如下。

#!/usr/bin/env bash
export JAVA_HOME=/opt/jdk-11.0.1

echo $JAVA_HOME

su - elasticsearch -c 'bash /usr/share/elasticsearch/bin/elasticsearch' 

sleep 5
echo 
curl -XPOST http://0.0.0.0:9200/cars/car/_bulk --data-binary  @/tmp/cars.json
sleep 5

任何想法我该如何解决找不到Java的问题

谢谢

1 个答案:

答案 0 :(得分:0)

您在export JAVA_HOME=/opt/jdk-11.0.1中进行了initializeElasticSearch.sh,但是最后在执行elasticsearch时使用su -,这意味着您不再在运行的shell中运行elasticsearch导出JAVA_HOME。

要修复此问题,您需要将JAVA_HOME添加到真正执行elasticsearch的外壳中,如下所示:

su - elasticsearch -c 'bash -c "export JAVA_HOME=/opt/jdk-11.0.1; /usr/share/elasticsearch/bin/elasticsearch"'