简体   繁体   中英

How to locate openjdk in Docker container?

I tried to run the pyspark application.For this first I installed pyspark from pip then pulled openjdk:8 to set the JAVA_HOME variable

Dockerfile :

FROM python:3

ADD my_script.py /
COPY requirements.txt ./

ENV JAVA_HOME  /usr/lib/jvm/java-8-openjdk-amd64/

RUN pip install --no-cache-dir -r requirements.txt

CMD [ "python", "./my_script.py" ]

my_script.py :

from pyspark import SparkContext
from pyspark import SparkConf

#spark conf
conf1 = SparkConf()
conf1.setMaster("local[*]")
conf1.setAppName('hamza')
print(conf1)
sc = SparkContext(conf = conf1)


from pyspark.sql import SQLContext
sqlContext = SQLContext(sc)
print(sqlContext)

Requirements.txt :

pyspark

numpy

Getting this error :

C:\Users\hrafiq\Desktop\sample>docker run -it --rm --name data2 my-python-app
<pyspark.conf.SparkConf object at 0x7f4bd933ba58>
/usr/local/lib/python3.7/site-packages/pyspark/bin/spark-class: line 71: 
/usr/lib/jvm/java-8-openjdk-amd64//bin/java: No such file or directory
Traceback (most recent call last):
   File "./my_script.py", line 14, in <module>
    sc = SparkContext(conf = conf1)
  File "/usr/local/lib/python3.7/site-packages/pyspark/context.py", line 115, in __init__
    SparkContext._ensure_initialized(self, gateway=gateway, conf=conf)
  File "/usr/local/lib/python3.7/site-packages/pyspark/context.py", line 298, in _ensure_initialized
    SparkContext._gateway = gateway or launch_gateway(conf)
  File "/usr/local/lib/python3.7/site-packages/pyspark/java_gateway.py", line 94, in launch_gateway
    raise Exception("Java gateway process exited before sending its port number")
Exception: Java gateway process exited before sending its port number  

So the question is If is not finding the java file then how will I find the that file? I know it is stored in some virtual hard disk which we dont have any access.

Any help would be appreciated

Thanks

Setting the JAVA_HOME env var is not enough. You need to actually install openjdk inside your docker image.

Your base image (python:3) is itself based on a Debian Stretch image. So you can use apt-get install to fetch the JDK :

FROM python:3

RUN apt-get update && \
     apt-get install -y openjdk-8-jdk-headless && \
    rm -rf /var/lib/apt/lists/*
ENV JAVA_HOME  /usr/lib/jvm/java-8-openjdk-amd64/

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY my_script.py ./
CMD [ "python", "./my_script.py" ]

(In the above I have optimized the layers ordering so that you won't need to re-build the pip install layer when only your script changes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM