Initial commit.

This commit is contained in:
2024-01-15 03:11:26 +01:00
commit 38876665fe
5 changed files with 153 additions and 0 deletions

32
Dockerfile Normal file
View File

@@ -0,0 +1,32 @@
# Using Ubuntu because it's the most popular, which typically makes
# troubleshooting easier
FROM ubuntu:22.04
WORKDIR /minecraft-server
# Install dependencies
COPY packages-ubuntu.txt .
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y $(cat packages-ubuntu.txt)
COPY get-papermc.sh .
RUN ./get-papermc.sh
# lightweight profiler
COPY get-spark.sh .
RUN ./get-spark.sh
COPY entrypoint.sh .
RUN groupadd -r mcserver \
&& useradd -r -g mcserver mcserver \
&& chown mcserver:mcserver --recursive /minecraft-server
USER mcserver:mcserver
ENTRYPOINT ["./entrypoint.sh"]
EXPOSE 25565/tcp
EXPOSE 25565/udp

56
entrypoint.sh Executable file
View File

@@ -0,0 +1,56 @@
#!/bin/bash
set -eu
memory_heuristic () {
if [[ -v memory_heuristic_cache ]]; then
echo $memory_heuristic_cache
return
fi
local total_memory=$(free | awk '/Mem:/{print $2}')
# Below 8 GB we assume this is running on a dedicated server, so we
# allocate all but 10mb of the RAM. Otherwise allocate 30%. This is not
# based on any profiling or testing
if [[ total_memory -lt 8388608 ]]; then
memory_heuristic_cache=$((total_memory - 10240))
else
memory_heuristic_cache=$((total_memory * 3/10))
fi
memory_heuristic_cache+=k
echo $memory_heuristic_cache
}
# https://docs.papermc.io/paper/aikars-flags
args=()
args+=("-Xms$(memory_heuristic)")
args+=("-Xmx$(memory_heuristic)")
args+=("-XX:+UnlockExperimentalVMOptions")
args+=("-XX:+ParallelRefProcEnabled")
args+=("-XX:+DisableExplicitGC")
args+=("-XX:+AlwaysPreTouch")
args+=("-XX:+PerfDisableSharedMem")
args+=("-XX:MaxGCPauseMillis=200")
args+=("-XX:InitiatingHeapOccupancyPercent=15")
args+=("-XX:SurvivorRatio=32")
args+=("-XX:MaxTenuringThreshold=1")
# G1 options
args+=("-XX:+UseG1GC")
args+=("-XX:G1NewSizePercent=30")
args+=("-XX:G1MaxNewSizePercent=40")
args+=("-XX:G1HeapRegionSize=8M")
args+=("-XX:G1ReservePercent=20")
args+=("-XX:G1HeapWastePercent=5")
args+=("-XX:G1MixedGCCountTarget=4")
args+=("-XX:G1MixedGCLiveThresholdPercent=90")
args+=("-XX:G1RSetUpdatingPauseTimePercent=5")
mkdir -p logs
args+=("-Xlog:gc*:logs/gc.log:time,uptime:filecount=5,filesize=1M")
args+=("-Dcom.mojang.eula.agree=true")
args+=("-jar" "paper.jar" "--nogui")
exec java ${args[@]}

34
get-papermc.sh Executable file
View File

@@ -0,0 +1,34 @@
#!/bin/bash
set -eu
# print msg to stderr
debug_msg () {
echo "$@" >/dev/stderr
}
# get the download url for the latest version
latest_url() {
if [[ -v latest_url_cache ]]; then
echo $latest_url_cache
return
fi
debug_msg "finding download url to latest version..."
local url=https://papermc.io/api/v2/projects/paper
local minecraft_version=$(curl --silent "$url" | jq -r '.versions[-1]')
url+=/versions/$minecraft_version
local papermc_build=$(curl --silent $url | jq -r '.builds[-1]')
url+=/builds/$papermc_build
latest_url_cache=$url/downloads/paper-$minecraft_version-$papermc_build.jar
debug_msg "Found url" $latest_url_cache
echo $latest_url_cache
}
debug_msg "downloading..."
curl --silent $(latest_url) > paper.jar

26
get-spark.sh Executable file
View File

@@ -0,0 +1,26 @@
#!/bin/bash
set -eu
# print msg to stderr
debug_msg () {
echo "$@" >/dev/stderr
}
if ! [[ -d plugins ]]; then
debug_msg Creating directory \'plugins\'...
mkdir plugins
fi
cd plugins
debug_msg downloading spark...
curl --silent \
https://ci.lucko.me/job/spark/400/artifact/spark-bukkit/build/libs/spark-*-bukkit.jar/*zip*/libs.zip \
> spark.zip
debug_msg unzipping...
unzip -oq spark.zip # -o is not 'output', but 'overwrite'. -q is quiet
debug_msg deleting zip...
rm spark.zip

5
packages-ubuntu.txt Normal file
View File

@@ -0,0 +1,5 @@
openjdk-21-jre-headless
curl
jq
bash
unzip