Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle and Docker: How to run a Gradle build within Docker container?

I have the following docker file that runs a spring boot application:

# For Java 11, try this
FROM adoptopenjdk/openjdk11:alpine-jre

#
ARG JAR_FILE=/build/libs/pokerstats-0.0.1-SNAPSHOT.jar

#
WORKDIR /opt/app

#
COPY ${JAR_FILE} app.jar

#
ENTRYPOINT ["java","-jar","app.jar"]

The issue is that currently I have to run gradle clean build on my host machine first to create the jar file on my local machine at path:

/build/libs/pokerstats-0.0.1-SNAPSHOT.jar

How can I put this gradle clean build into my docker file so that the build step is done inside the container?

edit:

I want the steps for a user to be:

  1. Clone my project from github
  2. run docker build -t pokerstats . - which will do the gradle build
  3. run docker container run -d -p 8080:8080 pokerstats

The user will clone my project from github - I then want them to be able to run the docker container without having to build the project with gradle first - I.e. I want the docker file to do the build and copy the jar into the container.

like image 944
java12399900 Avatar asked Apr 08 '20 18:04

java12399900


People also ask

Is Gradle a container?

gradle:<version> It is designed to be used both as a throw away container (mount your source code and start the container to start your app), as well as the base to build other images off of.

How do I run a Gradle project from a docker container?

Run this from the directory of the Gradle project you want to build. docker run --rm -u gradle -v "$PWD":/home/gradle/project -w /home/gradle/project gradle gradle <gradle-task> Note the above command runs using uid/gid 1000 (user gradle) to avoid running as root.

Is there a way to build an image without defining dockerfile?

Configuration is more opinionated. e.g. you can build an image for a Spring Boot project, without having to define the Dockerfile. See article bmuschko Docker Gradle plugin review for full details. 3. Step-by-step example

What can I do with the mesos Gradle plugin?

This plugin is quite versatile because it provides Gradle tasks for almost every Docker CLI command. In Mesos Elasticsearch we build two Docker images. The first image, mesos/elasticsearch-scheduler is an image which contains the executable jar of the Elasticsearch scheduler. By creating an image for it we can deploy it via Marathon.

How do I deploy a Spring Boot application using Gradle?

Step-by-step example 1 3.1 Application to deploy. You can download this sample application jar file which runs a Spring Boot application on port 8080. 2 3 .2 Initialising Gradle. ... 3 3.3. ... 4 3.4. ... 5 3.5 Running the Docker image with Gradle. ...


1 Answers

After reading this article I have been able to solve this using a Multi Stage Docker Build. Please see the Docker file below:

# using multistage docker build
# ref: https://docs.docker.com/develop/develop-images/multistage-build/
    
# temp container to build using gradle
FROM gradle:5.3.0-jdk-alpine AS TEMP_BUILD_IMAGE
ENV APP_HOME=/usr/app/
WORKDIR $APP_HOME
COPY build.gradle settings.gradle $APP_HOME
  
COPY gradle $APP_HOME/gradle
COPY --chown=gradle:gradle . /home/gradle/src
USER root
RUN chown -R gradle /home/gradle/src
    
RUN gradle build || return 0
COPY . .
RUN gradle clean build
    
# actual container
FROM adoptopenjdk/openjdk11:alpine-jre
ENV ARTIFACT_NAME=pokerstats-0.0.1-SNAPSHOT.jar
ENV APP_HOME=/usr/app/
    
WORKDIR $APP_HOME
COPY --from=TEMP_BUILD_IMAGE $APP_HOME/build/libs/$ARTIFACT_NAME .
    
EXPOSE 8080
ENTRYPOINT exec java -jar ${ARTIFACT_NAME}
like image 99
java12399900 Avatar answered Oct 28 '22 20:10

java12399900