Cannot run executable from Dockerfile - java

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/

Related

Can't run docker image

First, sorry if my question sounds too easy or silly. I'm new to docker.
I have created my docker image and passed several jar files which are to be run immediately when the container starts.
I want to run the script "serve.sh" immediately when the container starts
I succeeded in creating the images well, but when I run the container, it throws me this error:
C:\Program Files\Docker\Docker\resources\bin\docker.exe: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"-it\": executable file not found in $PATH": unknown.
Here is the command I use to run the image I craeted:
docker run b24b37614e1a -it
Here is my docker file:
FROM openjdk:8-jdk-alpine
EXPOSE 8080:8080
COPY apigateway-0.0.1-SNAPSHOT.jar apigateway.jar
COPY authservice-0.0.1-SNAPSHOT.jar authservice.jar
COPY institutionsservice-0.0.1-SNAPSHOT.jar institutionsservice.jar
COPY messagesservice-0.0.1-SNAPSHOT.jar messagesservice.jar
COPY postsservice-0.0.1-SNAPSHOT.jar postsservice.jar
COPY userservice-0.0.1-SNAPSHOT.jar userservice.jar
COPY serve.sh serve.sh
CMD [ "bash" "./serve.sh" ]
Please what am I doing wrong ? I'm new to docker
There are a couple of things which you should correct, First one is the CMD format which should be
CMD instruction has three forms:
CMD ["executable","param1","param2"] (exec form, this is the preferred form)
CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
CMD command param1 param2 (shell form)
CMD [ "/bin/bash" , "./serve.sh" ]
Another thing, When you do docker run, the instructions are
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
which means all the options has to be before IMAGE and in your case it is appearing after IMAGE.
The correct command should be
docker run -it b24b37614e1a
BTW, small question, why you want to run an interactive container of this application. Ideally, it should be something like
docker run -p $HOST_PORT:$APP_PORT b24b37614e1a
-p => Publish a container's port(s) to the host
and then you can access your application localhost:$HOST_PORT or machine_IP:$HOST_PORT
Keep in mind that docker args order matters:
you wrote docker run b24b37614e1a -it which is different from
docker run -it b24b37614e1a
Hope it solves your problem :)
tl;dr
docker run -it then args (c/o Luca Fabbian)
You don't have bash. Use sh. (c/o MC Emperor)
CMD exec form needs an array, so use commas: CMD ["sh", "./serve.sh"]
CMD has a shell form if you forget syntax easily: CMD ./serve.sh
Don't even exec the shell if you don't have to: CMD ["./serve.sh"]
Argument order matters
This assumes you already took Luca's advice:
Keep in mind that docker args order matters:
you wrote docker run b24b37614e1a -it which is different from docker run -it b24b37614e1a
You don't have bash.
I pulled the openjdk:8-jdk-alpine image to confirm, and it does not come with bash. Alpine images come with sh so the code provided will never work unless you correct the shell used or install bash.
"unknown operand"
From your response to nischay:
I get this error: "sh: ./serve.sh: unknown operand"
I updated My CMD instruction to: CMD [ "bin/bash" "./serve.sh" ]
CMD in the exec form takes an array of instructions. These must be separated by a comma.
Do this:
CMD [ "executable", "param1" ]
^
A note on using CMD
As nischay said, there are a few different ways to use CMD to do roughly the same thing, from the reference quoted.
You can in fact use the exec form of CMD to say things like:
CMD [ "sh", "./serve.sh" ]
Typing out /bin/sh or /bin/bash or whichever shell you want is not required unless desired. But if you don't need the shell, you can use exec form without the shell as well:
CMD [ "./serve.sh" ]

Cannot run my Java 8 application from docker container

