Files
pokerfreak/deploy/platinum/docker/Dockerfile.bot
T

69 lines
4.4 KiB
Docker

# syntax=docker/dockerfile:1.7
# Sandbox template image for UPLOADED USER BOTS (Python and TypeScript): the "match worker" sandbox.
#
# Build the arena image first (it carries the compiled sharingan-arena binary), then this one, from the repo root:
# podman build -f deploy/platinum/docker/Dockerfile --target arena -t ghcr.io/<org>/sharingan-arena:0.1.0 .
# podman build -f deploy/platinum/docker/Dockerfile.bot --build-arg ARENA_IMAGE=ghcr.io/<org>/sharingan-arena:0.1.0 \
# -t ghcr.io/<org>/sharingan-bot:0.1.0 .
# Then register it as a Platinum template (deploy/platinum/runner/src/provision.ts, templates/sharingan-bot.json).
#
# Contents
# /usr/local/bin/sharingan-arena the match worker (`sharingan-arena run --bot "<cmd>" ...`): engine + house bots + AIVAT,
# spawns the user's bot as a stdin/stdout subprocess (`sharingan pipe mybot.py`)
# python3 + `sharingan` CLI (sdk/python; `sharingan pipe`, and `sharingan connect` for the remote-bot mode)
# node 22 + poker-sharingan (sdk/typescript) + ws, launch_bot.mjs
# /opt/sharingan/launch_bot.{py,mjs} thin wrappers that CONNECT a bot to /v1/play (remote-bot mode only)
# The image has no entrypoint process: the runner starts everything with sandbox.process.start().
# The user's bot runs unprivileged (each rotation of duplicate deals as its own uid, 10010..10015; installs as 10002); the worker runs as root and keeps its signing key (written at run time,
# mode 0400) out of the bot's reach.
ARG ARENA_IMAGE=localhost/sharingan-arena:local
FROM ${ARENA_IMAGE} AS arena-bin
FROM docker.io/library/node:22-bookworm-slim AS node
FROM docker.io/library/node:22-bookworm-slim AS tsbuild
WORKDIR /sdk
COPY sdk/typescript/ ./
RUN npm install --no-audit --no-fund && npm run build
# runtime dependency of the SDK's WebSocket client, installed on its own (the SDK lists ws as a peer)
RUN mkdir /wsdep && cd /wsdep && npm init -y >/dev/null && npm install --omit=dev --no-audit --no-fund ws@8
FROM docker.io/library/python:3.12-slim-bookworm
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates tar util-linux coreutils && rm -rf /var/lib/apt/lists/*
# match worker (built on debian bookworm, same glibc as this base)
COPY --from=arena-bin /usr/local/bin/sharingan-arena /usr/local/bin/sharingan-arena
# node 22 runtime (copied from the official image; no apt repo needed)
COPY --from=node /usr/local/bin/node /usr/local/bin/node
ENV NODE_OPTIONS="--experimental-strip-types --no-warnings"
# Python SDK
COPY sdk/python /tmp/sdk-python
RUN pip install --no-cache-dir /tmp/sdk-python && rm -rf /tmp/sdk-python
# TypeScript SDK: laid out so `import "poker-sharingan"` resolves for any file under /opt/sharingan
RUN mkdir -p /opt/sharingan/node_modules/poker-sharingan /opt/sharingan/bot
COPY --from=tsbuild /sdk/dist /opt/sharingan/node_modules/poker-sharingan/dist
COPY --from=tsbuild /sdk/package.json /opt/sharingan/node_modules/poker-sharingan/package.json
COPY --from=tsbuild /wsdep/node_modules/ws /opt/sharingan/node_modules/ws
COPY deploy/platinum/bot/launch_bot.py /opt/sharingan/launch_bot.py
COPY deploy/platinum/bot/launch_bot.mjs /opt/sharingan/launch_bot.mjs
COPY deploy/platinum/bot/sharingan-scrub /usr/local/bin/sharingan-scrub
RUN chmod -R a+rX /opt/sharingan && chmod 0755 /usr/local/bin/sharingan-scrub \
&& mkdir -p /opt/sharingan/bot /opt/sharingan/deps /work && chmod 0755 /opt/sharingan/bot /opt/sharingan/deps /work
# Unprivileged users. The bot's install (pip) and the remote-bot mode run as 10002; the hosted worker runs each rotation of
# duplicate deals as its own user, 10010..10015, and wipes what it left before the next (sharingan-scrub). No home
# directories: HOME is /tmp, a size-capped tmpfs in the sandbox. Nothing on the image is writable by them.
RUN useradd -u 10002 -M -d /tmp -s /usr/sbin/nologin bot \
&& for i in 0 1 2 3 4 5; do useradd -u $((10010 + i)) -M -d /tmp -s /usr/sbin/nologin "rot$i" || exit 1; done \
&& { [ ! -d /run/lock ] || chmod 0755 /run/lock; } \
&& find / -xdev -perm /6000 -type f -exec chmod a-s {} + \
&& ww="$(find / -xdev -perm -0002 \( -type d -o -type f \) ! -path /tmp ! -path /var/tmp 2>/dev/null)" \
&& { [ -z "$ww" ] || { echo "world-writable paths in the bot image (a bot could keep notes there): $ww" >&2; exit 1; }; }
WORKDIR /opt/sharingan/bot
CMD ["sleep", "infinity"]