1

I am trying to setup a Dockerfile so that it has instructions to setup JAVA_HOME environment variable setup in the container when it starts. Then it install my node application. I need the Java environment for the xsd-schema-validator library. The library needs JAVA_HOME environment set in the container. This is what I have:

FROM openjdk:9

ENV JAVA_HOME  /usr/lib/jvm
RUN export JAVA_HOME

RUN echo $JAVA_HOME

FROM collinestes/docker-node-oracle:latest

# Create app directory
WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .
RUN npm run build

RUN npm run package

# Bundle app source
COPY . ./build

CMD [ "node", "bundle-app.js" ]

The node app seems to start up fine. I dont see any errors when docker container starts. However when I invoke the application it crashes with error javaError: spawn javac ENOENT

I think that is because the container does not have the JAVA_HOME variable set. I have done docker exec -i -t container bash and tried command echo $JAVA_HOME and it does not show the java executable. Additionally the java and javac commands do not work.

Does anyone know what I am doing wrong? Any help would be appreciated. Thanks in advance!

Aman Mohammed
  • 2,878
  • 5
  • 25
  • 39
  • 1
    Everything in the Dockerfile starts over again at each `FROM` line. You are building two separate images (one Java one, one Node one) and my guess is the second image doesn't have Java installed at all. You probably don't need to set `$JAVA_HOME` but you do need a JDK. – David Maze Nov 01 '19 at 10:39
  • agree with david, you need to install JDK in nodejs image, add this at before packag.json `RUN apt-get update && \ DEBIAN_FRONTEND=noninteractive \ apt-get -y install default-jre-headless && \ apt-get clean && \ rm -rf /var/lib/apt/lists/*` – Adiii Nov 01 '19 at 10:42
  • As previous commentators said you need an image with both java and node installed. Google's first result is [this image](https://hub.docker.com/r/timbru31/java-node/). You may use this or build a new one on your own. – leopal Nov 01 '19 at 10:55
  • @DavidMaze, thanks for that clarification. – Aman Mohammed Nov 01 '19 at 11:08

1 Answers1

0

As per multi-stage build:

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image.

Note: You are not even using any artifacts from the first stage for your final container, so you can even discard the first FROM stage...

You need to declare ENV JAVA_HOME /usr/lib/jvm in your final image too (i.e., put the line after in your latest FROM statement).

Furthermore, you need JDK and NodeJS installed together in your final image. You can install the JDK using your current base image (as it already has NodeJS on it), or use an image already prepared with it (e.g., something like this one).

Eduardo Baitello
  • 10,469
  • 7
  • 46
  • 74