Java command not working in docker container - java

I am trying to run java with docker image in gitlab.
Here is my docker file.
FROM java:latest
FROM perl
COPY . /
ENTRYPOINT ["/usr/bin/perl", "/myapp_entrypoint.pl"]
I was able to build docker image successfully and run perl commands but java commands are not working.
My application is a linux application and I am running 'java -version'. I am not getting any output completely blank output for version command.
What would be the issue? Do I need to add anything related linux as I am running 'java -version' as linux command?

You don't specify what OS you're running in your container, but the main issue is that you're blowing away your Java layer with another FROM directive.
From the documentation, emphasis mine:
Each FROM instruction clears any state created by previous instructions.
So I'd espouse a solution in which I install Perl (if I really needed to) after having my base Java image.
However, if you use the base OpenJDK images, Perl comes preinstalled, so that will simplify your Dockerfile significantly.
FROM openjdk:latest
COPY . /
ENTRYPOINT ["/usr/bin/perl", "/myapp_entrypoint.pl"]

Related

Can we create native image from inside a graalvm docker and run it anywhere?

We are new to Graalvm and are building a simple Java command line app. The jar runs fine in jvm mode (java -jar <file>.jar). We don't install Graalvm on our machines (yet) so we tried to create the native image from inside Docker container which runs Graalvm, the Dockerfile is this simple:
FROM ghcr.io/graalvm/native-image:ol8-java17
COPY . /home/app
WORKDIR /home/app
RUN ./mvnw clean package
RUN native-image -cp target/appj-1.0.0.jar "org.nqm.Appj"
RUN mv org.nqm.Appj appj
RUN chmod +x appj
The docker image was successfully built.
Then we tried to access the container (docker run --rm --name dkappj -it --entrypoint bash dkappj) to collect the native image file. From inside the docker container, we could run it with ./appj without any errors.
However, when we copied the native image file to the host machine and run it with ./appj it produces an error:
zsh: exec format error: ./appj
So my question is can we create native image from inside a graalvm docker container, copy it and run it anywhere? Or we have to install Graalvm on our host machine to be able to execute the native image? Because I thought graalvm native image is actually the machine code (like the apps created from C language)
Edit:
when I replicated the whole code base on a new computer which runs the same os (ubuntu 20.04) the native image runs fine on that host machine.
(answer to my own question)
Can we create native image from inside a graalvm docker and run it anywhere?
The answer is YES!
For some specific reasons I am now able to build the native image file inside docker container and run it.
I think you can by doing this way, https://github.com/swseighman/Spring-GraalVM-REST-Example/blob/main/src/main/resources/containers/Dockerfile.stage
I tried doing the same and it did not work as you got to use same architecture to build and run the image. Linux built image would run on Linux and so as macOS built on mac. I use builtpacks and it give you OCI image which runs perfectly fine.

Why do we need FROM in dockerfile for a java app?

I have a very basic question about FROM line in dockerfile for a java application, like a spring boot app.
So we write it like:
FROM openjdk:11
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
In line 3 we are basically telling docker to use the application jar inside target folder for running the app inside container. Now when a jar is build for a java application, build process takes into account all the needed dependencies like those from jdk or those defined in POM etc.
So then why do we need FROM line? We already have all in our jar, isn't?
The FROM argument describes a base image to use. https://docs.docker.com/engine/reference/builder/#from
An analogy in a non containerized world would be sitting down to a computer freshly installed with some OS and various dependencies already setup for you. Handy!
In this specific case, you are getting an image from OpenJDK. https://hub.docker.com/_/openjdk.
Edit to add detail: As a number of comments state, Java needs the JVM installed on some OS to run a Java program. If you look down at image variants in the docker hub link, you'll see that you are requesting an image with Debian Linux and OpenJDK 11.

How to install Jar (java) to NVIDIA-Docker image or NVIDIA-Docker dependencies to Java image?

