Error while executing gradle command in docker file - docker

FROM gradle:4.2.1-jdk8-alpine
WORKDIR /app
ADD --chown=gradle:gradle /app/producer /app
RUN ./gradlew build --stacktrace
Project Structure is as follows . It is a muti module project:
<code>
--springbootdocker (Root folder of project) <br>
--producer (Sub module Producer) <br>
-- Dockerfile (for Producer)<br>
--consumer (Sub module Consumer) <br>
-- Dockerfile (for Consumer)<br
</code>
This is the docker file.
While doing docker build: getting this error
failed to build: ADD failed: stat
/var/lib/docker/tmp/docker-builder561553413/app/producer: no such
file or directory

Here you have to fix a couple of things in your Dockerfile.
ADD command
ADD command requires two parameters <src> and <dest>. So, you should provide producer path from the host as src container path as dest. But in such cases recommended to use COPY command.
COPY --chown=gradle:gradle producer /app/producer
RUN ./gradlew
It should be just gradle and it's WORKDIR should be /app/producer. If not it'll fail and you'll get,
Failed to create parent directory '/app/.gradle' when creating directory
'/app/.gradle/4.2.1/fileHashes' error when running gradle command.
Because the WORKDIR /app owns by user root.
Recommend to divide RUN gradle build --stacktrace as ENTRYPOINT and CMD.
Complete Dockerfile
FROM gradle:4.2.1-jdk8-alpine
WORKDIR /app
COPY --chown=gradle:gradle producer /app/producer
WORKDIR /app/producer
ENTRYPOINT ["gradle"]
CMD ["build", "--stacktrace"]
The partial output of docker build
Starting a Gradle Daemon (subsequent builds will be faster)
:buildEnvironment
------------------------------------------------------------
Root project
------------------------------------------------------------
classpath
No dependencies
BUILD SUCCESSFUL in 5s
1 actionable task: 1 executed

