From d85efbad60076b50e33e0c72cc6bc5b46b87805a Mon Sep 17 00:00:00 2001 From: Joseph Goh Date: Sun, 23 Oct 2022 04:09:57 +0800 Subject: [PATCH 1/3] Dockerize TShock --- .dockerignore | 15 +++++++++++++++ Dockerfile | 41 +++++++++++++++++++++++++++++++++++++++++ docs/changelog.md | 1 + 3 files changed, 57 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..002067b4 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,15 @@ +# ignore every file +* + +# except for the ones required for building +!i18n/ +!prebuilts/ +!TerrariaServerAPI/ +!TShockAPI/ +!TShockLauncher/ +!TShockLauncher.Tests/ +!TShock.sln + +# but exclude build artifacts +*/bin/ +*/obj/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..00a6f3e2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,41 @@ + +# Docker Instructions +# Build Image: +# docker build -t tshock . +# and run: +# docker run -p 7777:7777 -p 7878:7878 \ +# -v :/tshock \ +# -v :/worlds \ +# -v :/plugins \ +# --rm -it tshock -world /worlds/ + +FROM mcr.microsoft.com/dotnet/sdk:6.0 AS builder + +ARG ARCH=linux-x64 + +# Copy build context +WORKDIR /TShock +COPY . ./ + +# Build and package release +RUN dotnet build +WORKDIR /TShock/TShockLauncher +RUN dotnet publish -o output/ -r ${ARCH} -f net6.0 -c Release -p:PublishSingleFile=true --self-contained false + +# Runtime image +FROM mcr.microsoft.com/dotnet/runtime:6.0 AS runner +WORKDIR /server +COPY --from=builder /TShock/TShockLauncher/output ./ +RUN mkdir -p /tshock /worlds /plugins + +VOLUME ["/tshock", "/worlds", "/plugins"] +EXPOSE 7777 7878 + +ENTRYPOINT [ \ + "./TShock.Server", \ + "-configpath", "/tshock", \ + "-logpath", "/tshock/logs", \ + "-crashdir", "/tshock/crashes", \ + "-worldselectpath", "/worlds", \ + "-additionalplugins", "/plugins" \ +] \ No newline at end of file diff --git a/docs/changelog.md b/docs/changelog.md index 166645b3..7d277da5 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -64,6 +64,7 @@ Use past tense when adding new entries; sign your name off when you add or chang * Added an internationalization system. The base for the i18n system was built by Janet Blackquill ([@pontaoski](https://github.com/pontaoski)). A small donation in her honor was made to the [KDE project](https://kde.org/) as a thankyou for this work. This also includes the `TSHOCK_LANGUAGE` environment variable. Setting `TSHOCK_LANGUAGE=tok` will enable a small number of [Toki Pona](https://tokipona.org/) translations as a proof-of-concept. (@pontaoski) * Added support for Terraria 1.4.4.6, through OTAPI 3.1.5. (@SignatureBeef) * Added GeoIP.dat back to the included list of files. (@SignatureBeef) +* Dockerize TShock (@PotatoCider) ## TShock 4.5.18 * Fixed `TSPlayer.GiveItem` not working if the player is in lava. (@PotatoCider) From 108b19970b640bb984b11cfb9c14f18e46b97f7b Mon Sep 17 00:00:00 2001 From: Joseph Goh Date: Sun, 23 Oct 2022 06:10:36 +0800 Subject: [PATCH 2/3] Add support for multi-platform builds with docker buildx - also added docs --- .dockerignore | 2 +- Dockerfile | 38 +++++++++++++++++++------------------- docs/_sidebar.md | 1 + docs/docker.md | 12 ++++++++++++ 4 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 docs/docker.md diff --git a/.dockerignore b/.dockerignore index 002067b4..b20ac5b6 100644 --- a/.dockerignore +++ b/.dockerignore @@ -12,4 +12,4 @@ # but exclude build artifacts */bin/ -*/obj/ \ No newline at end of file +*/obj/ diff --git a/Dockerfile b/Dockerfile index 00a6f3e2..372ddff8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,32 +1,32 @@ +FROM --platform=${BUILDPLATFORM} mcr.microsoft.com/dotnet/sdk:6.0 AS builder -# Docker Instructions -# Build Image: -# docker build -t tshock . -# and run: -# docker run -p 7777:7777 -p 7878:7878 \ -# -v :/tshock \ -# -v :/worlds \ -# -v :/plugins \ -# --rm -it tshock -world /worlds/ - -FROM mcr.microsoft.com/dotnet/sdk:6.0 AS builder - -ARG ARCH=linux-x64 +ARG TARGETPLATFORM # Copy build context WORKDIR /TShock COPY . ./ -# Build and package release -RUN dotnet build +# Build and package release based on target architecture +RUN dotnet build -v m WORKDIR /TShock/TShockLauncher -RUN dotnet publish -o output/ -r ${ARCH} -f net6.0 -c Release -p:PublishSingleFile=true --self-contained false +RUN case "${TARGETPLATFORM}" in \ + "linux/amd64") export ARCH="linux-x64" \ + ;; \ + "linux/arm64") export ARCH="linux-arm64" \ + ;; \ + "linux/arm/v7") export ARCH="linux-arm" \ + ;; \ + "windows/amd64") export ARCH="win-x64" \ + ;; \ + *) echo "Error: Unsupported platform ${TARGETPLATFORM}" && exit 1 \ + ;; \ + esac && \ + dotnet publish -o output/ -r "${ARCH}" -v m -f net6.0 -c Release -p:PublishSingleFile=true --self-contained false # Runtime image -FROM mcr.microsoft.com/dotnet/runtime:6.0 AS runner +FROM --platform=${TARGETPLATFORM} mcr.microsoft.com/dotnet/runtime:6.0 AS runner WORKDIR /server COPY --from=builder /TShock/TShockLauncher/output ./ -RUN mkdir -p /tshock /worlds /plugins VOLUME ["/tshock", "/worlds", "/plugins"] EXPOSE 7777 7878 @@ -38,4 +38,4 @@ ENTRYPOINT [ \ "-crashdir", "/tshock/crashes", \ "-worldselectpath", "/worlds", \ "-additionalplugins", "/plugins" \ -] \ No newline at end of file +] diff --git a/docs/_sidebar.md b/docs/_sidebar.md index c352829e..56c82447 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -1,6 +1,7 @@ * [☕️⚡️ TShock documentation home](/) * [Changelog](/changelog.md) * [Command line parameters](/command-line-parameters.md) +* [Docker Setup](/docker.md) * Subsystems * [Tile providers](/tile-providers.md) diff --git a/docs/docker.md b/docs/docker.md new file mode 100644 index 00000000..5b8ce54b --- /dev/null +++ b/docs/docker.md @@ -0,0 +1,12 @@ +# Docker Setup + +## Build Image: +`docker build -t tshock .` +## and run: +```bash +docker run -p 7777:7777 -p 7878:7878 \ + -v :/tshock \ + -v :/worlds \ + -v :/plugins \ + --rm -it tshock [-world /worlds/] +``` \ No newline at end of file From 298f277bf0eda4bf2caa6ac630b8505c8015b3a1 Mon Sep 17 00:00:00 2001 From: Joseph Goh Date: Sun, 23 Oct 2022 07:02:49 +0800 Subject: [PATCH 3/3] Finish up docker docs and fix docker non-buildx builds --- Dockerfile | 26 +++++++++++++++----------- docs/docker.md | 47 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/Dockerfile b/Dockerfile index 372ddff8..186bee70 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,6 @@ +ARG TARGETPLATFORM=linux/amd64 +ARG BUILDPLATFORM=${TARGETPLATFORM} + FROM --platform=${BUILDPLATFORM} mcr.microsoft.com/dotnet/sdk:6.0 AS builder ARG TARGETPLATFORM @@ -9,17 +12,18 @@ COPY . ./ # Build and package release based on target architecture RUN dotnet build -v m WORKDIR /TShock/TShockLauncher -RUN case "${TARGETPLATFORM}" in \ - "linux/amd64") export ARCH="linux-x64" \ - ;; \ - "linux/arm64") export ARCH="linux-arm64" \ - ;; \ - "linux/arm/v7") export ARCH="linux-arm" \ - ;; \ - "windows/amd64") export ARCH="win-x64" \ - ;; \ - *) echo "Error: Unsupported platform ${TARGETPLATFORM}" && exit 1 \ - ;; \ +RUN \ + case "${TARGETPLATFORM}" in \ + "linux/amd64") export ARCH="linux-x64" \ + ;; \ + "linux/arm64") export ARCH="linux-arm64" \ + ;; \ + "linux/arm/v7") export ARCH="linux-arm" \ + ;; \ + "windows/amd64") export ARCH="win-x64" \ + ;; \ + *) echo "Error: Unsupported platform ${TARGETPLATFORM}" && exit 1 \ + ;; \ esac && \ dotnet publish -o output/ -r "${ARCH}" -v m -f net6.0 -c Release -p:PublishSingleFile=true --self-contained false diff --git a/docs/docker.md b/docs/docker.md index 5b8ce54b..89fea25b 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -1,12 +1,45 @@ # Docker Setup -## Build Image: -`docker build -t tshock .` -## and run: +In order to run TShock in a docker container, you would need to have mountpoints for + - `/tshock` (TShock config files, logs and crash reports) + - `/worlds` + - `/plugins` + - `/server` (optional, if you want to mount TShock's cwd) + +These folders can be mounted using `-v :` + +Open ports can also be passed through using `-p :`. + - `7777` for Terraria + - `7878` for TShock's REST API + +For Example: ```bash +# Building the image +docker build -t tshock:linux-x64 --build-arg TARGETPLATFORM=linux-x64 . + +# Running the image docker run -p 7777:7777 -p 7878:7878 \ - -v :/tshock \ - -v :/worlds \ - -v :/plugins \ - --rm -it tshock [-world /worlds/] + -v /home/cider/tshock/:/tshock \ + -v /home/cider/.local/share/Terraria/Worlds:/worlds \ + -v /home/cider/tshock/plugins:/plugins \ + --rm -it tshock:linux-x64 \ + -world /worlds/backflip.wld -motd "OMFG DOCKER" +``` + +## Building for Other Platforms + +Using `docker buildx`, you could build [multi-platform images](https://docs.docker.com/build/building/multi-platform/) for TShock. + +For Example: +```bash +# Building the image using buildx and loading it into docker +sudo docker buildx build -t tshock:linux-arm64 --platform linux/arm64 --load . + +# Running the image +docker run -p 7777:7777 -p 7878:7878 \ + -v /home/cider/tshock/:/tshock \ + -v /home/cider/.local/share/Terraria/Worlds:/worlds \ + -v /home/cider/tshock/plugins:/plugins \ + --rm -it tshock:linux-arm64 \ + -world /worlds/backflip.wld -motd "ARM64 ftw" ``` \ No newline at end of file