From 8429d78e016d7cfe68848b85fde15451a1ca2fd8 Mon Sep 17 00:00:00 2001 From: James McDowell Date: Sun, 16 May 2021 09:59:12 +1000 Subject: [PATCH] Optimised multi layered builds to speed up deployment, added comments to help new developers, reduced unnecessary objects being sent to the docker build context, mounted in files that may change regularly to remove the requirement for a full rebuild. --- .dockerignore | 18 +++++++++++++----- Dockerfile | 38 +++++++++++++++++++++++++++++--------- docker-compose.yml | 10 ++++++++++ 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/.dockerignore b/.dockerignore index 79289b3acd..389407f6ea 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,13 @@ -/docs -/handbook -/tools -/docker-db-data -/.idea \ No newline at end of file +# Project administration files +.idea +.git +.github + +# Not used by the build. +docs +handbook +sql +tools + +# Created by the db when using docker-compose, large and causes rebuild issues if sent to the context. +docker-db-data diff --git a/Dockerfile b/Dockerfile index b24fea224d..21eaab8819 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,40 @@ # Initial Docker support thanks to xinyifly +# Optimisation performed by wejrox # -# Build stage +# Cosmic JAR creation stage # -FROM maven:3.6.3-jdk-8 AS build -COPY src /home/app/src -COPY pom.xml /home/app -RUN mvn -f /home/app/pom.xml clean package +FROM maven:3.6.3-jdk-8 AS jar + +# Build in a separated location which won't have permissions issues. +WORKDIR /opt/cosmic +# Any changes to the pom will affect the entire build, so it should be copied first. +COPY pom.xml ./pom.xml +# Grab all the dependencies listed in the pom early, since it prevents changes to source code from requiring a complete re-download. +RUN mvn -f ./pom.xml clean dependency:go-offline +# Source code changes may not change dependencies, so it can go last. +COPY src ./src +RUN mvn -f ./pom.xml clean package # -# Package stage +# Server creation stage # FROM openjdk:8 -COPY --from=build /home/app/target/Cosmic.jar /usr/local/lib/Cosmic.jar -COPY ./ ./ + +# Host the server in a location that won't have permissions issues. +WORKDIR /opt/server +# Copy the wizet files first since they're so big and won't change often. +COPY wz ./wz +# Copy the JAR we build earlier. +COPY --from=jar /opt/cosmic/target/Cosmic.jar ./Server.jar +# Scripts are sourced on server startup, so you can mount over them for quicker redeploy. +COPY scripts ./scripts/ +# Config is read on server startup, so you can mount over it for quicker redeploy. +COPY config.yaml ./ +# Default exposure, although not required if using docker compose. +# This exposes the login server, and channels. +# Format for channels: WWCC, where WW is 75 plus the world number and CC is 75 plus the channel number (both zero indexed). EXPOSE 8484 7575 7576 7577 -ENTRYPOINT ["java", "-jar", "/usr/local/lib/Cosmic.jar"] +ENTRYPOINT ["java", "-jar", "./Server.jar"] diff --git a/docker-compose.yml b/docker-compose.yml index fc7334dedd..449b5039da 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,8 +5,18 @@ services: depends_on: - db ports: + # Login server - "8484:8484" + # Channels. + # Format: WWCC, where WW is 75 plus the world number and CC is 75 plus the channel number (both zero indexed). + # In this case, world 1 channels 1-3. - "7575-7577:7575-7577" + volumes: + # Config changes can be reloaded without rebuilding the image. + # Still requires a redeployment as they're sourced on startup. + - ./config.yaml:/opt/server/config.yaml + - ./scripts:/opt/server/scripts + - ./wz:/opt/server/wz environment: DB_HOST: "db"