OSRSB / script-template

Template for OsrsBot scripts
https://osrsbot.org/
BSD 3-Clause "New" or "Revised" License
16 stars 22 forks source link

Multi-stage Dockerfile with Gradle cache + local dependency copies #21

Open raverydavis opened 2 years ago

raverydavis commented 2 years ago

We should implement a multi-stage Dockerfile that cache gradle dependencies and copies over the local dependencies for a Script - minimally that's just OSRSBot, but we can use DaxWalker as well

Here's an example that isn't quite right

# using multistage docker build
# ref: https://docs.docker.com/develop/develop-images/multistage-build/

# temp container to build using gradle
FROM gradle:7.4.2-jdk17-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

# Set base image from Docker image repo
# https://adoptium.net/temurin
FROM eclipse-temurin:17-jre-centos7

# Installs XDisplay packages so we can actually view the container (and run the bot)
RUN yum install libXext.x86_64 libXrender.x86_64 libXtst.x86_64 -y

ENV BOT_JAR_FILE=OSRSBot.jar
ENV APP_HOME=/usr/app/

# Adds the bot jar to the container
COPY --from=TEMP_BUILD_IMAGE $APP_HOME/$BOT_JAR_FILE .
# Adds the scripts to the container
COPY --from=TEMP_BUILD_IMAGE $APP_HOME/build/libs root/.config/OsrsBot/Scripts/Precompiled

# Exposes a port to connect via
EXPOSE 8080

# Runs the bot with the bot flag
ENTRYPOINT exec java -jar ${BOT_JAR_FILE} -bot-runelite -developer-mode
raverydavis commented 2 years ago

This should work using git clone of the OsrsBot repo - does not include DaxWalker, it needs to have settings.gradle updated for the correct file path

# using multistage docker build
# ref: https://docs.docker.com/develop/develop-images/multistage-build/

# temp container to cache gradle
FROM gradle:7.4.2-jdk17-alpine AS cache
# Environment vars
ENV APP_HOME /app
WORKDIR $APP_HOME
# Copy gradle settings and config to /app in the image
COPY build.gradle settings.gradle gradlew $APP_HOME
COPY gradle $APP_HOME/gradle
# Build gradle - caches dependencies
RUN ./gradlew --no-daemon build || return 0

# Clone the OsrsBot repo to build the jar
RUN git clone https://github.com/OSRSB/OsrsBot.git /OsrsBot/
# Gradle settings needs to be updated to be the same as OsrsBot
#RUN git clone https://github.com/OSRSB/DaxWalkerOSRSBot.git /DaxWalkerOSRSBot/

# Copy our scripts source and build the project
COPY src/ /src
RUN ./gradlew --no-daemon build

# actual container

# Set base image from Docker image repo
# https://adoptium.net/temurin
FROM eclipse-temurin:17-jre-centos7

ENV APP_HOME /app
# Name of the built OSRSBot jar file
ENV BOT_JAR_FILE OsrsBot.jar

# Installs XDisplay packages so we can actually view the container (and run the bot)
RUN yum install libXext.x86_64 libXrender.x86_64 libXtst.x86_64 -y

# Adds the bot jar to the container
COPY --from=cache $APP_HOME/$BOT_JAR_FILE $BOT_JAR_FILE
# Adds the scripts to the container
COPY --from=cache $APP_HOME/build/libs root/.config/OsrsBot/Scripts/Precompiled

# Exposes a port to connect via
EXPOSE 8080

# Runs the bot with the bot flag
ENTRYPOINT exec java -jar ${BOT_JAR_FILE} -bot-runelite -developer-mode

# Looped entry for debugging the image
#ENTRYPOINT ["tail", "-f", "/dev/null"]
raverydavis commented 2 years ago

Updated to use the same OS & chmod for gradle - should fix the permission error that one may get?

# using multistage docker build
# ref: https://docs.docker.com/develop/develop-images/multistage-build/

# temp container to cache gradle
FROM gradle:jdk17-jammy AS cache
# Environment vars
ENV APP_HOME /app
ENV GRADLE_USER_HOME /cache
WORKDIR $APP_HOME
# Copy gradle settings and config to /app in the image
COPY build.gradle settings.gradle gradlew $APP_HOME
COPY gradle $APP_HOME/gradle
RUN chmod +x gradlew
# Build gradle - caches dependencies
RUN ./gradlew --no-daemon build || return 0

# Clone the OsrsBot repo to build the jar
RUN git clone https://github.com/OSRSB/OsrsBot.git /OsrsBot/
#RUN git clone https://github.com/OSRSB/DaxWalkerOSRSBot.git /DaxWalkerOSRSBot/

# Copy our scripts source and build the project
COPY src/ /src
RUN ./gradlew --no-daemon build

# actual container

# Set base image from Docker image repo
# https://adoptium.net/temurin
FROM eclipse-temurin:17-jre-jammy

ENV APP_HOME /app
# Name of the built OSRSBot jar file
ENV BOT_JAR_FILE OsrsBot.jar

