Docker CMD - Path to script - java

How to pass the path of the script file to the CMD in the Dockerfile?
Here is my Dockerfile
FROM openjdk:8-jdk-alpine as base
EXPOSE 8080
WORKDIR '/app'
COPY run/ .
RUN mvn clean install
FROM openjdk:8-jdk-alpine
WORKDIR '/app'
COPY --from=base /app/data/startup.sh ./startup.sh
RUN ["chmod", "+x", "startup.sh"]
CMD ["startup.sh", "start"]
When I gave CMD ["/app/startup.sh", "start"], throws the below error
Cannot start service myapp: OCI runtime create failed:
container_linux.go:367: starting container process caused: exec: "/app/startup.sh": stat
/app/startup.sh: no such file or directory: unknown
The documentation says the path would be relative to the WORKDIR, so I tried passing the scriptname alone as it is now - CMD ["startup.sh", "start"] which resulted in the below error:
Cannot start service myapp: OCI runtime create failed:
container_linux.go:367: starting container process caused: exec: "service.sh": executable file
not found in $PATH: unknown
The startup.sh is a simple script file with a shebang - #!/bin/sh
Am I missing anything here? I am using docker desktop for windows and get the error when I run docker-compose up just fyi.
Please share how I can resolve this issue. Thanks

You are copying the file to /app/data in this step COPY --from=base /app/data/
So try:
CMD ["/app/data/startup.sh", "start"]

CMD ["/app/data/startup.sh", "start"]
should work

Related

Converting Shell Script to Dockerfile

I have Java app and want to generate docker image, I have shell script like this:
#!/bin/sh
java -version
export APPLICATION_DIR=$PWD
for rJarFile in `ls ${APPLICATION_DIR}/lib/*.jar`
do
export CLASSPATH=$rJarFile:$CLASSPATH
done
export CLASSPATH=$APPLICATION_DIR/classes:$CLASSPATH
java -Xverify:none -Xmx2048m -Djava.awt.headless=true -DFI_IS_CONFIGSER=N -DFICLIENT_APP_PATH=${APPLICATION_DIR} -DFI_APP_NAME=FIONLINE -DFI_BASE_INSTANCE_ID=1 -DPRODUCT_BOOTSTRAP_FILE=${APPLICATION_DIR}/data/BootstrapFile.properties -DFEBA_SYS_PATH=${APPLICATION_DIR}/data
And I try to convert it into Dockerfile like this
# FROM openjdk:8
FROM openjdk:11
RUN javac -version
# Create app directory
WORKDIR /usr/src/app
# Bundle app source
COPY . .
ENV APPLICATION_DIR=/usr/src/app
RUN echo $APPLICATION_DIR
RUN for rJarFile in `ls ${APPLICATION_DIR}/lib/*.jar`; do export CLASSPATH=$rJarFile:$CLASSPATH; done
RUN echo $CLASSPATH
ENV $CLASSPATH=$APPLICATION_DIR/classes:$CLASSPATH
# Run app
ENTRYPOINT ["java", "-Xverify:none", "-Xmx2048m", "-Djava.awt.headless=true", "-DFI_IS_CONFIGSER=N", "-DFICLIENT_APP_PATH=${APPLICATION_DIR} -DFI_APP_NAME=FIONLINE -DFI_BASE_INSTANCE_ID=1", "-DPRODUCT_BOOTSTRAP_FILE=${APPLICATION_DIR}/data/BootstrapFile.properties", "-DFEBA_SYS_PATH=${APPLICATION_DIR}/data"]
It can be generated, but there's an error when I try to run it like this:
Error response from daemon: OCI runtime create failed: container_linux.go:370: starting container process caused: process_linux.go:459: container init caused: setenv: invalid argument: unknown
I've also changed this script RUN for rJarFile in `ls ${APPLICATION_DIR}/lib/*.jar`; do export into this RUN for rJarFile in ls ${APPLICATION_DIR}/lib/*.jar; do export CLASSPATH=$rJarFile:$CLASSPATH; done, but none of them working. I don't want to make Dockerfile execute the script. Below is logs when i generate and run it.
You cannot update the classpath as you do with:
ENV $CLASSPATH=$APPLICATION_DIR/classes:$CLASSPATH
instead you can do
ENV CLASSPATH=$APPLICATION_DIR/classes:$CLASSPATH
Also - please consider moving the script into a separate shell script and adding into the container. This would greatly simplify the Dockerfile, for example:
# FROM openjdk:8
FROM openjdk:11
RUN javac -version
# Create app directory
WORKDIR /usr/src/app
# Bundle app source
COPY . .
ENV APPLICATION_DIR=/usr/src/app
RUN echo $APPLICATION_DIR
ENTRYPOINT ["/usr/src/app/start_java.sh"]
and keep your existing script inside start_java.sh

