nemo_gym.data.tasks.harbor.dockerfile
nemo_gym.data.tasks.harbor.dockerfile
Recognize Dockerfiles Gym can run without a build, and resolve them as Docker would.
Two shapes are recognized:
- Pull mode:
FROM <image>plusWORKDIR,ENV,USERorLABELlines. Nothing changes the filesystem, so Gym runs the base image directly and applies the recorded settings at sandbox creation (base_image_only). - Overlay mode: the same, plus
RUNlines. Gym pulls the base image and runs theRUNlines inside the sandbox at seed, each with theWORKDIR,ENVandUSERin effect at that point of the Dockerfile (overlay_image). This is the shape of SWE-bench style tasks: a published environment image plus setup commands.
ENV, WORKDIR and USER are resolved the way docker build resolves them: the
base image’s own configuration (BaseImageConfig, recorded from the registry at
prepare) is the starting state, $NAME and ${NAME} expand against the accumulated
environment (not inside single quotes; \$ is a literal dollar), a relative WORKDIR
joins onto the previous one. The resolved values are literal: nothing reaches the sandbox
with a live $ in it. A Dockerfile whose resolution depends on the base image (a RUN
line, a reference to a variable the Dockerfile did not set, a relative first WORKDIR)
raises ImageConfigRequired when no configuration is given.
An empty CMD [] is accepted and ignored. An empty ENTRYPOINT [] is accepted and
recorded (entrypoint_cleared): the sandbox provider starts the image with its own keepalive
either way, and the Harbor environment server runs the base image’s recorded Entrypoint
beside it unless the Dockerfile cleared it. Anything else (COPY, ADD, multi-stage FROM,
ARG-templated images, heredoc RUN, RUN --mount, a real ENTRYPOINT or
CMD, HEALTHCHECK, EXPOSE, VOLUME, SHELL, STOPSIGNAL, a FROM flag
such as --platform, an # escape= parser directive) needs a real build, which is not
part of this loader yet; DockerfileNeedsBuild names the first instruction that requires it.
Module Contents
Classes
Functions
Data
API
A Dockerfile that reduces to a base image plus creation-time settings.
env holds the keys the Dockerfile’s ENV lines set, with their resolved final
values; workdir and user are the resolved final values (None when neither
the Dockerfile nor a given base image configuration sets them). entrypoint_cleared
is whether the Dockerfile wrote ENTRYPOINT [], so the base image’s entrypoint must not run.
What a base image’s OCI configuration sets before its Dockerfile’s first instruction.
env is the image’s Env as a mapping; workdir and user are WorkingDir
and User (None when the image sets none, meaning / and root); shell is the
image’s Shell, which runs its shell-form RUN lines (Docker’s /bin/sh -c when unset).
Read a compose-images.json entry (see nemo_gym.data.tasks.harbor.image_configs).
Raises ValueError for an entry written before RECORD_VERSION: it lacks fields
this resolver reads (an absent Env would read as a known, empty environment), so the
dataset must be prepared again to re-record it.
The parsed instructions of a pull-mode or overlay-mode Dockerfile, before resolution.
Bases: ValueError
The Dockerfile holds an instruction this loader cannot apply without a build.
Bases: ValueError
Resolving the Dockerfile needs the base image’s recorded configuration, which was not given.
Bases: BaseImage
A base image plus the RUN lines to apply at seed, in Dockerfile order.
workdirs are the WORKDIR instructions, in order, each with the user in effect at it
and its place among the runs (see OverlayWorkdir).
One RUN line with the Dockerfile state in effect when it runs, resolved to literals.
command is the command text: a shell-form line as written, or an exec-form argument
vector joined with shell quoting. shell runs it: the image’s Shell in effect for a
shell-form line (Docker’s /bin/sh -c unless the image sets one), None for an exec-form
line, which runs its arguments directly, without a shell. env is the whole environment the
line runs with (the base image’s Env plus the ENV lines so far), workdir the
directory it runs in and user who runs it (None is root).
One WORKDIR instruction: the directory to create, owned by the user in effect at it.
before is how many RUN lines precede the instruction, which places it among
OverlayImage.runs: the directory is made before run before (counting from 0),
or after the last run when no RUN follows it. Docker creates every WORKDIR whether or
not a RUN uses it, so the finished image has them all.
Docker’s shell-word processing: quote removal, escapes and $ expansion against env.
With known=False the base image’s environment is unknown, so a reference to a
variable the Dockerfile did not set itself raises ImageConfigRequired instead of
silently expanding to nothing.
Expand the $ at index; return the text and the index after the expression.
Index of the } closing the { that opens text, skipping nested ${...}; -1 when unterminated.
(key, raw value) pairs of an ENV line; values keep their quotes for the expander.
The JSON array an exec-form instruction holds, or None when the arguments are shell form.
Join continuation lines and drop comments and blanks, as Docker’s parser does.
A trailing backslash is removed together with the newline and the next line is appended verbatim (no separator is inserted). Comment and blank lines are dropped even inside a continuation. Only the first line of an instruction has its leading blanks removed.
Refuse a Dockerfile whose leading comment block sets # escape=.
Docker reads parser directives only in the comments before the first instruction, and this loader
joins continuation lines with a backslash alone (_logical_lines), so a file that switches
the escape character would be read with the wrong line structure (RUN echo foo \ would swallow
the next instruction). A build honours the directive; a # escape= after an instruction is a plain
comment, as it is for Docker. # syntax= and # check= do not change how lines are read.
The validated arguments of a RUN line: a shell-form line, or the exec-form JSON array as written.
Return the base image and settings, or None when the file has RUN lines or needs a build.
Whether any RUN, COPY or ADD instruction of a Dockerfile opens a heredoc.
Each instruction’s arguments are judged as has_heredoc judges one line, word by word as BuildKit
does, so RUN cat > /x << 'EOF' (a space before the delimiter, which BuildKit does not read as a heredoc
either) is not one. The body lines of a heredoc are not instructions, but they are only ever reached once
the opening line has already answered.
Whether command opens a Dockerfile heredoc, judged word by word as BuildKit does.
The base image, settings and RUN lines, or None when the file needs a build.
Raises ImageConfigRequired when the file depends on a base image configuration and none is given.
Read a pull-mode or overlay-mode Dockerfile; raise DockerfileNeedsBuild for anything else.
Apply the instructions in order from the base image’s state, as docker build would.
With base given, the starting environment is its Env (plus Docker’s default
PATH when it has none), the working directory its WorkingDir or / and the
user its User. Without it, only a Dockerfile whose values do not depend on the base
image resolves; anything else raises ImageConfigRequired.
Split on unquoted blanks, keeping quotes and escapes in the words (Docker’s parseWords).