From 4dfd45443058168d696e1f20a9fe445aa9d35088 Mon Sep 17 00:00:00 2001 From: Ryan Sheehan Date: Sat, 23 May 2020 00:48:48 -0500 Subject: [PATCH 1/5] initial docker build --- .dockerignore | 15 +++++++++++++++ docker/bootstrap.sh | 26 +++++++++++++++++++++++++ dockerfile | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 .dockerignore create mode 100644 docker/bootstrap.sh create mode 100644 dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..e2f45a4a8 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,15 @@ +bin +obj +*.suo +/*.user +packages/ + +.DS_Store + +Output/ + +*.exe +*.pdb +*.dll + +.idea/ diff --git a/docker/bootstrap.sh b/docker/bootstrap.sh new file mode 100644 index 000000000..5d78f6e6c --- /dev/null +++ b/docker/bootstrap.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +echo "\nBootstrap:\nworld_file_name=$WORLD_FILENAME\nconfigpath=$CONFIGPATH\nlogpath=$LOGPATH\n" +echo "Copying plugins..." +cp -Rfv /plugins/* ./ServerPlugins + +WORLD_PATH="/root/.local/share/Terraria/Worlds/$WORLD_FILENAME" + +if [ -z "$WORLD_FILENAME" ]; then + echo "No world file specified in environment WORLD_FILENAME." + if [ -z "$@" ]; then + echo "Running server setup..." + else + echo "Running server with command flags: $@" + fi + mono --server --gc=sgen -O=all TerrariaServer.exe -configPath "$CONFIGPATH" -logpath "$LOGPATH" "$@" +else + echo "Environment WORLD_FILENAME specified" + if [ -f "$WORLD_PATH" ]; then + echo "Loading to world $WORLD_FILENAME..." + mono --server --gc=sgen -O=all TerrariaServer.exe -configPath "$CONFIGPATH" -logpath "$LOGPATH" -world "$WORLD_PATH" "$@" + else + echo "Unable to locate $WORLD_PATH.\nPlease make sure your world file is volumed into docker: -v :/root/.local/share/Terraria/Worlds" + exit 1 + fi +fi diff --git a/dockerfile b/dockerfile new file mode 100644 index 000000000..cb581a866 --- /dev/null +++ b/dockerfile @@ -0,0 +1,47 @@ +FROM mono:6.8.0.96 AS build + +ARG BUILD_MODE=Release + +ADD https://dist.nuget.org/win-x86-commandline/v5.5.1/nuget.exe / +COPY . /src +RUN chmod +x /nuget.exe && \ + mono /nuget.exe restore /src/TerrariaServerAPI/ && \ + xbuild /src/TerrariaServerAPI/TShock.4.OTAPI.sln /p:Configuration=$BUILD_MODE && \ + cd /src/TerrariaServerAPI/TShock.Modifications.Bootstrapper/bin/$BUILD_MODE && \ + mono TShock.Modifications.Bootstrapper.exe -in=OTAPI.dll -mod=../../../TShock.Modifications.**/bin/$BUILD_MODE/TShock.Modifications.*.dll -o=Output/OTAPI.dll && \ + cd / && \ + xbuild /src/TerrariaServerAPI/TerrariaServerAPI/TerrariaServerAPI.csproj /p:Configuration=$BUILD_MODE && \ + mono /nuget.exe restore /src/ && \ + xbuild /src/TShock.sln /p:Configuration=$BUILD_MODE + +FROM mono:6.8.0.96-slim + + +# documenting ports +EXPOSE 7777 7878 + +# env used in the bootstrap +ENV CONFIGPATH=/root/.local/share/Terraria/Worlds +ENV LOGPATH=/tshock/logs +ENV WORLD_FILENAME="" + +# Allow for external data +VOLUME ["/root/.local/share/Terraria/Worlds", "/tshock/logs", "/plugins"] + +# copy game files +COPY --from=build /src/TShockAPI/bin/Release/ /tshock/ + +# copy bootstrapper +COPY ./docker/bootstrap.sh /tshock/bootstrap.sh + +# install nuget to grab tshock dependencies +RUN apt-get update -y && \ + apt-get install -y nuget && \ + rm -rf /var/lib/apt/lists/* /tmp/* && \ + chmod +x /tshock/bootstrap.sh + +# Set working directory to server +WORKDIR /tshock + +# run the bootstrap, which will copy the TShockAPI.dll before starting the server +ENTRYPOINT [ "/bin/sh", "bootstrap.sh" ] \ No newline at end of file From ab0ddedbfa2ed52771d567f55525745b2cdefb8e Mon Sep 17 00:00:00 2001 From: Ryan Sheehan Date: Sat, 23 May 2020 02:17:54 -0500 Subject: [PATCH 2/5] fix packaging of files in build stage --- docker/bootstrap.sh | 4 ++-- dockerfile | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/docker/bootstrap.sh b/docker/bootstrap.sh index 5d78f6e6c..122fa45ea 100644 --- a/docker/bootstrap.sh +++ b/docker/bootstrap.sh @@ -13,12 +13,12 @@ if [ -z "$WORLD_FILENAME" ]; then else echo "Running server with command flags: $@" fi - mono --server --gc=sgen -O=all TerrariaServer.exe -configPath "$CONFIGPATH" -logpath "$LOGPATH" "$@" + mono --server --gc=sgen -O=all TerrariaServer.exe -configpath "$CONFIGPATH" -logpath "$LOGPATH" "$@" else echo "Environment WORLD_FILENAME specified" if [ -f "$WORLD_PATH" ]; then echo "Loading to world $WORLD_FILENAME..." - mono --server --gc=sgen -O=all TerrariaServer.exe -configPath "$CONFIGPATH" -logpath "$LOGPATH" -world "$WORLD_PATH" "$@" + mono --server --gc=sgen -O=all TerrariaServer.exe -configpath "$CONFIGPATH" -logpath "$LOGPATH" -world "$WORLD_PATH" "$@" else echo "Unable to locate $WORLD_PATH.\nPlease make sure your world file is volumed into docker: -v :/root/.local/share/Terraria/Worlds" exit 1 diff --git a/dockerfile b/dockerfile index cb581a866..dd05e1f57 100644 --- a/dockerfile +++ b/dockerfile @@ -5,6 +5,7 @@ ARG BUILD_MODE=Release ADD https://dist.nuget.org/win-x86-commandline/v5.5.1/nuget.exe / COPY . /src RUN chmod +x /nuget.exe && \ + # build TShock mono /nuget.exe restore /src/TerrariaServerAPI/ && \ xbuild /src/TerrariaServerAPI/TShock.4.OTAPI.sln /p:Configuration=$BUILD_MODE && \ cd /src/TerrariaServerAPI/TShock.Modifications.Bootstrapper/bin/$BUILD_MODE && \ @@ -12,11 +13,20 @@ RUN chmod +x /nuget.exe && \ cd / && \ xbuild /src/TerrariaServerAPI/TerrariaServerAPI/TerrariaServerAPI.csproj /p:Configuration=$BUILD_MODE && \ mono /nuget.exe restore /src/ && \ - xbuild /src/TShock.sln /p:Configuration=$BUILD_MODE + xbuild /src/TShock.sln /p:Configuration=$BUILD_MODE && \ + # create final output + mkdir -p /out/ServerPlugins && \ + cp /src/packages/BCrypt.Net.0.1.0/lib/net35/* /out/ && \ + cp /src/packages/MySql.Data.6.9.8/lib/net45/* /out/ && \ + cp /src/packages/Newtonsoft.Json.10.0.3/lib/net45/* /out/ && \ + cp /src/prebuilts/* /out/ && \ + cp /src/TerrariaServerAPI/TerrariaServerAPI/bin/$BUILD_MODE/OTAPI.dll /out/ && \ + cp /src/TerrariaServerAPI/TerrariaServerAPI/bin/$BUILD_MODE/TerrariaServer.* /out/ && \ + cp /src/TShockAPI/bin/$BUILD_MODE/TShockAPI.* /out/ && \ + mv /out/TShockAPI.dll /out/ServerPlugins/TShockAPI.dll FROM mono:6.8.0.96-slim - # documenting ports EXPOSE 7777 7878 @@ -29,7 +39,7 @@ ENV WORLD_FILENAME="" VOLUME ["/root/.local/share/Terraria/Worlds", "/tshock/logs", "/plugins"] # copy game files -COPY --from=build /src/TShockAPI/bin/Release/ /tshock/ +COPY --from=build /out/ /tshock/ # copy bootstrapper COPY ./docker/bootstrap.sh /tshock/bootstrap.sh From 433bc7b2b3e360efccde2d99c8400de1b57901b1 Mon Sep 17 00:00:00 2001 From: Ryan Sheehan Date: Sat, 23 May 2020 02:25:32 -0500 Subject: [PATCH 3/5] added docker readme --- docker/README.md | 167 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 docker/README.md diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 000000000..de05ad1b9 --- /dev/null +++ b/docker/README.md @@ -0,0 +1,167 @@ +# TShock Terraria Server + +## Quick start guide + +First you need a machine with [Docker][Docker] installed. Everything from here on out assumes the docker service is running _(you may need to start the service after install)_. + +### Create directory to save your world to + +Next create a directory for your world file, configuration, and logs + +```bash +mkdir -p $HOME/terraria/world +``` + +### Creating a fresh world + +For the first run you will need to generate a new world with a size where: _1=Small, 2=Medium, 3=Large_ + +```bash +sudo docker run -it -p 7777:7777 --rm -v $HOME/terraria/world:/root/.local/share/Terraria/Worlds terraria:latest -world /root/.local/share/Terraria/Worlds/.wld -autocreate +``` + +**Note:** If you close the the terminal, the server will stop running. You will need to restart with a preexisting world. It may +be worth while to close after creation anyway to update the initial `config.json` settings. + +To create a world with a few more initial options, you can do so in an interactive mode. + +```bash +sudo docker run -it -p 7777:7777 --rm -v $HOME/terraria/world:/root/.local/share/Terraria/Worlds terraria:latest +``` + +### To start with a preexisting world + +```bash +sudo docker run -d --rm -p 7777:7777 -v $HOME/terraria/world:/root/.local/share/Terraria/Worlds --name="terraria" -e WORLD_FILENAME=<.wld world_filename_here> terraria:latest +``` + +**Note:** This command is designed to run in the background, and it is safe to close the terminal window. + +Any `config.json` in the directory will automatically be loaded. The `.wld` should be the name of your wld file in your $HOME/terraria/world directory. + +## Updating your container + +Updating is easy! + +1. Grab the latest terraria container + + ```bash + docker pull terraria:latest + ``` + +2. First we need to find our running container to stop, so we can later restart with the latest + + ```bash + docker container ls | grep terraria + ``` + + The first few numbers and letters, on a line, are the container hash. Remember the first 3 or so letters or numbers + + Example: + + ```bash + f25261ac55a4 terraria:latest "/bin/sh bootstrap.s…" 3 minutes ago Up 3 minutes 0.0.0.0:7777->7777/tcp, 7878/tcp reverent_solomon + ``` + + `f25` would be the first few letters/numbers of the container hash + + **NOTE:** If you see multiple lines, find the one that still has an `up` status. + +3. Stop and remove the container + + ```bash + docker container rm -f xxx # xxx is the letters/numbers from the last step + ``` + +4. Start your container again with your world _(see the [Quick start](#Quick-start-guide))_ + +## [Virtual] Machine Setup + +Provision a linux machine that can support docker and containerization. For more information visit [docker][Docker]. For a small or medium world with no more than 8 users a linux machine with 1-1.5GB of ram should suffice. **If you are running a vm in the cloud, make sure to expose tcp port 7777 and udp port 7777.** + +## Running a container image + +Whether you build your own container, or use [my container](https://hub.docker.com/r/terraria) published to docker hub, +we are ready to run our terraria server! + +**Note:** For a full set of docker run options go [here](https://docs.docker.com/engine/reference/run/) + +### First run + +The first run of the server will give you the opportunity to create a world, and it will generate +the server's config file. You may wish to add the config file for many reasons, but one of which is to +add a password to your server. + +```bash +docker run -it --rm -p 7777:7777 -v $HOME/terraria/world:/root/.local/share/Terraria/Worlds terraria:latest +``` + +Let's break down this command: + +| Command Part | Description | +| ------------ | ----------- | +| `docker run` | tells linux to run a docker image | +| `-it` | run interactively and output the text to terminal | +| `--rm` | remove docker container when the container stops or fails | +| `-p 7777:7777` | exposes terraria port <host machine side>:<container side> | +| `-v $HOME/terraria/world:/root/.local/share/Terraria/Worlds` | maps a folder on the host machine into the container for saving the .wld file. This does not have to be `$HOME/terraria/world`. Anything left of the `:` is host machine directory | +| `terraria` | the name of the docker image that will run. | +| `:latest` | the tag, which defaults to `latest` if not specified. `latest` is the most recently published container | + +* The config file can be found in the directory specified by the `-v` volume. +* If the terminal window is shut down, that will exit the process. Make sure to do so after the world is created! + +### Running with an existing generated world + +After a world has been generated, you may want to load directly into it. + +```bash +docker run -d --rm -p 7777:7777 -v $HOME/terraria/world:/root/.local/share/Terraria/Worlds terraria:latest -world /root/.local/share/Terraria/Worlds/.wld +``` + +Let's break down the command: + +| Command Part | Description | +| ------------ | ----------- | +| `-d` | run this in the background. It is okay to close the terminal window, the container will continue to run | +| `-world /root/.local/share/Terraria/Worlds/.wld` | specifies the world file name you wish to immediately load into | + +* for the other parts check out the [First run](#First-run) section +* check out additional server startup flags [here](https://tshock.readme.io/docs/command-line-parameters). They go on +after the `terraria:latest` portion of the line + +## Plugin support + +A volume exists to support plugins. Create a folder, not inside your `/world` folder, for your plugins + +```bash +mkdir ServerPlugins +``` + +Mount the plugins directory with an additional -v switch on your `docker run ...` command + +```bash +-v :/plugins +``` + +## Logs + +A separate directory can be volumed in for storing logs outside of the image + +```bash +-v :/tshock/logs +``` + +## *Notes* + +* `sudo` may be required to run docker commands. + +* Please post to the [TShock](https://github.com/Pryaxis/TShock/discussions) team with questions on how to run a server. + +* Any [additional command-line instructions](https://tshock.readme.io/docs/command-line-parameters) can be added to the end of either method for launching a server. Docker maps the $HOME/terraria/world linux-host folder to the /tshock/world container-folder. + +* Expecting your server to run for a while? Add `--log-opt max-size=200k` to limit your log file size. Otherwise one day you will wake up to see all your hdd space chewed up for a terraria docker setup! + + +[TShock]: https://github.com/Pryaxis/TShock/releases +[Docker]: https://docs.docker.com/get-docker/ From a36bcd3f70e0fb90758f5ec5d63ef8f3f847158a Mon Sep 17 00:00:00 2001 From: Ryan Sheehan Date: Sat, 23 May 2020 02:31:59 -0500 Subject: [PATCH 4/5] Updated Changelog to show dockerfile addition --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bfac74d7..2eceb579c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin ## TShock 4.4.0 (Pre-release ?) * Fix bed spawn issues when trying to remove spawn point in SSC (@Olink) * Fix Snake Flute (@Olink) +* Added Dockerfile for docker support ## TShock 4.4.0 (Pre-release 6) * Updates to OTAPI 2.0.0.35 (@DeathCradle). From f4c091c59848a6acfdb3f7cf6b53d949abd1f065 Mon Sep 17 00:00:00 2001 From: Ryan Sheehan Date: Sat, 23 May 2020 11:15:21 -0500 Subject: [PATCH 5/5] Rename dockerfile to Dockerfile --- dockerfile => Dockerfile | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename dockerfile => Dockerfile (100%) diff --git a/dockerfile b/Dockerfile similarity index 100% rename from dockerfile rename to Dockerfile