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

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;"]

Related

unable to start container process: exec: "mvnw": executable file not found in $PATH: unknown

This is my source code https://github.com/donhuvy/springboot-docker . I follow guide at tutorial video https://www.youtube.com/watch?v=7BCtc9cAS6o . File Dockerfile
# syntax=docker/dockerfile:1
#Which "official Java image" ?
FROM openjdk:oraclelinux8
#working directory
WORKDIR /app
#copy from your Host(PC, laptop) to container
COPY .mvn/ .mvn
COPY mvnw pom.xml ./
#Run this inside the image
RUN ./mvnw dependency:go-offline
COPY src ./src
#run inside container
CMD [ "mvnw", "spring-boot:run" ]
Log
C:\Users\donhu>docker pull donhuvy/springboot-docker:v1.0.0
v1.0.0: Pulling from donhuvy/springboot-docker
e54b73e95ef3: Pull complete
e6e62647f09f: Pull complete
da8e06a8884e: Pull complete
d8cbf9b4e6de: Pull complete
9971eb650313: Pull complete
366b24bf882f: Pull complete
35b5c085babf: Pull complete
b51a76bbfa65: Pull complete
Digest: sha256:f637c16c3b2a930d048e95f89f2a7aa53754f349e08e0c5a86398c5481eb07f1
Status: Downloaded newer image for donhuvy/springboot-docker:v1.0.0
docker.io/donhuvy/springboot-docker:v1.0.0
C:\Users\donhu>docker run donhuvy/springboot-docker:v1.0.0
docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "mvnw": executable file not found in $PATH: unknown.
C:\Users\donhu>
How to fix it?
add springboot-docker dir to your $PATH variable.
basically the shell is not able to find the mvnw command, it looks for all the locations in the $PATH var. since your mvnw binary is located in your project dir you need to add it.
if this was in a linux environment it would be done like this
export PATH=$PATH:./springboot-docker
for windows you can search online how this can be edited.
Change CMD [ "mvnw", "spring-boot:run" ] to CMD [ "./mvnw", "spring-boot:run" ]
When your docker want to execute command mvnw springboot:run, it needs to find file mvnw in environment $PATH, but find none.
Change mvnw to ./mvnw which will tell docker to execute the mvnw file in current work directory(where you have copied mvnw before).
it's a permission problem, common on linux docker compose.
In my case it worked when I REMOVED sudo.
i.e
docker-compose up instead of sudo docker-compose up
you can also try adding sudo chmod -R a+rwx to the working directory in Dockerfile

Execute Java Jar using shell script in Docker container

I want to execute a java jar with the help of shell script in docker container. My java program simply accepts command line argument and print the message along with the malue passed as command line argument.
class SampleJar {
public static void main(String [] args) {
String msg = args[0];
System.out.println("Hello " + msg);
}
}
I build the jar and jar is working fine.
java -jar SampleJar.jar John
o/p: Hello John.
Inside my working directory there is Dockerfile and scripts folder.
Script folder having 3 files:
executeJar.sh
execute.sh
SampleJar.jar
My Dockerfile content:
FROM ubuntu
WORKDIR /opt
#COPY execute.sh .
#COPY executejar.sh .
#COPY SampleJar.jar .
COPY scripts /
ENTRYPOINT ["/execute.sh"]
CMD ["alpha"]
executeJar.sh content:
#!/bin/bash
echo "This is from execute jar . sh"
java -cp SampleJar.jar SampleJar John
execute.sh content:
#!/bin/bash
echo "This is from execute.sh"
case "$1" in
alpha)
echo "executing alpha script"
/executejar.sh "#"
;;
esac
The jar file is working fine tested independently.
I build the image using command:
docker build -t exp .
exp is the name of the image & . indicates the Dockerfile present in current directory
I create container
docker run -it --name exp-container exp:latest alpha
-it for interactive mode
exp-container is the name of the container
So execute.sh will be executed and case alpha will be executed and executeJar.sh will invoke.
Now executejar.sh should execute the java jar.
So the final output comes as
docker run -it --name exp-container2 exp:latest alpha
This is from execute.sh
executing alpha script
This is from execute jar . sh
/executejar.sh: line 3: java: command not found
How can I get the ouput of the jar?
The plain ubuntu docker container (FROM ubuntu) does not include the java runtime environment.
You have the following options:
Install java into the ubuntu container. See here: https://dzone.com/articles/creating-a-docker-image-with-ubuntu-and-java
Use the openjdk as your base container, e.g. FROM openjdk:11
I would prefer the second option unless you really need ubuntu as your base container.
See also the docker hub page for openjkd, which has several other variantes available, e.g. opnejdk

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 start a fat jar in docker?

I want to start my fat JAR inside a Docker container
Docker file
FROM java:8-jre
COPY config.yml /opt/hello/
COPY build/libs/Dockerwizard.jar /opt/hello/
EXPOSE 80
WORKDIR /opt/hello
CMD ["java", "-Xms128m", "-Xmx1500m", "-Dfile.encoding=UTF-8", "-jar", "Dockerwizard.jar", "server", "config.yml"]
Everytime I run
docker build --tag=myapp .
and
docker run -p 18080:8080 -t -i myapp
I get the message
Error: Unable to access jarfile Dockerwizard.jar
How can I resolve this?
It says:
COPY build/libs/Dockerwizard.jar /opt/hello/
...WORKDIR /opt/chat
So you are putting the Jar into /opt/hello, but then you want to run it from /opt/chat.
Maybe you want to look into using consistent path information. Beyond that, when you are not using the -cp option of java, you always have the issue that your CLASSPATH might be incomplete. So try adding -cp . for example.
Try changing the WORKDIR in your Dockerfile
WORKDIR /opt/chat is incorrect
It should be /opt/hello
Update
you have updated the question and renamed /chat to /hello everywhere - so you have made a correction to your error/ mistake

Docker Cloud automated builds - no such file or directory

Running a build of Maven based project but it fails.
The reason is no such file or directory when it tries to find the jar.
Dockerfile:
FROM frolvlad/alpine-oraclejdk8:slim
FROM maven:3.5.2-jdk-8-slim
VOLUME /tmp
CMD ['mvn package']
ADD target/app-0.1.0-SNAPSHOT.jar app.jar <-- fails there
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
The log output:
...
Removing intermediate container 60da937dde8a
Step 4/8 : CMD ['mvn package']
---> Running in 8ba364ba9d98
---> 4a722569d1a7
Removing intermediate container 8ba364ba9d98
Step 5/8 : ADD target/app-0.1.0-SNAPSHOT.jar app.jar
ADD failed: stat /var/lib/docker/tmp/docker-builder1534563/target/app-0.1.0-SNAPSHOT.jar: no such file or directory
ERROR: Build failed: ADD failed: stat /var/lib/docker/tmp/docker-builder1534563/target/app-0.1.0-SNAPSHOT.jar: no such file or directory
ERROR: Build failed with exit code 2
Have played with the different settings but it still doesn't work despite the app name is matches to the built jar one.
How to fix the issue?
This question IMO has nothing to do with Spring Boot, and it's Docker related.
In general please share more information about how exactly you run docker build command, from which directory and where does your Dockerfile reside exactly. Without this information we can only speculate and provide generic answers:
To provide some points for consideration: Docker knows nothing about Maven structure of your project so you can just maintain the following layout:
<some_dir>
|____ Dockerfile
|____ app-0.1.0-SNAPSHOT.jar
Then you can run the docker build command from this directory and this should work. Then you can experiment with target directory and once you'll understand when it works and when it doesn't proceed with your current folders layout, the chances that with this practice you'll find out the answer very quickly.

Categories