Unable to Build Docker Image with gradlew Command - java

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

Related

Dockerfile COPY file generated by RUN sh script

I want to automate the building and execution of a java spring web service by using Docker.
I run a script from the dockerfile that generate a settings.xml file. I want to copy that file in the image, but because RUN create a new container (I think?) the build can't find the generated file.
Here is the begining of my Dockerfile:
FROM ubuntu:20.04
WORKDIR /app
COPY . .
RUN ./script.sh
COPY settings.xml /root/.m2/
and there is the output...
How can I copy the settings.xml file that was generated by my sh script?
Thanks!
COPY copies from the host to the image. If you want to copy from the image to the image, you use the 'normal' cp command.
You're right that each RUN statement runs in a separate shell. But since your generated file is stored in the file system, it'll work if you do it in different RUN statements.
But let's do it in one RUN statement to only get 1 new layer in the image:
FROM ubuntu:20.04
WORKDIR /app
COPY . .
RUN ./script.sh && \
cp settings.xml /root/.m2/
You can also use mv to move the file if you don't need a copy to stay in /app.
By executing RUN script.sh the file will be generated inside the container. Thus it cannot be copied to the image using the COPY Dockerfile command. Instead you could mv the file inside the container to the desired destination:
FROM ubuntu:20.04
WORKDIR /app
COPY . .
RUN ./script.sh \
&& mv /path/to/settings.xml /root/.m2/

Unable to run 'RUN ./mvnw dependency:go-offline -B' when building docker image from "openjdk:8-jdk-alpine" for Spring Boot app

So I am trying to run a spring boot app with maven wrapper inside the docker container. Here is my Docker file:
### Stage 1: Build the application
FROM openjdk:8-jdk-alpine as build
RUN mkdir -p /app
#Set the current working directory inside the image
WORKDIR /app
#copy maven executable to the image
COPY mvnw .
COPY .mvn .mvn
#Copy the pom.xml file
COPY pom.xml .
#Build all the dependencies in preparation to go offline
#This is a separate step so the dependencies will be cached unless
#the pom.xml file has changed
RUN ./mvnw dependency:go-offline -B
#Copy the project source
COPY src src
#Package the application
RUN ./mvnw package -DskipTests
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)
I have this error:
Step 7/16 : RUN ./mvnw dependency:go-offline -B
---> Running in 642a32f86392
/bin/sh: ./mvnw: not found
ERROR: Service 'app-server' failed to build: The command '/bin/sh -c ./mvnw dependency:go-offline -B' returned a non-zero code: 127
I am working with windows 10 pro. Please I need your help
Maybe a duplicate of Unable to run './mvnw clean install' when building docker image based on "openjdk:8-jdk-alpine" for Spring Boot app
Can you check the line endings of the mvnw shell script?
You could fix it by adding this before executing the mvnw command:
RUN dos2unix mvnw
Alternatively, if the file is in git, you can also fix it by adding the following to a .gitattributes file and checking the file out again:
*.bat text eol=crlf
mvnw text eol=lf
You have to copy the project files into the /app dir first. And you don't have the maven wrapper in the context folder where you run the docker build.
Try change the end of line mvnw file from Windows style CRLF to Unix style LF. Then rebuild the image.

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.

javac: File Not Found Error in Dockerfile

I am trying to build a docker image for a java app.
I have done the following in the dockerfile :
FROM java:8
RUN javac HelloDocker.java
CMD ["java","HelloDocker"]
When I am trying to build the image it is throwing the following exception :javac: file not found: HelloDocker.java
The HelloDocker.java file and Dockerfile is in the same directory.
Also, when I tried to compile the java file separately (via javac HelloDocker.java), it did not throw any error.
You are using a deprecated image. You should be using openjdk image. See the below
https://hub.docker.com/_/openjdk/
Also you need javac so you should be using the one with jdk tag and not the jre tag.
So try openjdk:8-jdk
Edit-1
Also you need to copy the files inside your Dockerfile. When you use docker build ., then the current directory files as available to you as context but they are not inside image
FROM java:8
WORKDIR /app
COPY HelloDocker.java .
RUN javac HelloDocker.java
CMD ["java","HelloDocker"]
Assuming this is the complete Dockerfile, you need to have a file named HelloDocker.java in your docker image's file system, before you can compile the file using "RUN javac HelloDocker.java" . You may copy the file from your host's file system to you docker image using docker COPY/ADD command.

Docker image of Java project

I am trying make a docker image of a java project. I first created a directory and in that I created a docker.txt file. The files contains this
FROM java:8
# Install maven
RUN apt-get update
RUN apt-get install -y maven
WORKDIR /home/mmt/CouchBaseClient/CB-RestAPI/CouchBaseThinClient
# Prepare by downloading dependencies
ADD pom.xml /home/mmt/CouchBaseClient/CB-RestAPI/CouchBaseThinClient/pom.xml
RUN ["mvn", "dependency:resolve"]
RUN ["mvn", "verify"]
# Adding source, compile and package into a fat jar
ADD src /home/mmt/CouchBaseClient/CB-RestAPI/CouchBaseThinClient/src
RUN ["mvn", "package"]
EXPOSE 4567
CMD ["/usr/lib/jvm/java-8-openjdk-amd64/bin/java", "-jar", "target/sparkexample-jar-with-dependencies.jar"]
and then I run in terminal the following command
docker build -t API .
I get the following error
invalid value "API" for flag -t: Error parsing reference: "API" is not a valid repository/tag
See 'docker build --help'.
Docker is complaining about "API" in the sense that it's not allowed to have a tag name with one or more character in uppercase:
$ docker build -t FOO .
repository name component must match "[a-z0-9](?:-*[a-z0-9])*(?:[._][a-z0-9](?:-*[a-z0-9])*)*"
Usually "recipes" to build Docker images are written in a file named Dockerfile, anyway you can continue to use docker.txt using the -f option:
docker build -f docker.txt -t api .

Categories

Resources