Dockerfile import
pycontainer import converts an existing Dockerfile into [tool.pycontainer]
settings and prints a migration report. It uses only the standard library and
never runs Docker.
pycontainer import Dockerfile # print report and snippetpycontainer import Dockerfile --output pyproject.toml # write a new filepycontainer import Dockerfile -o pyproject.toml --forcepycontainer import Dockerfile --build-arg PY=3.12pycontainer import Dockerfile --shell '["/bin/bash", "-c"]'pycontainer import Dockerfile --format toml > imported.tomlpycontainer import Dockerfile --strict --output pyproject.tomlOutput formats and strict mode
Section titled “Output formats and strict mode”| Successful invocation | stdout | stderr |
|---|---|---|
Default --format text, no --output |
Report and TOML snippet | Empty |
--format text --output FILE |
Report and confirmation | Empty |
--format toml, no --output |
UTF-8 TOML only | Report |
--format toml --output FILE |
Empty | Report and confirmation |
Both formats write the same configuration to --output. In toml mode, use
a shell that preserves native byte streams when redirecting stdout; some
shells transcode redirected output. Alternatively, use --output to write
UTF-8 directly. Without --strict, imports with warnings can still produce
partial configuration.
--strict exits with status 3 if any warnings are recorded. It emits the
report but no TOML, and checks for warnings before inspecting, creating or
overwriting --output, including when --force is used. Notes alone do not
fail strict mode. The report goes to stdout in text mode and stderr in
toml mode. Shell redirection can create or truncate its destination before
pycontainer runs, even on strict failure; use --output when that matters.
Strict mode additionally warns on bare ARG declarations without an explicit
--build-arg, undefined variables or escaped dollars in ARG defaults,
unresolved variables in FROM --platform, and unverified global ARG default
expansion. Automatic platform arguments such as $TARGETPLATFORM are not
inferred; use a literal platform when they cannot be resolved. Stage ARG
inheritance is not verified; supply an explicit build argument even if a
global default exists. Strict mode requires --build-arg KEY=VALUE with a
valid argument name; an explicitly empty value is permitted. These extra
checks do not change the default text report or add ARG translation. Strict
mode only rejects recorded warnings: success is not proof of complete Dockerfile
equivalence, supported syntax, or a working runtime.
| Exit status | Meaning |
|---|---|
| 0 | Import completed; non-strict imports may have warnings |
| 1 | Existing output was not overwritten without --force |
| 2 | Invalid input, usage or unsafe merge |
| 3 | Strict import rejected warnings; no configuration was emitted or written by pycontainer |
Output safety
Section titled “Output safety”- Without
--output, pycontainer does not write a file. - If the output file exists and
--forceis not given, it is left untouched, and the command exits with status 1. Intextmode, the report and snippet are printed on stdout; intomlmode, stdout is empty and the refusal is reported on stderr. A strict warning takes precedence and exits with status 3. - With
--force, only the[tool.pycontainer]tables (including[tool.pycontainer.env]and[tool.pycontainer.labels]) are replaced; the rest of the file is preserved and the result is validated as TOML. --forcerefuses, leaving the file untouched, if it already defines a top-level[build]table, because a config file must contain exactly one pycontainer table.- Existing inline
tool.pycontainerdefinitions cannot be merged;--forcefails without changing the file. Convert them to tables before retrying.
Building from the imported settings
Section titled “Building from the imported settings”Pass the generated file to the build command; it reads [tool.pycontainer]
with the same keys, validation and precedence as [build]:
pycontainer import Dockerfile -o pyproject.tomlpycontainer build --config pyproject.toml --tag myapp:latestExplicit CLI options such as --base-image or --platform override the
imported values; omitted options keep them. A file that defines both [build]
and [tool.pycontainer] is rejected instead of silently picking one.
Mapping
Section titled “Mapping”| Dockerfile | Setting |
|---|---|
FROM image (final stage) |
base_image |
FROM --platform=... |
platform |
WORKDIR (relative paths resolve against the previous one) |
workdir |
ENV |
[tool.pycontainer.env] |
LABEL |
[tool.pycontainer.labels] |
EXPOSE (TCP) |
exposed_ports |
ENTRYPOINT |
entrypoint |
CMD |
cmd |
USER |
user |
STOPSIGNAL |
stop_signal |
VOLUME |
volumes |
SHELL (exec form) |
shell used for later shell-form CMD/ENTRYPOINT |
ARG/ENV substitution ($VAR, ${VAR}, ${VAR:-default}, ${VAR:+alt})
is applied to the instructions Docker expands at build time, including
STOPSIGNAL, VOLUME and USER; --build-arg KEY=VALUE overrides ARG defaults.
Like Docker, CMD/ENTRYPOINT text is not substituted; the shell expands it
when the container starts.
STOPSIGNAL values are normalized the way Docker parses them (term, 15
and SIGTERM all become SIGTERM; numbers use Linux numbering, and aliases
such as IOT map to their canonical names like SIGABRT). Unknown
signals produce a warning and are skipped. VOLUME accepts JSON or
space-separated paths; trailing slashes are removed and relative or unsafe
paths produce a warning. Volumes are image metadata only: nothing is mounted
at build time.
USER must be one of the OCI Linux forms (user, uid, user:group,
uid:gid, uid:group, user:gid). Unlike the warnings above, a USER value
that references an undefined variable, expands to an empty value, or is
malformed fails the import with the Dockerfile line number, and no output is
written. Names are not resolved at import time; the runtime resolves them
inside the image. No UID/GID mapping or file-ownership change is configured.
Shell-form commands
Section titled “Shell-form commands”Exec-form CMD/ENTRYPOINT arrays are imported unchanged. Shell-form text
(including JSON-looking text that is not a valid JSON string array) is kept
verbatim, quotes and all, and wrapped with the current shell:
- The default shell is
["/bin/sh", "-c"]. --shell JSONsets the initial shell, for example--shell '["/bin/bash", "-c"]'. It must be a non-empty JSON array of strings with a non-empty executable first and no NUL characters; malformed values are rejected.- A Dockerfile
SHELL ["…"]instruction changes the shell for laterCMD/ENTRYPOINTinstructions in the final stage, overriding--shell. It still produces a warning, becauseRUNis never executed and OCI has no field to record the shell for later images. A malformedSHELLproduces a warning and is ignored.
Compatibility boundary:
- The importer never runs command text on the build host; the shell only runs inside the container at startup.
- Docker inherits a
SHELLset by the base image’s own Dockerfile; OCI images do not record it, so pass--shellwhen the base image changed its shell. - The shell must exist in the target image. A distroless base produces a warning because it typically has no shell; prefer exec form there.
- A
CMD-only import does not get an auto-detected or framework entrypoint: like Docker, the image keeps the base image’sEntrypoint(if any) and runs the importedCMDunder it. Setentrypointto runcmdas its arguments. Exec-formENTRYPOINT []/CMD []clear the base image’s value.
To check a converted shell-form command in a real runtime (Linux/WSL with Skopeo and a running Docker daemon; not part of the default tests or CI):
PYTHONPATH=src timeout 600 python scripts/check_runtime_image.py --shell-command --base-image python:3.11-slimPYTHONPATH=src timeout 600 python scripts/check_runtime_image.py --shell-command --shell '["/bin/bash", "-c"]'It imports a quoted shell-form ENTRYPOINT, builds the image, imports it into
Docker, runs it and compares the exact output. The requested --platform must
match the Docker daemon. An optional --evidence record is tagged
shell-command and does not satisfy the separate full-startup release gate.
For CMD-only imports, use the separate verifier on a disposable Docker host
with Skopeo:
PYTHONPATH=src timeout 600 python scripts/check_runtime_image.py --cmd-only --evidence cmd-only-evidence.jsonIt builds a base with a known entrypoint, imports shell- and exec-form
CMD Dockerfiles with no ENTRYPOINT, and checks the exact output of each
container. The same-repository PR runtime workflow runs this check on the
exact PR head on Linux/amd64 and uploads a separate cmd-only-runtime-evidence
artifact. It does not substitute for the full-startup release evidence.
Parsing
Section titled “Parsing”Parser directives (# syntax=, # escape=), comments, line continuations
(including comments inside a continuation), JSON and shell forms, and heredoc
bodies are handled. Only the final stage of a multi-stage build is imported.
Warnings
Section titled “Warnings”The importer reports each of these with a WARNING and the relevant line
number: RUN, COPY/ADD (including --from), HEALTHCHECK,
SHELL (even when applied), ONBUILD, MAINTAINER, unknown instructions,
non-final stages (including their VOLUME/STOPSIGNAL), FROM scratch, UDP
ports, undefined variables, invalid STOPSIGNAL/VOLUME values and
shell-form commands on distroless bases. HEALTHCHECK is not translated
(#40); set it manually with a [build.healthcheck] table.
Because RUN is never executed, install dependencies on the host and use
include_deps = true / requirements_file; project files are selected with
include_paths / exclude_paths rather than COPY.