The first publish failed its smoke test: on the production host (2 slow cores) clang-tidy took over 30s on a two-line file and hit the runner's 20s lint limit, and even a trivial build took 4s, nearly all of it parsing <bits/stdc++.h>. The runner image now precompiles that header for g++ (.gch) and clang (.pch) with the flags the runner passes, and the runner uses them when they are there. g++ lint gains -O2 so the build's header matches it. Alpine 3.24's clang-tidy 22 walks far less of the system headers than 3.22's clang-tidy 20. Measured on the host: a build 3.9s to 0.6s, and clang-tidy 32.5s to 4.3s. Co-Authored-By: Claude Opus 5.5 <[email protected]>
77 lines
4.0 KiB
Docker
77 lines
4.0 KiB
Docker
# =============================================================================
|
|
# 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.
|
|
# Alpine 3.24 for clang-tidy 22, which walks far less of the
|
|
# system headers than 3.22's clang-tidy 20: about 4x faster on
|
|
# the same file, which on the production host was the
|
|
# difference between a 30s lint and a usable one.
|
|
#
|
|
# It also precompiles <bits/stdc++.h>, which every solution
|
|
# includes, into /opt/pch (internal/runner/pch.go): a .gch for
|
|
# g++ and a .pch for clang-tidy, built with the flags the runner
|
|
# passes (-std=c++17 -O2 -DLOCAL), since both compilers reject
|
|
# a header built differently. A build drops from about 4s to
|
|
# 0.6s on the production host.
|
|
#
|
|
# 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.24
|
|
RUN apk add --no-cache g++ musl-dev python3 clang-extra-tools ruff tini \
|
|
&& adduser -D -H -u 1000 app
|
|
RUN header=$(echo '#include <bits/stdc++.h>' | g++ -std=c++17 -x c++ -E -H - 2>&1 >/dev/null \
|
|
| grep -m1 'bits/stdc++\.h$' | sed 's/^\.* //') \
|
|
&& test -f "$header" \
|
|
&& mkdir -p /opt/pch/bits \
|
|
&& g++ -std=c++17 -O2 -DLOCAL -x c++-header "$header" -o /opt/pch/bits/stdc++.h.gch \
|
|
&& clang++ -std=c++17 -DLOCAL -x c++-header "$header" -o /opt/pch/stdc++.pch
|
|
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"]
|