Set JAVA_HOME for docker in NLTK for Stanford NLP

967 Views Asked by At

I am a beginner in using Docker. I'm using Docker toolbox for Windows 7 , i built an image for my python web app and everything works fine.

However, for this app, i use nltk module which also needs java and java_home setting to the java file. When running on my computer, i can mannualy set the java_home, but how to do it in the dockerfile so that it wont get error when running on another machine. Here is my error :

My Error

p.s : Answer below

2

There are 2 best solutions below

3
On

When you are running a container you have the option of passing in environment variables that will be set in your container using the -e flag. This answer explains environment variables nicely: How do I pass environment variables to Docker containers?

docker container run -e JAVA_HOME='/path/to/java' <your image>

Make sure your image actually contains Java as well. You might want to look at something like the openjdk:8 image on docker hub.

It sounds like you need a docker file to build your image. Have a look at the ENV command documented here to set the JAVA_HOME var: https://docs.docker.com/engine/reference/builder/#env and then build your image with docker build /path/to/Dockerfile

I see you've already tried that and didn't have much luck.. run the container and instead of running your application process just run a bash script along the lines of echo $JAVA_HOME so you can at least verify that part is working.

Also make sure you copy in whatever files/binaries needed to the appropriate directories within the image in the docker file as noted below.

0
On

i finally found the way to install the java for dockerfile , it is use the java install commandline of ubuntu image. Below is the docker file . Thanks for your reading.

RUN apt-get update
RUN apt-get install -y python-software-properties
RUN apt-get install -y software-properties-common
RUN add-apt-repository -y ppa:openjdk-r/ppa

RUN apt-get update
RUN apt-get install -y openjdk-8-jdk

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