I have to bundle up my system using Docker. But my system uses Java (JAR file to run) and python with PyTorch. I initially tried to use openjdk:buster base Docker image and then installed python3 on top of it. So both JAR and PyTorch worked, but PyTorch is only CPU supportive. But now I have to speed-up my PyTorch code using GPU, and for that I need NVIDIA-Cuda. In a separate Docker, I found nvidia/cuda:10.2-base-ubuntu18.04 works for my PyTorch. But this Docker can't run JAR file.
So I am stuck in combining these 2. I either want to
install NVIDIA-Cuda dependencies to openjdk Docker base image
install openjdk (openjdk-14) dependencies to NVIDIA-Cuda Docker base image
Anyone has any suggestions on how I can do that or any alternative hacks ?
You can have a single image, instead of two by creating your own docker image that uses the nvidia image, and install java on it. I.e. have a Dockerfile as below
FROM nvidia/cuda:10.2-base-ubuntu18.04
RUN apt-get update
RUN apt-get install openjdk-14-jdk
COPY <your jar file> <a path>
CMD [ "java" "other java flags/args>" "-jar" "<path to your jar file>"]
run docker build on that Dockerfile, and docker run as you normally would, and your java code should have access to NVIDIA-Cuda. (Also note, some prefer ENTRYPOINT to CMD)

How to execute Carrot2 Document Clustering server

I downloaded the Carrot2 Document clustering server build 3.15.0 for Mac. The read me file says:
The DCS requires a Java Runtime Environment (JRE) version 1.7.0 or later. To
run the DCS, execute the 'dcs' script and point your browser at
http://localhost:8080 for further instructions.
Mac OS Sierra doesn't make it easy, but I got 1.8.0_112 installed.
The problem is that I don't know how to execute the 'dcs' script.
There are .cmd, .sh, .war, and .jar files. I wasn't sure which of those to work with. I thought .jar looked promising, so I followed some of this thread and tried this in a terminal window:
java -jar invoker.jar
I cd-ed to the correct directory, but it just says Provide main class, but I'm not sure what or where that is.
Can anybody provide instructions or a link to how to do this?
Use the dcs.sh (on Linux/Mac) and dcs.cmd (on Windows) to start the server. The scripts will set some extra options for the JVM and then start the DCS. In case of any problems, append the -v option to see diagnostic output.

Trying to install zgrviewer to view DOT files

I am trying to install zgrviewer on my Ubuntu machine,to view DOT files. I have already installed java jdk 1.6 and Graphviz (sudo apt-get install graphviz).
The java installation path i notice after typing "which javac" is /usr/bin/javac.
I checkout the zgrviewer from sourceforge.net:
svn co https://zvtm.svn.sourceforge.net/svnroot/zvtm/zgrviewer/trunk zgrviewer
I am supposed to launch zgr viewer by running the run.sh script. The contents of the run.sh script is:
#!/bin/sh
# If you want to be able to run ZGRViewer from any directory,
# set ZGRV_HOME to the absolute path of ZGRViewer's main directory
# e.g. ZGRV_HOME=/usr/local/zgrviewer
ZGRV_HOME=/usr/local/zgrviewer
java -Xmx1024M -Xms512M -jar $ZGRV_HOME/target/zgrviewer-0.9.0-SNAPSHOT.jar "$#"
I am not sure how to edit this script to point to a specific Java Virtual Machine ; right now, it just says java and therefore uses the first JVM it finds in my PATH.
so when i run the script it says: Unable to access jarfile /usr/local/zgrviewer/target/zgrviewer-0.9.0-SNAPSHOT.jar
Please help me install zgrviewer successfully.
I like graphviz a lot, but I eventually gave up on the native "dot" viewers. Instead, I build (or obtain) graphviz with pdf support, and translate .dot to pdf. From there, many PDF viewers work well (and they tend to be more polished than dot viewers, probably because the need is more common), even for large documents. I'm mostly a gnome 2.x person, but I find KDE's okular to be the best PDF viewer I've encountered for large PDF's so far.
If this can help, I've written a guide on how to install a Graphviz viewer (ZGRViewer) and thumbnailer on Ubuntu/Gnome.
It is available at http://bernaerts.dyndns.org/linux/74-ubuntu/287-ubuntu-graphviz-viewer-gnome-thumbnailer
I've been able to use ZGRViewer 0.8.2 without any problem, but no success till date with version 0.9.0.

Categories

Resources