Docker Provider
The docker provider runs each sandbox as one long-lived container on the local Docker daemon. It shells out to the docker CLI (docker run, docker exec, docker cp, docker rm), so it needs a running daemon but no OpenSandbox server, control plane, or Kubernetes. Use it for local development, CI, and single-machine setups where Docker is the available runtime.
Setup
Install Docker and make sure the docker binary is on PATH with a running daemon. The provider does not install or start Docker; constructing it raises RuntimeError if the binary is missing, and create() fails if the daemon is unreachable.
Images can be any reference the daemon can run, such as ubuntu:22.04, python:3.13.14-slim, or a registry reference. An Apptainer-style docker:// prefix is tolerated and stripped. GPU passthrough needs the NVIDIA Container Toolkit; CPU and memory limits need cgroups.
Provider Config
NeMo Gym ships a Docker provider config at nemo_gym/sandbox/providers/docker/configs/docker.yaml. It defines a top-level sandbox block that agents reference with sandbox_provider: sandbox.
Run an agent with the provider config by passing it alongside the agent and model configs:
The provider constructor accepts three optional config sections:
SandboxSpec Provider Options
Set Docker-specific create options under SandboxSpec.provider_options, or under sandbox_spec.provider_options in an agent config:
Relevant SandboxSpec Fields
Resource Mapping
SandboxResources is translated into Docker CLI flags when create.apply_resource_limits is enabled:
Lifecycle
The provider runs one detached container per sandbox:
Containers are named nemo-gym-<uuid>, and the image ENTRYPOINT is overridden with a keep-alive command so images that define their own entrypoint (common for SWE benchmark images) stay up across exec() calls. State written by one command is visible to later commands in the same sandbox. --init inserts a minimal init process so the many short docker exec children are reaped instead of accumulating as zombies.
File Transfer
Uploads and downloads use docker cp in both directions, creating the target’s parent directory first on upload. Uploaded files are owned by root; read them as root or chown them for a non-root user. Download any files you need before stopping the sandbox — stopping force-removes the container.
Interactive Terminals
The provider implements SupportsSandboxPty, so await sandbox.pty.create(...), await sandbox.pty.exec(...) and the
interactive sandbox shell (python -m nemo_gym.sandbox.shell) work on Docker as they do on OpenSandbox. A harness that
drives a terminal session, such as a Terminus-2-style PTY harness, can build on them. The Terminus-2 sandboxed agent in
this repository drives tmux over exec instead.
How a session runs
- In PTY mode (the default), each session is one
docker exec -i -tclient running under a host pseudo-terminal of its own. The CLI sees a terminal, allocates one in the container and forwards bytes both ways; the session reads and writes the host terminal’s master side. rowsandcolssize the host terminal before the client starts, and the CLI applies that size to the container’s terminal when the exec starts, so a program that reads its size at once sees the requested one.resize()changes the host terminal’s size and sends the clientSIGWINCH, which the CLI turns into a resize of the container’s terminal.cwd,envandusermap to-w,--envand--user, as forexec().pty=Falseruns the client on plain pipes:read()returns stdout andread_stderr()stderr.- With no command,
create()starts a shell:bashwhen the image has it, elsesh. wait_exit()returns the program’s exit status asdocker execreports it.- Session clients are long-lived and are not counted against
exec.concurrency. Stopping the sandbox releases its sessions beforedocker rm -f, andaclose()on the provider closes any still open.
The pid record
A one-line launcher records the program’s pid, then execs the program. send_signal() and close() reach the process
through that pid.
- The record goes to
/tmp/.nemo-gym-pty-<session id>.pid, or to/dev/shm/.nemo-gym-pty-<session id>.pidwhen/tmprefuses the write (a container withread_only: trueand no tmpfs on/tmp)./dev/shmis a writable tmpfs in every container that does not run with--ipc=none; under--ipc=hostit is the host’s/dev/shm. - A session whose pid can be recorded in neither place could be neither signalled nor cleaned up, so the launcher does not start
it. It prints
nemo-gym: cannot record the pid of this PTY session in ...on stderr (the terminal in PTY mode) and exits 1, and the session ends at once with that message.create_pty()itself returns as soon as the client is spawned, without a round trip. send_signal()andclose()read the record the first time they need it, waiting up to two seconds for the launcher to write it: a call made right aftercreate_pty()uses a seconddocker execthat races the session’s own to the daemon.
Signals and close
send_signal()takes"SIGINT","SIGTERM","SIGKILL","SIGQUIT"or"SIGHUP"and runskillas root through a seconddocker exec.- In PTY mode it signals the foreground process group of the session’s terminal (read from
/proc/<pid>/stat), which is what a key press would reach: the program of a command session, or the job an interactive shell is running (the idle shell itself when there is none). A shell with job control runs each job in its own process group, so a signal to the shell’s group would never reach the running command. - In pipe mode, which has no terminal, it signals the process’s own group, then the process.
- In PTY mode it signals the foreground process group of the session’s terminal (read from
- A docker client that goes away does not end the process it started, so
close()of a live session ends it through one more exec:SIGHUPto the recorded process’s group, up to one second for a shell to relay the hangup to its jobs (under job control, an interactive shell’s foreground and background jobs sit in process groups of their own, which a signal to the shell’s group never reaches), thenSIGKILLto every process still in the recorded process’s session, then to its group and the process itself (all a pipe-mode process that leads no session gets). Both pid paths are removed with it.close()of a session whose process already exited only removes them. - The in-container
killlines work in busyboxsh(Alpine-based images) as well as dash and bash (kill -INT -<pgid>).
How the pid record is protected against tampering
The session’s own user writes the pid file and can rewrite it, or replace it with a link to a file it does not own (in a
world-writable /tmp a hard link keeps the linked inode’s owner). So neither the file’s content nor its owner is
trusted as such:
-
It is read as the session’s own user, at most 32 bytes, under a timeout of its own:
PID_READ_TIMEOUT_S(30 seconds), orexec.default_timeout_swhen that is shorter. A pid that passes the checks below is kept; after a failed read, the nextsend_signal()orclose()reads again. A path that is not a regular file (a FIFO, or a link to a device) is refused at once rather than opened, and the file is tested again on its opened descriptor, so swapping the path after the test cannot hand the read something that blocks. -
The read runs as the session’s user because
/proc/<pid>/environis readable by the process’s own uid but not by a root withoutCAP_SYS_PTRACE, which Docker’s default capability set does not grant, so a root-side read would fail for every non-root session. Thekilland the cleanup run as root and need no added capability. -
The pid is followed only when it is a plain pid above 1 whose process:
- carries the session’s marker,
NEMO_GYM_PTY_SESSION=<session id>, in its environment (set by the session’s owndocker exec --env, which a process can pass only to its own children); - has its parent outside the container (
PPid0 in the container’s pid namespace, true of a process the runtime injected and of nothing started inside it, the agent’s own children included); - and, in PTY mode, leads its session.
Otherwise
send_signal()raisesSandboxPtyErrorandclose()leaves the process to the container’s removal. - carries the session’s marker,
-
A rewritten or relinked record can therefore never aim the root-side
killat a process the session did not start. The session’s user can make its record useless, but cannot makesend_signal()orclose()wait on it past the two-second wait: a record that is not a regular file is refused at once, and any other unusable record (rewritten, relinked, emptied or removed) is refused by the end of that wait, sosend_signal()raises andclose()only removes the record.
Errors
When the daemon refuses the exec (the container is paused or gone, or the daemon is unreachable), the client exits
without starting the program. Gym tells this from the client’s stderr, using the same lines that make exec() return
error_type="sandbox", and ends the session with SandboxPtyError from wait_exit() and read() instead of
reporting the client’s exit code as the program’s. So sandbox.pty.exec() raises where sandbox.exec() returns the
runtime sentinel. In PTY mode the client’s own stderr is kept apart from the terminal output for this; in pipe mode it
is the program’s stderr too, and is classified as exec() classifies its stderr. If the sandbox is stopped while
create() opens a session, create() raises SandboxPtyError.
Limitations
- No re-attach. Sessions live in the Gym process, not in the sandbox, so another process cannot re-attach by id
(
SupportsSandboxPtyAttachis not implemented). detach=Trueruns attached.pty.exec(detach=True)stays attached, since there is no proxied connection to spare between polls.- Host pid namespace.
send_signal()andclose()rely on the container’s own pid namespace (PPid0 marks the runtime’s execs), so a container that shares the host’s (--pid=hostthroughextra_run_args) cannot be signalled or cleaned up through them. - Programs that change user. A session whose program changes its user or
execs a setuid program (su,sudo -i) has an environment the session’s user can no longer read, sosend_signal()raises andclose()leaves the process to the container’s removal. - No writable
/tmpor/dev/shm. A container with neither (read_only: truewithout a tmpfs on/tmp, and--ipc=none) cannot host a PTY session; it ends at once with the launcher’s message.
Isolation and Security
Docker containers share the host kernel. This is process and namespace isolation, not a virtual machine, and is not a hard boundary for hostile code. For adversarial or untrusted workloads, run Docker on a disposable VM or use a stronger runtime such as gVisor, Kata Containers, or a microVM.
- Never run with
--privilegedand never mount the Docker socket into the sandbox; either grants host root. - Networking is on by default because most tasks need egress (pip, git). Set
network: noneto cut it entirely, ornetwork: <name>to attach a locked-down internal network. - Harden with
read_only: true,cap_drop: ["ALL"],security_opt: ["no-new-privileges"], andpids_limit: <n>as a fork-bomb guard. Enforceresourcesso one sandbox cannot starve the host.read_only: trueleaves/tmpread-only too (addextra_run_args: ["--tmpfs", "/tmp"]for programs that need one); PTY sessions keep working, their pid record moves to/dev/shm(see Interactive Terminals). - Rootless Docker narrows the blast radius (container root is not host root) and is recommended for shared machines;
docker cpownership and some--userbehaviors differ under it. - Every container is labeled
nemo-gym.sandbox=1. Reap leaks from an unclean shutdown withdocker rm -f $(docker ps -aq --filter label=nemo-gym.sandbox=1).
User and Runtime Notes
The neutral user argument to exec() maps onto --user:
A command given as a list of strings is passed to docker exec as the program and its arguments, without <shell> -c; user, env and cwd apply as for shell text.
By default exec_shell is unset, so the provider auto-detects bash once the container is ready (conda and SWE-bench setup commands use source, which POSIX sh/dash lack) and falls back to sh. Pin exec_shell to /bin/sh or /bin/bash to skip the per-create probe.
Command failures return SandboxExecResult with the command’s exit code. A command timeout returns code 125 with error_type="timeout", and a docker runtime failure — daemon down or container gone, detected via stderr markers — returns code 125 with error_type="sandbox". A timed-out command’s in-container process is reaped when the sandbox is closed, since docker rm -f stops the whole process tree.
Task folders
When the harbor Environment Server runs task folders on Docker:
- A task whose Dockerfile needs a build is built with
dockerinto the local daemon, so no registry is needed. This holds while no build repository is set. Whenbuilds.repositoryorNEMO_GYM_BUILD_REPOSITORYnames one, the image is built withbuilds.tool, else the first of buildah, podman or docker onPATH, and pushed there (see Harbor image builds). - Compose tasks do not start on Docker, which cannot network a group of sandboxes. A task with a Compose file in
environment/, or any task when the server’scompose_overlaynames Composefiles, fails at seed withCould not set up sandbox for '<task>': Sandbox provider does not support networking between sandboxes. The failure is reported as retryable, so--resumeruns these tasks again, and they fail the same way, until each has used itsNEMO_GYM_MAX_ROLLOUT_ATTEMPTSattempts (3 by default). Leave them out with--exclude-tasks, or run them on OpenSandbox (see Multi-Container Tasks). - Gym holds a running image
ENTRYPOINTin onedocker exec. That exec ends atexec.default_timeout_s(180 seconds by default), but the entrypoint keeps running, untracked. If it then exits non-zero during the rollout, the sample is still scored, not masked withharbor:entrypoint_failed. The exit status is only logged and kept asentrypoint-exit.txt.