diff --git a/README.md b/README.md index 12f3a0d9..54806b07 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,18 @@ This does not require the RCON connection to be exposed. docker exec factorio rcon /h ``` +### Server health + +Regular and rootless images check server responsiveness using the read-only `rcon /players count` command, with a five-second timeout. RCON does not need to be published, and the check does not affect achievements. + +```shell +docker inspect --format '{{.State.Health.Status}}' factorio +``` + +The default interval is 30 seconds, with three retries and a five-minute startup grace period. For large saves or heavy mod sets, adjust Compose `healthcheck.start_period`, `interval` and `retries` to allow for loading and save pauses. Use `healthcheck.disable: true` (or `docker run --no-healthcheck`) if you intentionally disable or replace RCON. + +Docker reports `starting`, `healthy` or `unhealthy`; it does not automatically restart unhealthy containers. Use this status with your monitoring tool. The check does not test external UDP connectivity or provide an HTTP endpoint. Keep the Docker API and RCON private. + ### Upgrading Before upgrading backup the save. It's easy to make a save in the client. diff --git a/docker/Dockerfile b/docker/Dockerfile index e17ea1c7..75cd50e9 100755 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -103,4 +103,6 @@ COPY --from=rcon-builder /src/rcon /bin/rcon VOLUME /factorio EXPOSE $PORT/udp $RCON_PORT/tcp +HEALTHCHECK --interval=30s --timeout=10s --start-period=5m --retries=3 CMD ["/docker-healthcheck.sh"] + ENTRYPOINT ["/docker-entrypoint.sh"] diff --git a/docker/Dockerfile.rootless b/docker/Dockerfile.rootless index a8d837d8..9fca4af0 100644 --- a/docker/Dockerfile.rootless +++ b/docker/Dockerfile.rootless @@ -97,4 +97,6 @@ EXPOSE $PORT/udp $RCON_PORT/tcp # Run as non-root user (UID 1000 is common for the first user in rootless containers) USER 1000:1000 +HEALTHCHECK --interval=30s --timeout=10s --start-period=5m --retries=3 CMD ["/docker-healthcheck.sh"] + ENTRYPOINT ["/docker-entrypoint.sh"] \ No newline at end of file diff --git a/docker/files/docker-healthcheck.sh b/docker/files/docker-healthcheck.sh new file mode 100755 index 00000000..9e1a5dfc --- /dev/null +++ b/docker/files/docker-healthcheck.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -euo pipefail + +# Avoid invoking the RCON client before startup has created its password file. +[[ -s "${CONFIG:?}/rconpw" && -r "$CONFIG/rconpw" ]] || exit 1 + +# A TCP connection alone cannot detect failed authentication or a stuck server. +response=$(timeout 5s rcon /players count) || exit 1 +[[ "$response" =~ ^Players\ \([0-9]+\): ]]