·5 min read
Rust image for Pterodactyl: sizing cargo jobs from the cgroup CPU quota
A Rust build that works on my laptop can still kill a small game-server container, and the reason is a number the build tool reads from the wrong place. When I wrote the Rust runtime image for my Pterodactyl yolks, the part that took the most thought was not installing the toolchain. It was deciding how many parallel rustc jobs a container that is allowed one CPU should run on a host that has many more.
- Project
- Pterodactyl yolks
- Date
- Oct 2026
- Stack
- Docker, Bash, Rust, Pterodactyl
- Commit
- e028013
What the image has to do
A yolk in Pterodactyl is a Docker image plus a startup command. The user uploads a project, presses start, and whatever the image does next is the whole experience. For Rust that means: build the Cargo project in the current directory, then run what it produced. There is no separate build step in the panel, so the start script has to do both, and it has to do them every time the server restarts.
The Dockerfile keeps the toolchain read-only under /usr/local and puts everything that changes under /home/container:
ENV USER=container HOME=/home/container \
CARGO_HOME=/home/container/.cargo \
RUSTUP_TOOLCHAIN=${RUST_VERSION} \
CARGO_TERM_COLOR=alwaysThe registry, git checkouts and the target directory therefore live in the persistent volume. A restart rebuilds only what changed instead of compiling the dependency tree from scratch, which for a Rust project is the difference between a few seconds and many minutes.
The number nproc gets wrong
Cargo decides how many rustc processes to run from the CPU count it sees. Inside a container, nproc and the standard library calls behind it report the host's cores, not the limit the panel gave the server. The limit is enforced by the kernel through a cgroup quota, and the process is never told about it.
The failure mode is not slowness. A build with a dozen jobs on a container limited to one CPU gets throttled, but the bigger problem is memory: each rustc job holds a lot of RAM while it compiles a large crate, and the peaks line up. Pterodactyl servers have a memory limit too, and hitting it ends the container with an out-of-memory kill and a log that says very little.
The script reads the quota itself. This is the block from rust-start:
if [ -z "${CARGO_BUILD_JOBS:-}" ]; then
jobs=""
if [ -r /sys/fs/cgroup/cpu.max ]; then
read -r quota period < /sys/fs/cgroup/cpu.max
if [ "$quota" != "max" ] && [ "${period:-0}" -gt 0 ]; then
jobs=$(( (quota + period - 1) / period ))
fi
fi
[ -n "$jobs" ] && [ "$jobs" -ge 1 ] && export CARGO_BUILD_JOBS="$jobs"
fiOn cgroup v2, cpu.max holds two values: a quota and a period in microseconds, or the word max when there is no limit. A quota of 150000 over a period of 100000 means one and a half CPUs. The arithmetic rounds up, so that case runs two jobs rather than one: a fractional CPU should not serialise the whole build. When the file is missing or says max, the variable stays unset and cargo falls back to its own default. If the user sets CARGO_BUILD_JOBS in the panel, the script does not touch it, because the person who owns the server knows its memory better than a heuristic does.
Not letting a project change the toolchain
One image exists per Rust minor version, selected by a build argument, so a server on 1.99 really runs 1.99. The risk is a project that ships a rust-toolchain.toml. Rustup honours that file and, finding a toolchain it does not have, tries to install one into RUSTUP_HOME. That directory is read-only on purpose, so the start would fail with a permissions error that looks unrelated to the project.
Setting RUSTUP_TOOLCHAIN in the image environment overrides the file. The comment in the Dockerfile says exactly this, because it is the kind of line someone deletes later as redundant. The trade is plain: a project that truly needs a different version should pick the image for that version, not install one at boot.
Finding the binary without guessing
After cargo build --release the script needs to know what to execute. Guessing target/release/<package name> breaks for workspaces and for crates that rename their binary. Instead the build runs with --message-format=json-render-diagnostics, which still prints human-readable compiler errors but also emits one JSON message per artifact. The script saves that stream to a temp file and extracts every executable path:
mapfile -t bins < <(grep -o '"executable":"[^"]*"' "$manifest" | sed 's/^"executable":"//; s/"$//' | sort -u)Then the choice is explicit. If RUST_BIN is set, it must match one of the produced names, or the script says so and stops. With a single binary, that is the one. With none, the message suggests the crate may be a library. With several, it reads default-run from Cargo.toml, and only if that is missing does it ask the user to set RUST_BIN, listing the names it found. I would rather print a sentence a server owner can act on than start the wrong program.
One more detail: every line the script prints goes to stderr. Some entrypoints capture the startup command's stdout and execute it, and a status message on stdout would then run as a shell command.
What I took from it
None of these three pieces is clever. Each one is a place where a container hides a fact from the tool inside it: the CPU limit, the writable toolchain directory, the path of the artifact. The image works because the start script goes and asks, instead of trusting the defaults. I did not benchmark build times for this commit, so I will not claim a speedup. What I can say from the code is what it refuses to do: oversubscribe a quota it can read, reinstall a toolchain it cannot write, or run a binary it had to guess.
#docker#rust#cgroups#pterodactyl
Keep reading
Have a project in mind?