Unable to Build Docker Image with gradlew Command

I am trying to build docker image having following command in Dockerfile
FROM openjdk:13-jdk-alpine AS builder
WORKDIR /app-build
ADD . .
RUN pwd
RUN ./gradlew console:build
RUN ls /app-build/console/build/libs/
It exit with following message
/bin/sh: ./gradlew: not found
The command '/bin/sh -c ./gradlew console:build' returned a non-zero code: 127
you need to place your local directory path where your docker file is located and the path of your code repo in docker build command
docker build -f /home/ubuntu/your_project_repo/Dockerfile /home/ubuntu/your_project_repo/
I am not certain what openjdk:13-jdk-alpine already has in its image, but if does not already contain a ./gradlew executable file, you will need to double-check that the line ADD . . in your Dockerfile is actually copying in the file ./gradlew.
Is ./gradlew stored locally in the same path as your Dockerfile? ADD . . will take all the files in the path of your Dockerfile and copy it over into your custom image.
Another way to check to see if you are copying in the correct files is by building the image and then using a docker exec command to manually check to see what files are in your custom Docker image.
I would use the following command to do this: docker exec -it <name_of_docker_image> bash

Failed to parse configuration class, when starting Spring Boot application as Docker image

Dockerfile:
FROM java:8-jdk-alpine
RUN mkdir -p /usr/app
RUN mkdir -p /usr/app/logs/
COPY ./storefront/build/libs/storefront-0.0.1-SNAPSHOT.jar /usr/app
WORKDIR /usr/app
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "storefront-0.0.1-SNAPSHOT.jar"]
start.sh
sudo docker build ./ -t platform
sudo docker run -p 8080:8080 platform
Error:
2020-05-11 11:53:01.925 ERROR 1 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [lv.dz.platform.storefront.StorefrontApplication]; nested exception is java.io.FileNotFoundException: /home/daniel/projects/MY/platform/storefront/src/main/resources/application.properties (No such file or directory)
Note:
Working with java -jar storefront-0.0.1-SNAPSHOT.jar and ./gradlew bootRun
Any ideas?
Update:
Issue was in one of the modules (not storefront), where #PropertySource was defined as full path to application.properties. Resolved by removing this line, since it was old code.
Its works in you computer, because you have dependency to your local file /home/daniel/projects/MY/platform/storefront/src/main/resources/application.properties. After building and running java file inside container your code still tries fo load application.properties file from /home/daniel/projects/MY/platform/storefront/src/main/resources/application.properties which is not exists inside container.
Inside you application change absolute path to relative.
Use dockers feature multi-staged build. Build your jar file during building an docker image.
Have you tried to to put property out of jar like below ?
ADD application.properties /usr/app/application.properties
ENTRYPOINT ["java" ,"--spring.config.location=classpath:file:/usr/app/application.properties","-jar","storefront-0.0.1-SNAPSHOT.jar"]

sh File Not found

I am using GraalVM to create a native image of petclinic in Java SpringBoot
Here is my DockerFile
FROM oracle/graalvm-ce:19.2.1
WORKDIR /opt/graalvm
RUN gu install native-image
COPY ./spring-petclinic/* /usr/local/
RUN native-image -jar /usr/local/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
FROM alpine:latest
WORKDIR /usr/local/
COPY --from=0 /opt/graalvm/spring-petclinic-2.2.0.BUILD-SNAPSHOT .
CMD yes
When I am using the image and do a ls -la I see the file spring-petclinic-2.2.0.BUILD-SNAPSHOT which is the native image I want to execute
My problem append when I try to execute de binary.
I get this error message:
sh: ./spring-petclinic-2.2.0.BUILD-SNAPSHOT: not found
I cannot execute a file which exists.

What changes\setups I need to make to run java in docker over WSL(windows-subsystem-for-linux)?

I'm running the docker command in windows command line successfully but when I run the same command in windows-subsystem-for-linux it shows class not found exception.
windows:
docker run -it --cpus 4 -v ${PWD}:/app --workdir /app adoptopenjdk/openjdk11 java -cp C:\path\to\class Helloworld.java
WSL:
docker run -it --cpus 4 -v ${PWD}:/app --workdir /app adoptopenjdk/openjdk11 java -cp /path/to/class Helloworld.java
Expected output:
Hello world!!
Error:
Could not find or load main class Helloworld.java
Caused by: java.lang.ClassNotFoundException: Helloworld.java
It seems like the path may be wrong
You can also access your local machine’s filesystem from within the
Linux Bash shell – you’ll find your local drives mounted under the
/mnt folder. For example, your C: drive is mounted under /mnt/c:
-- https://learn.microsoft.com/en-us/windows/wsl/faq#what-can-i-do-with-wsl

Categories

Resources