Skip to content

Commit

Permalink
Merge pull request #339 from BZFlag-Dev/feature/container
Browse files Browse the repository at this point in the history
Add official Dockerfile to the project
  • Loading branch information
blast007 authored May 26, 2024
2 parents 0c2439f + df4af88 commit 04fc025
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Start the build image
FROM alpine:3 as build

# A comma seperated list of extra plugins to compile
ARG plugins=""

# The configure argument is passed to the ./configure script
ARG configure=""

# Update package list and upgrade, and install build dependencies
RUN apk update && apk upgrade --no-cache && \
apk add --no-cache autoconf automake c-ares-dev curl-dev g++ libtool make zlib

# Create and switch to a normal user for the build
RUN adduser -S builder
USER builder
WORKDIR /home/builder

# Copy the local source directory into the docker environment
COPY --chown=builder:nogroup . bzflag/
WORKDIR /home/builder/bzflag

# Run the build process
RUN ./autogen.sh && \
./configure \
--disable-bzadmin \
--disable-client \
--enable-custom-plugins="$plugins" \
$configure && \
make -j$(nproc)

# Switch to root to run the install step
USER root
RUN make install-strip

# Start the final image
FROM alpine:3

# Update package list and upgrade, and install runtime dependencies. Add a user
# and create a data directory owned by said user.
RUN apk update && apk upgrade --no-cache && \
apk add --no-cache c-ares libcurl libgcc libstdc++ zlib && \
adduser -S bzfsd && \
mkdir /data && chown bzfsd:nogroup /data

# Copy in files from the build
COPY --from=build /usr/local/bin/bzfs /usr/local/bin/bzfs
COPY --from=build /usr/local/lib/bzflag /usr/local/lib/bzflag

# Switch to the user and work out of the /data directory, which can be passed
# in as a volume.
USER bzfsd
WORKDIR /data

ENTRYPOINT [ "/usr/local/bin/bzfs" ]

65 changes: 65 additions & 0 deletions README.Container
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
BZFlag Server Container
=======================

We provide a Dockerfile for those who wish to run BZFlag servers inside containers. Adding custom plugins or source code
changes is easy as it copies the local source code into the build container. The example commands below will use the
docker command, but should also work with the podman command.

Building the container
----------------------

Building a stock BZFlag server container:
docker build . -t bzflag-server:latest

Custom plugins placed in the plugins/ directory can be built by passing a comma separated list of directory names as a
build argument:
docker build . -t bzflag-server:latest --build-arg plugins="UselessMine,mapchange"

It is also possible to supply custom arguments to the configure script:
docker build . -t bzflag-server:latest --build-arg configure="--enable-custom-plugins-file=plugins.txt"

Troubleshooting
---------------

> [stage-1 2/5] RUN apk update && apk upgrade --no-cache && apk add --no-cache c-ares libcurl libgcc libstdc++ zlib\
&& adduser -S bzfsd && mkdir /data && chown bzfsd:nogroup /data:
> 0.393 fetch https://dl-cdn.alpinelinux.org/alpine/v3.20/main/x86_64/APKINDEX.tar.gz
> 5.394 WARNING: updating and opening https://dl-cdn.alpinelinux.org/alpine/v3.20/main: temporary error (try again later)

If the build process fails due to network errors on the `apk update` step, then it may be a DNS problem with your Docker
engine configuration. Add the `--network=host` flag to your `docker build` command to use your host machine's DNS
instead of Docker's internal DNS.

Running a self-built image
--------------------------

Assume that the configuration, world, groupdb, etc for a given server are stored in a subdirectory of /srv/bzfs/, such
as /srv/bzfs/5154. The configuration file is at /srv/bzfs/5154/config.

Start a server that automatically starts when the system boots up (and restarts if bzfs exits or crashes):
docker run -d --restart unless-stopped -p 0.0.0.0:5154:5154/tcp -p 0.0.0.0:5154:5154/udp -v /srv/bzfs/5154:/data \
--name bzfs5154 bzflag-server:latest -conf config

Note that podman does not automatically start containers when the host system reboots. You must externally manage them
with some other method, such as a systemd unit. Podman supports the generation of unit files. Build and run the
container first. Then you need to use loginctl to enable linger so that the user unit can start without requiring you to
first log in to the user. Then you use podman to generate a systemd unit and enable it.
sudo loginctl enable-linger UsernameHere
mkdir -p ~/.config/systemd/user/
podman generate systemd --new --name bzfs5154 > ~/.config/systemd/user/bzfs5154.service
podman stop bzfs5154 && podman rm bzfs5154
systemctl --user daemon-reload
systemctl --user enable bzfs5154.service --now

List running and stopped containers:
docker ps -a

Stop a running server:
docker stop bzfs5154

Start a stopped server:
docker start bzfs5154

Remove a stopped server:
docker rm bzfs5154

0 comments on commit 04fc025

Please sign in to comment.