How to run a jar file from the open jdk docker image - java

I am trying to containerise an application using docker and the official openjdk image. From GitHub: https://github.com/jactor-rises/jactor-persistence/tree/feature-docker
I am trying to simulate the following jar command:
java -jar target/jactor-persistence-1.2.1-SNAPSHOT-app.jar
My Dockerfile:
FROM openjdk:13
LABEL jactor-rises="https://github.com/jactor-rises" \
email="..."
COPY target/jactor-persistence-*-app.jar /usr/src/myapp/app.jar
WORKDIR /usr/src/myapp
EXPOSE 1099
CMD [ "java -jar app.jar" ]

The application runs fine by changing the CMD line suggested by David Maze in his comment
CMD java -jar app.jar
No build error throw me off and I did not consider that this was a possible cause of the error...

Related

Why debug is not worked in java 11?

This is using my Dockerfile
FROM lpicanco/java11-alpine
VOLUME /tmp
ADD knetconfig /tmp/knetconfig
ADD grpc_health_probe_linux_x64 /app/grpc_health_probe_linux_x64
RUN chmod +x /app/grpc_health_probe_linux_x64
ADD aero-pay-core-service-latest.jar app.jar
RUN sh -c 'touch /app.jar'
EXPOSE 5005
ENV JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Djava.security.egd=file:/dev/./urandom"
ENTRYPOINT java $JAVA_OPTS -jar /app.jar
But it getting this error in building time,I am using java 11,
Error occurred during initialization of VM
Could not find agent library jdwp on the library path, with error: libjdwp.so: cannot open shared object file: No such file or directory
What would be the best possible options?
It may be that the alpine version of JDK11 is tuned to minimize the footprint.
Likely a debugging library was removed or never installed.

gu No such file or directory

I'm building a docker image out of a micronaut application.
But whenever I run the docker build command
docker build -f Dockerfile -t micronaut .
I get this error
/bin/sh: /bin/gu: No such file or directory
Here's the content of my docker file anyway:
FROM oracle/graalvm-ce:20.0.0-java11 as graalvm
RUN $GRAALVM_HOME/bin/gu install native-image
COPY . /home/app/micronautguide
WORKDIR /home/app/micronautguide
RUN $GRAALVM_HOME/bin/native-image --no-server -cp build/libs/complete-*-all.jar
FROM frolvlad/alpine-glibc
RUN apk update && apk add libstdc++
EXPOSE 8080
COPY --from=graalvm /home/app/micronautguide/micronautguide /micronautguide/micronautguide
ENTRYPOINT ["/micronautguide/micronautguide", "-Xmx68m"]
Though by running gu command from the terminal works.
Your context does not contain RUN $GRAALVM_HOME/bin/gu . What is $GRAAL_VM_HOME? Whatever this variable is, it is set as null. Also, $GRAALVM_HOME/bin/gu needs to be part of docker image.
$GRAALVM_HOME does not exists in the oracle/graalvm-ce:20.0.0-java11 container. Thus, make suer to specify the right path. In this case it is /bin/gu
I think $GRAALVM_HOME is a variable you use in the local machine and gu tool is already installed and available at /bin in the docker image. So I have kept the $GRAALVM_HOME unchanged in the second build stage.
So, the Dockerfile should be like below,
FROM oracle/graalvm-ce:20.0.0-java11 as graalvm
RUN /bin/gu install native-image
COPY . /home/app/micronautguide
WORKDIR /home/app/micronautguide
RUN /bin/native-image --no-server -cp build/libs/complete-*-all.jar
FROM frolvlad/alpine-glibc
RUN apk update && apk add libstdc++
EXPOSE 8080
COPY --from=graalvm /home/app/micronautguide/micronautguide /micronautguide/micronautguide
ENTRYPOINT ["/micronautguide/micronautguide", "-Xmx68m"]

Simple Docker container dies immediatelly after docker run command

I have a Dockerfile which looks like this:
FROM alpine:3.9
RUN apk add --update openjdk8
RUN mkdir /var/generator/
COPY generator.jar /var/generator
EXPOSE 8080
ENTRYPOINT [ "/bin/sh" ]
Dockerfile is inside generator/ folder. I am building it using:
docker build -t generator generator/
It builds successfully:
Successfully built 878e81f622cc
Successfully tagged generator:latest
but when I am trying to run this image with
docker run -d -p 8080:8080 generator
it dies immediately. docker logs gives no output.
What is wrong with my Dockerfile? Why is the container dying?
Try to run the JAR. Currently, it just runs sh command and exits. Make it something as below to run the JAR in foreground -
FROM alpine:3.9
RUN apk add --update openjdk8
RUN mkdir /var/generator/
COPY generator.jar /var/generator
EXPOSE 8080
ENTRYPOINT ["java","-jar","/var/generator/generator.jar"]
Beside your entrypoint is wrong (sh exits immediately) I would also recommend to start with an appropriate base image instead of starting with alpine and installing the openjdk package. Since you want to run a java application just use the JRE and not a full JDK and start the application as a foreground process.
Here's a minimal version which is also more efficient in disksize as the image will be smaller.
FROM openjdk:8-jre-alpine
COPY generator.jar /opt/generator.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/opt/generator.jar"]

How to run shell scripts along with java applications using docker?

Following is my Dockerfile , i am copying a folder and two java jar files onto the image and the intention was to execute the script file which is inside the copied directory followed by executing the two java applications .
FROM openjdk:latest
COPY ./mycluster /dir/latest/
COPY ./app1.jar /dir/latest/
COPY ./app2.jar /dir/latest/
CMD ["sh", "-c", "cd /dir/latest/ ;./local_cluster_startup.sh ./;sleep 5 ;java -jar app2.jar;sleep 5;java -jar app1.jar;"]
After building the docker image and running the following command docker container run tryout:nsdap the output is
sh: 1: cd: can't cd to /dir/latest/
sh: 1: ./local_cluster_startup.sh: not found
Error: Unable to access jarfile app2.jar
Error: Unable to access jarfile app1.jar
Can anyone please tell me what am doing wrong here ?
I am looking for a minimal docker image that would be able to execute java jars and the shell scripts . Can you please help me out ? Thanks
Use WORKDIR (https://docs.docker.com/engine/reference/builder/#workdir) to switch to the directory "/dir/latest".
Then you can run your commands from that directory
WORKDIR /dir/latest
CMD ["./local_cluster_startup.sh ;sleep 5 ;java -jar app2.jar;sleep 5;java -jar app1.jar;"]

Cannot run executable from Dockerfile

I have tried in various ways to run the following command:
shell form:
CMD java -jar ImageTester.jar -ml LAYOUT -k $APIKEY -f ../screenshots -p $PROXY -s $URL
I get the following error:
/bin/sh: 1: java: not found
exec form:
CMD [ "java", "-jar", "ImageTester.jar", "-ml LAYOUT -k $APIKEY -f ../screenshots -p $PROXY -s $URL" ]
I get this error:
container_linux.go:247: starting container process caused "exec: \"java\": executable file not found in $PATH"
My Dockerfile:
FROM node:8
RUN node --version
RUN npm install
RUN npm i puppeteer
CMD [ "java", "-jar", "ImageTester.jar", "-ml LAYOUT -k $APIKEY -f ../screenshots -p $PROXY -s $URL" ]
As your can expect, this works in my local. What am I missing?
Your Dockerfile should either have a base image gotten from https://hub.docker.com/_/openjdk/ or equivalent
or your Dockerfile needs to install java before it can call it...
Java is not part of the standard commands on a linux machine.
you are using node which does not contain java but nodejs which is JavaScript :-) not the same...
Normally though if you want a node application to call a java application in docker it is good practice to create a node image with the node application and a java image with the java application and let them talk to each other.
See for best practices this article https://docs.docker.com/v17.09/engine/userguide/eng-image/dockerfile_best-practices/

Categories