# ============================================================================= # The USACO site's code runner (cmd/usacorunner, internal/runner): compiles # and runs a solution against its tests, and lints it, for the solve page. # It runs whatever is submitted, so it is its own image and its own # container, apart from the site, which holds every secret. Two stages: # # build compiles the Go service, as in Dockerfile (go.mod/go.sum first, # so the dependency layer is cached across edits). # runtime alpine with the toolchains the service shells out to: # g++ + musl-dev builds C++ with the CLI's flags # python3 runs Python, and checks its syntax # clang-extra-tools clang-tidy, for lint (optional) # ruff Python lint (optional) # tini PID 1; see below # A missing linter only means less lint; see internal/runner. # # The build context is site/, like Dockerfile: no problems are in the image. # The site sends the source, its tests and any headers (debugging.h, # leetcode.h, driver.h) with each request. # # It runs as uid 1000 and writes only to $TMPDIR (/tmp), one directory per # request, which it removes afterwards. In production the container is # hardened around that (web_server's docker-compose.yml, service # usaco-runner): # read_only root, with a tmpfs at /tmp that must NOT be noexec, because the # compiled program runs from there; no network but the internal one the # site shares; no capabilities; no-new-privileges; mem_limit, cpus and a # pids_limit, which is what stops a fork bomb (the service does not set # RLIMIT_NPROC; limits.go says why). # Each program also gets its own rlimits (CPU, 512 MB address space, 64 MB # files) and a wall-clock limit, set by the service per test. # # tini is PID 1 because a program's orphans are re-parented to PID 1, and # the Go service never reaps processes it did not start: without an init, a # fork bomb's children stay zombies after the kill and hold the pids_limit # for good, and every later run fails to fork. # ============================================================================= FROM golang:1.26 AS build WORKDIR /src COPY go.mod go.sum ./ RUN --mount=type=cache,target=/go/pkg/mod go mod download COPY cmd ./cmd COPY internal ./internal RUN --mount=type=cache,target=/go/pkg/mod \ --mount=type=cache,target=/root/.cache/go-build \ CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/usacorunner ./cmd/usacorunner FROM alpine:3.22 RUN apk add --no-cache g++ musl-dev python3 clang-extra-tools ruff tini \ && adduser -D -H -u 1000 app COPY --from=build /out/usacorunner /app/usacorunner USER app ENV TMPDIR=/tmp EXPOSE 8081 # busybox's wget, which is enough for plain HTTP on localhost. HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \ CMD wget -qO- http://localhost:8081/health || exit 1 ENTRYPOINT ["/sbin/tini", "--"] CMD ["/app/usacorunner"]