0

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

Jake Lam
  • 3,254
  • 3
  • 25
  • 44
  • Quick question: do you really need docker to do NLTK? – alvas Sep 13 '17 at 09:22
  • i think yes because i need to deploy the app on my company server which will using the method of pulling image from dockerhub – Jake Lam Sep 14 '17 at 05:58
  • Interesting... If you would like to, please explain the requirements and ideal situation if NLTK is to host self-contained docker images on this issue: https://github.com/nltk/nltk/issues/542 – alvas Sep 14 '17 at 06:27

2 Answers2

2

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.

Nick Brady
  • 6,084
  • 1
  • 46
  • 71
  • He could even just include a "bootstrap" script in his Docker build that calls his python app and sets the environment variable – Niall Byrne Sep 13 '17 at 03:20
  • well the problem is i think i am not running the container. I just push it to dockerhub and use another app to pull the docker image back and deploy it to the company server. Therefore, i just can only modify the image before building it so I am seeking for a way allowing me to auto set java_home for the images – Jake Lam Sep 13 '17 at 06:56
  • @NickBrady even with the variable without mounting the actual binaries and the Stanford nlp directories onto docker, the code won't be able to access any of those resources. – alvas Sep 13 '17 at 09:21
0

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
Jake Lam
  • 3,254
  • 3
  • 25
  • 44