title: Unable to start elastic search as it cannot find JAVA_HOME
body: <p>I am using docker compose to spin two elastic search services. so I am having a file named elasticsearch.yml . I execute the file using the command (docker-compose -f src/main/docker/elasticsearch.yml up). However when i run, the second service  elasticsearch2 I am getting an error .</p>
  
  <pre><code>Building elasticsearch2
  Step 1/5 : FROM docker.elastic.co/elasticsearch/elasticsearch:6.6.1
   ---&gt; c6ffcb0ee97e
  Step 2/5 : ADD elasticsearch/scripts/initializeElasticSearch.sh /usr/local/bin/initializeElasticSearch
   ---&gt; 8bd9e8821c4e
  Step 3/5 : RUN chmod 755 /usr/local/bin/initializeElasticSearch
   ---&gt; Running in 71184d2e0af6
  Removing intermediate container 71184d2e0af6
   ---&gt; bc015460473c
  Step 4/5 : ADD elasticsearch/initial-data/cars.json  /tmp/cars.json
   ---&gt; b7140ea04924
  Step 5/5 : ENTRYPOINT ["initializeElasticSearch"]
   ---&gt; Running in f29015d87c1d
  Removing intermediate container f29015d87c1d
   ---&gt; 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
  </code></pre>
  
  <p>elasticsearch.yml</p>
  
  <pre><code>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:
  </code></pre>
  
  <p>the second elastic search container is build using a context and path with a docker file (elasticsearch-Migration.Dockerfile). so this my docker file</p>
  
  <pre><code>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"]
  </code></pre>
  
  <p>my initializeElasticSearch.sh  is as follows.</p>
  
  <pre><code>#!/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
  </code></pre>
  
  <p>any idea how can i solve the issue of not find the java </p>
  
  <p>thank you  </p>