This is the dockerfile which is currently working without any error.
FROM gradle:4.10.0-jdk8-alpine AS build
COPY --chown=gradle:gradle . /home/gradle/src/producer
WORKDIR /home/gradle/src/producer
RUN gradle bootJar --no-daemon --stacktrace
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=build/libs/*.jar
COPY --from=build /home/gradle/src/producer/build/libs/*.jar producer.jar
ENTRYPOINT ["java","-jar","/producer.jar"]

ADDed files need to be under the directory where the docker build is being run from, so depending on your structure you probably want something like:
ADD myProject /app
Assuming you have a structure like:
Dockerfile
myProject/

Related

Is it possible to copy files to the host during a multistage docker build?

I have written the following Dockerfile to containerize a maven build:
FROM maven AS builder
WORKDIR /build
COPY . /build
RUN mvn -Dmaven.repo.local=.m2/repository clean test clover:instrument clover:clover clover:check findbugs:check package site
FROM amazoncorretto:8
COPY --from=builder /build/target/*.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
The second stage copies the jar file built by the first stage so that it can be used by its ENTRYPOINT but I'd also like the docker host to get a copy of the entire target directory from the container because it holds other build artefacts like code coverage and analysis reports.
Specifically, I'd like the copy to happen as part of the docker build -t fakefirehose . Is this possible?

docker multiple stages - docker gradle build success but docker openjdk failed to build: COPY failed: no source files were specified

i am trying to build jar files in docker gradle, then docker openjdk copy the jar files and run it.
but hitting error
Step 10/14 : COPY --from=builder /test-command/build/libs/*.jar /app/test-command.jar
ERROR: Service 'test-command' failed to build: COPY failed: no source files were specified
docker file
FROM gradle:5.6.3-jdk8 as builder
COPY --chown=gradle:gradle . /test-command
ADD --chown=gradle . /app
WORKDIR /app
RUN gradle build
FROM ubuntu
FROM openjdk:8-alpine
WORKDIR /app
VOLUME ["/app"]
COPY --from=builder /test-command/build/libs/*.jar /app/test-command.jar
COPY --from=builder /test-command/docker/startup.sh /app/startup.sh
#RUN sh -c 'touch /app/test-command.jar'
RUN chmod +x /app/startup.sh
RUN chmod +x /app/test-command.jar
ENTRYPOINT ["/bin/sh", "/app/startup.sh"]
when the docker gradle building files, i can view them by using below docker command, and i saw the path, jar files in the container.
but once build is completed, then i did not see the container anymore.
could it be the reason why docker oepnjdk cannot find the files / source path??
docker exec -it name-of-container bash

Gradle dependency caching during docker multi stage build?

I have the following Dockerfile
FROM gradle:jdk13 AS appbuild
WORKDIR "/home/gradle/"
COPY --chown=gradle:gradle "./build.gradle" "/home/gradle/"
RUN gradle dependencies
COPY --chown=gradle:gradle "./src/" "/home/gradle/src/"
RUN gradle build --info
FROM openjdk:13
ENV LANG en_US.UTF-8
COPY --from=appbuild "/home/gradle/build/libs/frontend.jar" "/frontend.jar"
CMD ["java", "-jar", "-Dspring.profiles.active=default", "/frontend.jar"]
My aim is to prevent gradle from downloading the dependencies every time I build a docker image.
The command gradle dependencies downloads all the required java libraries in case they are missing.
Before the first gradle dependencies command I only copied the build.gradle in order to only download the dependencies and cache them.
When I run the gradle build command, why does it want to download all the files again? They are already present in one of the layers.
I have tried with RUN gradle clean build --info || return 0 instead of gradle dependencies, all the same.
The GRADLE_USER_HOME environment variable isn't set by default. You'll need to explicitly set it, and then copy over the downloaded dependencies in the next stage.
FROM gradle:jdk13 AS cache
WORKDIR /app
ENV GRADLE_USER_HOME /cache
COPY build.gradle gradle.properties settings.gradle ./
RUN gradle --no-daemon build --stacktrace
FROM gradle:jdk13 AS builder
WORKDIR /app
COPY --from=cache /cache /home/gradle/.gradle
COPY src/ src/
RUN gradle --no-daemon build --stacktrace
FROM openjdk:jre-alpine
WORKDIR /app
RUN apk --no-cache add curl
COPY --from=builder /app/build/libs/frontend.jar /frontend.jar
ENV PORT 80
EXPOSE 80
HEALTHCHECK --timeout=5s --start-period=5s --retries=1 \
CMD curl -f http://localhost:$PORT/health_check
CMD ["java", "-jar", "-Dspring.profiles.active=default", "/frontend.jar"]
Here's my original Dockerfile. I've modified it for yours, but haven't tested it, so you can refer to the original if you're in doubt.

How would I create docker container of Gradle application?

I have a gradle spark application that needs to be used with docker to create a container. I am new to docker and I am having a hard time setting up Dockerfile.
In Dockerfile I tried executing build.gradle and running Main.java.
# Start with a base image containing Java runtime
FROM gradle:jdk10 as builder
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle build
FROM openjdk:10-jre-slim
COPY ./ /tmp/
CMD ["cd tmp/src/main/java/Empowering/"]
CMD ["javac Main.java"]
EXPOSE 8080
For your runtime container, you shouldn't be using javac. Gradle is doing the build for you. So, you'll only need to worry about running what Gradle is producing. Additionally, you want to make sure that you're properly copying the stuff being built by your builder.
I don't know what type of application you are running and what you're Gradle configuration looks like so I'm going to have to make some assumptions here.
I would highly suggest that you utilize the application plugin if you're generating a web application. If that's the case the installDist will put everything you need into the build/install folder. Once that's in place, you can simply use the generated shell script for your CMD/ENTRYPOINT
For Example:
# Start with a base image containing Java runtime
FROM gradle:jdk10 as builder
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle installDist
FROM openjdk:10-jre-slim
EXPOSE 8080
COPY --from=builder /home/gradle/src/build/install/<app name>/ /app/
WORKDIR /app
CMD ["bin/<app name>"]
If you're packaging as a jar, you can just simply copy the jar generated by the build/ fat jar task in the builder then run that
# Start with a base image containing Java runtime
FROM gradle:jdk10 as builder
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle build
FROM openjdk:10-jre-slim
EXPOSE 8080
COPY --from=builder /home/gradle/src/build/<jar output path>/<app name>.jar /app/
WORKDIR /app
CMD ["java -jar <app name>.jar"]

COPY command failing when running DOCKER BUILD

I'm trying to create a Docker image but it's not working. I'm trying to run this Docker command from the CLI and I'm getting the following error:
(from the <repo root>/src/Services/Accounts/Accounts.Api)
> docker build .
Error message:
Step 7/18 : COPY ["src/Services/Accounts/Accounts.Api/Accounts.Api.csproj", "src/Services/Accounts/Accounts.Api/"]
COPY failed: stat /var/lib/docker/tmp/docker-builder937056687/src/Services/Accounts/Accounts.Api/Accounts.Api.csproj: no such file or directory
and here's my Dockerfile
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
RUN ls -al
COPY ["src/Services/Accounts/Accounts.Api/Accounts.Api.csproj", "src/Services/Accounts/Accounts.Api/"]
COPY ["src/Services/Accounts/Accounts.Api/NuGet.config", "src/Services/Accounts/Accounts.Api/"]
RUN dotnet restore "src/Services/Accounts/Accounts.Api/Accounts.Api.csproj"
COPY . .
WORKDIR "/src/src/Services/Accounts/Accounts.Api"
RUN dotnet build "Accounts.Api.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "Accounts.Api.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Hornet.Accounts.Api.dll"]
I've also tried running this from the repo root directory:
> docker build .\src\Services\Accounts\Accounts.Api
Why is it trying to find my files in some /var/lib/docker/tmp/blah folder?
Further info:
Windows 10 OS
Docker CE with Linux Containers
VSCode
I think we can solve this by clarifying how to use the build context and how to specify the location of the Dockerfile.
The Docker build command usage looks like this:
docker build [OPTIONS] PATH
Your build context is the location that you define with PATH, Docker can use the files in the build context for the build. (You cannot use files outside the build context for the build.)
In the Dockerfile in your COPY statements you are specifying the source files' location relative to the root of your repo. This implies that
You should run your build command from the <root of your repo> with PATH . like this:
docker build .
You have not specified the path of your Dockerfile in the question, but it seems to me that it's in a subfolder of your repo. To make your build work issue the docker build command from the <root of your repo> like this:
docker build -f <path to Dockerfile> .
Your Dockerfile must be in the build context.
I think getting the build context and the location of the Dockerfile right will fix your build.
Also remember to use the -t flag to tag your image.
Why is it trying to find my files in some /var/lib/docker/tmp/blah folder?
As you probably know, Docker uses a Linux VM under the hood in the Docker for Windows app to develop Linux based containers. This location simply refers to the location in the VM.

Resources