I can properly launch this Java 8 application using this command from a bash shell:
java -cp "simple-queue-0.1-SNAPSHOT.jar:jms-1.1.jar:commons-logging-1.2.jar:activemq-all-5.13.3.jar"
-Dserver1="my1.domain.com"
-Dserver2="my2.domain.com"
-Dusername="user"
-Dpassword="passwd"
com.fusesource.activemq.exercises.simple.queue.SimpleProducer
I want to containerize this application, so here is my Dockerfile:
FROM store/oracle/serverjre:8
MAINTAINER <me#myco.com>
EXPOSE 4567
VOLUME /data
COPY build/libs/*.jar /usr/local/bin/
COPY /app/simple-queue-0.1-SNAPSHOT.jar /usr/local/bin/
CMD ["java", "-cp", "/usr/local/bin/simple-queue-0.1-SNAPSHOT.jar:/usr/local/bin/jms-1.1.jar:/usr/local/bin/commons-logging-1.2.jar:/usr/local/bin/activemq-all-5.13.3.jar", "-Dserver1=$SERVER1", -Dserver2="$SERVER2", -Dusername="$USER", -Dpassword="$PASSWORD"]
I start my container like this:
docker run -it --rm -e SERVER1=my1.domain.com -e SERVER2=my2.domain.com -e USER=user -e PASSWORD=passwd ecosystem/simple-queue-client:1.1 com.fusesource.activemq.exercises.simple.queue.SimpleProducer
And I get this error message:
docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"com.fusesource.activemq.exercises.simple.queue.SimpleProducer\": executable file not found in $PATH": unknown.
When I get into that container, I can see that my jar files are in fact inside /usr/local/bin directory which is in the PATH. The CLASSPATH in the container is empty...
What do I need to do to fix this?
I used info from the link provided by midelb above and ended up with two containers: one for SimpleProducer and another for SimpleReceiver.
Here is the Docker file for one:
FROM store/oracle/serverjre:8
MAINTAINER <james.depaul#maxar.com>
VOLUME /data
COPY build/libs/*.jar /usr/local/bin/
COPY /app/simple-queue-0.1-SNAPSHOT.jar /usr/local/bin/
ENTRYPOINT java -classpath /usr/local/bin/simple-queue-0.1-SNAPSHOT.jar:/usr/local/bin/jms-1.1.jar:/usr/local/bin/commons-logging-1.2.jar:/usr/local/bin/activemq-all-5.13.3.jar -Dserver1=$SERVER1 -Dserver2=$SERVER2 -Dusername=$USER -Dpassword=$PASSWORD com.fusesource.activemq.exercises.simple.queue.SimpleConsumer
Build
docker build -t mysystem/simple-client-consumer:1.0
And I call it like this now:
docker run -d --rm -e SERVER1=server-b0.domain.com -e SERVER2=server-b1.domain.com -e USER=user -e PASSWORD=passwd mysystem/simple-client-consumer:1.0

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

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...

How to run a specific profile on WebSphere on Docker?

Is it possible to copy an existing WebSphere profile and run it on WebSphere in Docker?
I am doing some research on containerization, virtualization, etc. and am currently working with Docker. Getting WebSphere up and running on Docker is simple enough:
docker run --name wasserver -h wasserver -p 9043:9043 -p 9443:9443 -d ibmcom/websphere-traditional:install
What I'd like to do is use a profile from another WebSphere instance and run that on the Docker WebSphere. I have tried to do the following in an attempt to mount a directory that contains the profile in question, and to run same:
docker run -v /opt/WebSphere/WAS8_5/:/WASDIR --name myprofileserver -h myprofileserver -p 9043:9043 -p 9443:9443 -d ibmcom/websphere-traditional:install -e PROFILE_NAME=/WASDIR/profiles/myprofile1
The end result of this command is that the container is created, but does not run:
docker: Error response from daemon: oci runtime error: exec: "-e": executable file not found in $PATH
Perhaps there is a switch, setup, or other configuration I am missing here?
The last argument to docker run is the command you want to run inside the container (or the name of the image if you're running the default entrypoint / cmd). You just need to move your environment variable definition back in the command like this:
docker run -v /opt/WebSphere/WAS8_5/:/WASDIR --name myprofileserver -h myprofileserver -p 9043:9043 -p 9443:9443 -d -e PROFILE_NAME=/WASDIR/profiles/myprofile1 ibmcom/websphere-traditional:install

How to run a script in user mode inside an executable jar that is executed as root?

I want to run the following script within a Java executable jar on the Raspberry Pi.
the script (= stream.sh):
#!/bin/sh
raspivid -fps 25 -w 640 -h 360 -vf -n -o - -t 999999 |cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/cam.sdp,rtcp-mux}' :demux=h264
the Java code:
Runtime.getRuntime().exec("sh stream.sh"));
The problem is that the jar must be run with sudo and the vlc command doesn't accept sudo. Neither the script or the Java code contain sudo but as the jar is executed as sudo, vlc still gives the error "VLC is not supposed to be run as root...".
What is the easiest way to make the script run in user mode inside the jar?
I would use su -l $LOGIN -c $CMD or sudo -u $LOGIN $CMD.
Runtime.getRuntime().exec("sudo -u myuser sh stream.sh"));
man sudoers has all the information you need.
You need to change /etc/sudoers

Categories