j-c / terraria-aci

Docker images to run a Terraria Server on ACI
MIT License
4 stars 1 forks source link

Any interest in an Alpine image? #3

Open artburkart opened 4 years ago

artburkart commented 4 years ago

After playing with your code, I came up with an Alpine image for fun. I'm going to use a variation of it for the game server I'm running with my friends. I didn't want to create a PR or anything unless you were interested in it. As such, I'm just going to share the gist of it below.

run.sh file

#!/bin/sh
set -euo pipefail

tmux new-session -d -s terraria_session ./TerrariaServer.bin.x86_64 "$@"
tmux list-windows

echo To access the server after attaching to the container, use "tmux attach".
echo To exit from the session, use "<Ctrl+b> d" 

# Create default config files if they don't exist
if [ ! -f "/config/serverconfig.txt" ]; then
    cp ./serverconfig-default.txt /config/serverconfig.txt
fi

if [ ! -f "/config/banlist.txt" ]; then
    touch /config/banlist.txt
fi

# Naive checks once per minute to tell if the server is still running.
# https://docs.docker.com/config/containers/multi-service_container/

# We sleep before the loop to give the server some time to load the world file.
sleep 60
while sleep 5; do
    if ! pgrep TerrariaServer > /dev/null 2>&1; then
        echo TerrariaServer process not found. Quitting
        exit 1
    fi
done

Dockerfile

FROM alpine AS builder

WORKDIR /app
RUN wget -q -O terraria-server.zip https://www.terraria.org/system/dedicated_servers/archives/000/000/039/original/terraria-server-1405.zip \
  && unzip -q terraria-server.zip \
  && mv */Linux Linux \
  && mv */Windows/serverconfig.txt Linux/serverconfig-default.txt \
  && chmod +x Linux/TerrariaServer* \
  && if [ ! -f Linux/TerrariaServer ]; then echo "Missing Linux/TerrariaServer"; exit 1; fi

COPY run.sh /app/run.sh
RUN chmod +x /app/run.sh

FROM alpine:latest

RUN cd /tmp \
  && wget -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub \
  && wget -O glibc.apk https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.31-r0/glibc-2.31-r0.apk \
  && wget -O glibc-bin.apk https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.31-r0/glibc-bin-2.31-r0.apk \
  && apk add --no-cache tmux glibc.apk glibc-bin.apk \
  && rm -R /tmp/glibc* /etc/apk/keys/sgerrand.rsa.pub

WORKDIR /app
COPY --from=builder /app/Linux/ /app
COPY --from=builder /app/run.sh /app

VOLUME ["/config"]
ENTRYPOINT ["/app/run.sh"]
CMD ["-config", "/config/serverconfig.txt", "-banlist", "/config/banlist.txt"]

size (92.3MB)

small-terraria                                         latest              11009dbbceff        4 minutes ago       92.3MB

run command

docker run --rm -d -v $(pwd)/configs:/config \
  -p 7778:7777/udp -p 7778:7777 -p 8080:8080 \
  --name=terraria \
  small-terraria:latest -config /config/serverconfig.txt

Note, in my rendition, you can pass in whatever arguments you want to the container and it will respect them. In this case, rather than using the CMD defined inside the Dockerfile, it's using what I passed in. The effect of the command above is that there is no banlist used.

If you're not interested, no worries. I personally don't want to maintain a repository for this, so I thought it would be nice to at least put it up somewhere where others might be able to see it, and since this code is largely influenced by your work, I figured this was the best place. :+1: