I’m currently setting up a Spring Boot project using Docker. This is how I’m structuring my Dockerfile, but I’m encountering an issue related to the chmod +x command.
I would really appreciate any guidance on how to resolve this problem, as well as any suggestions on how I could improve my Dockerfile.
Thank you very much in advance!
FROM gradle:9.4.1-jdk21-alpine
RUN adduser -D user
WORKDIR /home
COPY . /home
RUN chmod +x gradlew
RUN ./gradlew build
CMD ["./gradlew", "bootRun"]
Even though you use a gradle base-image, you run your commands using the gradle wrapper. Why not use gradle provided by the image?
You add a user, but don’t use it. It would require a USER instruction to actually make the image start with the user
gradle is a build tool, it does not belong into a final image. Unnecessary dependencies, mean more potential surface for vulnerabilities. The same can be said about a Java jdk, it does not belong into the final image.
You should use a multi-staged build, and have one stage that builds the spring boot jar, and a final stage that bases on a java base image (you will need to pick a flavor, personally I prefer eclipse temurin jre images as base for the final stage), COPY --from your build stage into the final stage, and use a CMD that does not rely on gradle to start the app (smth like CMD [java", "-jar", "/path/app.jar"])