kKenny
kKenny

Reputation: 109

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

Upvotes: 3

Views: 1194

Answers (1)

Andrew A. Tasso
Andrew A. Tasso

Reputation: 86

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"]

Upvotes: 3

Related Questions