Skip to content

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.

Terminal window
pycontainer import Dockerfile # print report and snippet
pycontainer import Dockerfile --output pyproject.toml # write a new file
pycontainer import Dockerfile -o pyproject.toml --force
pycontainer import Dockerfile --build-arg PY=3.12
pycontainer import Dockerfile --shell '["/bin/bash", "-c"]'
pycontainer import Dockerfile --format toml > imported.toml
pycontainer import Dockerfile --strict --output pyproject.toml
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
  • Without --output, pycontainer does not write a file.
  • If the output file exists and --force is not given, it is left untouched, and the command exits with status 1. In text mode, the report and snippet are printed on stdout; in toml mode, 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.
  • --force refuses, 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.pycontainer definitions cannot be merged; --force fails without changing the file. Convert them to tables before retrying.

Pass the generated file to the build command; it reads [tool.pycontainer] with the same keys, validation and precedence as [build]:

Terminal window
pycontainer import Dockerfile -o pyproject.toml
pycontainer build --config pyproject.toml --tag myapp:latest

Explicit 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.

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.

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 JSON sets 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 later CMD/ENTRYPOINT instructions in the final stage, overriding --shell. It still produces a warning, because RUN is never executed and OCI has no field to record the shell for later images. A malformed SHELL produces 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 SHELL set by the base image’s own Dockerfile; OCI images do not record it, so pass --shell when 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’s Entrypoint (if any) and runs the imported CMD under it. Set entrypoint to run cmd as its arguments. Exec-form ENTRYPOINT [] / 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):

Terminal window
PYTHONPATH=src timeout 600 python scripts/check_runtime_image.py --shell-command --base-image python:3.11-slim
PYTHONPATH=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:

Terminal window
PYTHONPATH=src timeout 600 python scripts/check_runtime_image.py --cmd-only --evidence cmd-only-evidence.json

It 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.

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.

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.