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:
github
docker build -t pokerstats .
- which will do the gradle buildThe 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.
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.
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.
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
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.
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. ...
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With