Build gradle project inside a Docker

According to the Dockerfile for gradle:4.2.1-jdk8-alpine at , it has "gradle" as its default user. The files that you are copying from your app directory to docker image's app directory may not have right permissions for "gradle" user.

You should add three additional commands in your Dockerfile for setting the correct permissions:

FROM gradle:4.2.1-jdk8-alpine
WORKDIR /app
COPY --from=0 /app/myProject /app

USER root                # This changes default user to root
RUN chown -R gradle /app # This changes ownership of folder
USER gradle              # This changes the user back to the default user "gradle"

RUN ./gradlew build --stacktrace

Another option could be to use the ADD command instead of COPY and then use it's --chown option to change the owner of the file after copy. So the final Dockerfile would be even simpler.

FROM gradle:4.2.1-jdk8-alpine
WORKDIR /app
ADD --chown=gradle:gradle /app/myProject /app

RUN ./gradlew build --stacktrace