# Installs XDisplay packages so we can actually view the container (and run the bot)
RUN apt-get update && apt-get install libxext6 libxrender1 libxtst6 libxi6 -y

# Adds the bot jar to the container
COPY --from=cache $APP_HOME/$BOT_JAR_FILE $BOT_JAR_FILE
# Adds the scripts to the container
COPY --from=cache $APP_HOME/build/libs root/.config/OsrsBot/Scripts/Precompiled

# Exposes a port to connect via
EXPOSE 8080

# Runs the bot with the bot flag
ENTRYPOINT exec java -jar ${BOT_JAR_FILE} -bot-runelite -developer-mode
raverydavis commented 2 years ago

Latest version that uses BuildKit - provides a better way to cache our instructions - you'll need to have BuildKit enabled, follow instructions here: https://docs.docker.com/develop/develop-images/build_enhancements/

# syntax = docker/dockerfile:1.2
# Enable BuildKit https://docs.docker.com/develop/develop-images/build_enhancements/
# Using multistage docker build
# ref: https://docs.docker.com/develop/develop-images/multistage-build/
# Variable to use across build stages
ARG BUILD_HOME=/usr/app

# temp container to cache gradle
FROM gradle:7.4.2-jdk17-jammy AS cache
# Environment vars
ARG BUILD_HOME
ENV APP_HOME=$BUILD_HOME/bot
WORKDIR $APP_HOME
# Copy gradle settings and config to /app in the image
COPY build.gradle settings.gradle $APP_HOME

# Build gradle - caches dependencies
RUN gradle --no-daemon build || return 0

# Build container
FROM cache AS builder
ARG BUILD_HOME
ENV APP_HOME=$BUILD_HOME/bot
# Set where to pull OSRSBot from - default OSRSB
ENV REPO=OSRSB
# Set where git should store the OSRSBot project
ENV OSRS_REPO=TestRSB/
# Set where git should store the DaxWalkerOSRSBot project
ENV WALKER_REPO=DaxWalkerOSRSBot/

WORKDIR $APP_HOME

# We need to clone the project and cache the git pull - everytime there's something new pushed
# to master it will pull the latest changes
RUN --mount=type=cache,target=/tmp/git_cache/ \
    git clone https://github.com/$REPO/OsrsBot.git /tmp/git_cache/$OSRS_REPO; \
    cd /tmp/git_cache/$OSRS_REPO && git pull origin master && cp -r ./ $BUILD_HOME/$OSRS_REPO
RUN --mount=type=cache,target=/tmp/git_cache/ \
    git clone https://github.com/$REPO/DaxWalkerOSRSBot.git /tmp/git_cache/$WALKER_REPO; \
    cd /tmp/git_cache/$WALKER_REPO && git pull origin master && cp -r ./ $BUILD_HOME/$WALKER_REPO

# Copy over gradle from the cache and copy the src to build it
COPY --from=cache /root/.gradle /root/.gradle
COPY --from=cache $APP_HOME/build.gradle $APP_HOME/settings.gradle $APP_HOME
COPY src/ src/

RUN gradle --no-daemon clean build

# actual container
# Set base image from Docker image repo
# https://adoptium.net/temurin
FROM eclipse-temurin:17-jdk-jammy

ARG BUILD_HOME
ENV APP_HOME=$BUILD_HOME/bot
# Name of the built OSRSBot jar file
ENV BOT_JAR_FILE OSRSBot.jar

# Installs XDisplay packages so we can actually view the container (and run the bot)
# Caches our apt-get(s) with BuildKit
# Remove the apt list to save space
RUN --mount=type=cache,target=/var/cache/apt apt-get update \
    && apt-get upgrade -y \
    && apt-get install -yqq --no-install-recommends libxext6 libxrender1 libxtst6 libxi6 \
    && rm -rf /var/lib/apt/lists/*
#RUN apt-get update && apt-get install libxext6 libxrender1 libxtst6 libxi6 -y

# Adds the bot jar to the container
COPY --from=builder $APP_HOME/$BOT_JAR_FILE $APP_HOME/$BOT_JAR_FILE
# Adds the scripts to the container (copies source files, if you want the built jar comment this out and uncomment the line below)
#COPY --from=builder $APP_HOME/build/scripts root/.config/OsrsBot/Scripts/Sources
COPY --from=builder $APP_HOME/build/libs /root/.config/OsrsBot/Scripts/Precompiled
# Adds runelite config settings to the container
COPY /config/.runelite/settings.properties /root/.runelite/settings.properties
# Adds osrsbot account config to the container
COPY /config/.config/osrsbot_acct.ini /root/.config/osrsbot_acct.ini

EXPOSE 8080

# Launch in RuneLite mode
#ENTRYPOINT java -debug -ea -jar $APP_HOME/${BOT_JAR_FILE} --runelite --developer-mode
ENTRYPOINT java -debug -jar $APP_HOME/${BOT_JAR_FILE} --bot-runelite --developer-mode
raverydavis commented 2 years ago

https://github.com/OSRSB/script-template/pull/25 Should address this