Dockerfile review: reproducibility, shell boundaries and image scope

Review Dockerfile base images, RUN failures, root users, build context, health checks and layer caching with exact AI-finding rule references.

A Dockerfile contains image instructions and sometimes embedded shell code. Those layers need different explanations: COPY is not a shell command, JSON-form RUN does not use the default shell, and a package installed in one stage is not necessarily present in the final image. Read each stage and the final runtime configuration before assigning a risk.

Pin inputs without confusing pinning with patching

A latest or untagged base image can change while the Dockerfile remains unchanged. A digest pins the selected image, but does not pin the package index consulted by a later apt-get install. Reproducibility may require package versions and a repository snapshot as well. Pinning also requires an update policy so known vulnerabilities do not remain frozen indefinitely. Verify the image distribution before recommending apk instead of apt; changing libc can alter application compatibility.

Review RUN using its actual shell

Shell-form RUN normally uses /bin/sh -c on Linux unless SHELL changes it. JSON-form RUN supplies an executable and arguments directly. A pipeline can return the last command status and hide a failed download. Do not blindly add Bash pipefail to a shell that lacks it. Separate fetching, checksum or signature verification and execution with explicit guards. A remote script’s dependencies and behavior cannot be inferred from its URL or from curl flags.

Track files and privileges across stages

COPY . . exposes the build context as an input boundary, but the Dockerfile alone cannot prove whether .dockerignore excludes credentials. Inspect that separate file before declaring a leak. Likewise, an absent USER does not prove root execution when a base image sets its own user. Review the final effective user and filesystem ownership. GitHub Docker container actions have special root-user requirements; apply least privilege according to the actual execution environment, not a universal rewrite.

Separate size, health and process behavior

Keep apt update, installation and package-list cleanup in the same RUN layer. Manifest-first COPY before npm ci can preserve dependency caching when source changes; curl does not inherently bypass the cache. HEALTHCHECK declares a probe, not proof that an application is healthy, and some orchestrators configure probes elsewhere. Exec-form CMD avoids an extra shell, but the Dockerfile cannot reveal whether the application handles SIGTERM. Review those claims independently before making changes.

Review example

This is the intentionally problematic Dockerfile used for real AI-review verification. The example URL is illustrative. The checker never builds the image or downloads its script.

FROM node:latest
WORKDIR /app
RUN apt-get update && apt-get install curl
RUN curl -fsSL https://example.com/install.sh | sh
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Open the checker

What cannot be verified

Static analysis cannot verify a base image manifest, a remote script, the actual build context, runtime permissions or application health. Rule descriptions below distinguish those unknowns from visible instructions. An AI finding may overstate a risk or suggest an incompatible change; the guide supplies evidence limits and trade-offs so you can assess it rather than apply it automatically.

Finding references

rm -rf with a variable path

Recursive/forced rm using a variable path. If the variable is empty or unset, this can delete far more than intended.

Suggested safeguard: Guard the path: use "${dir:?dir is required}" and quote the expansion, e.g. rm -rf "${dir:?}/subdir".

SC2115

Piping a download into a shell

Remote content is piped straight into a shell. A compromised or partially downloaded response executes with your privileges.

Suggested safeguard: Download to a file, verify it (checksum or signature), inspect it, then execute it as a separate step.

eval with an expansion

eval is used with an expansion. Any attacker-controlled part of that string becomes executable code.

Suggested safeguard: Replace eval with an array, a case statement, or indirect expansion (${!name}) — none of which re-parse data as code.

SC2294

World-writable chmod 777

chmod 777 makes the target world-writable, so any local account can modify or replace it.

Suggested safeguard: Grant the narrowest mode that works — 755 for executables, 644 for data, 700 for private state.

cd without a failure guard

cd without a failure guard. If the directory does not exist the script keeps running in the wrong working directory.

Suggested safeguard: Write cd "$dir" || exit 1 (or set -e plus a quoted path) so the script stops instead of operating on the wrong tree.

SC2164

Unquoted variable expansion

Unquoted variable expansion. Values containing spaces or glob characters get word-split and globbed before the command sees them.

Suggested safeguard: Wrap the expansion in double quotes: "$var" / "${arr[@]}".

SC2086

Legacy backtick substitution

Legacy backtick command substitution. It nests badly and handles backslashes differently from the modern form.

Suggested safeguard: Use $(command) instead of `command`.

SC2006

Non-portable echo -e

echo -e is not portable — it is undefined in POSIX sh and behaves differently across shells and /bin/echo builds.

Suggested safeguard: Use printf '%b\n' "$value" for escape handling that behaves the same everywhere.

SC3037

sudo inside a script

The script invokes sudo. It will either prompt interactively in the middle of a run or silently require passwordless sudo.

Suggested safeguard: Check privileges up front (if [[ $EUID -ne 0 ]]) and fail with a clear message, rather than escalating mid-script.

Predictable /tmp path

Fixed path under /tmp. Predictable temp paths are a symlink-attack vector on shared machines and collide between concurrent runs.

Suggested safeguard: Create the file with mktemp (or mktemp -d) and remove it in a trap.

SC2129

Parsing ls output

Parsing the output of ls. Filenames containing spaces or newlines break the pipeline.

Suggested safeguard: Use a glob or find -print0 with a while IFS= read -r -d '' loop.

SC2012

read without -r

read without -r mangles backslashes in the input.

Suggested safeguard: Use read -r (and IFS= read -r line when preserving leading whitespace).

SC2162

Base image without a pinned tag

The base image uses :latest or no tag, so builds change silently when the upstream image is updated.

Suggested safeguard: Pin a version tag, ideally with a digest: FROM debian:12-slim@sha256:...

ADD with a remote URL

ADD downloads the URL without verifying it.

Suggested safeguard: Use ADD --checksum=sha256:... or RUN curl with a checksum check.

apt-get install without cleanup

The apt package lists stay in the layer and bloat the image.

Suggested safeguard: End the same RUN with && rm -rf /var/lib/apt/lists/*.

apt-get install without -y

apt-get install waits for confirmation, which fails a non-interactive build.

Suggested safeguard: Add -y (and --no-install-recommends to keep the image small).

Container runs as root

No USER instruction, so the container process runs as root.

Suggested safeguard: Create an unprivileged user and switch to it with USER app before CMD/ENTRYPOINT.

Alpine package versions and reproducibility

An apk add command without an explicit package version resolves the version available in its configured repositories. For example, installing curl without a version pin can produce different versions between builds. The Dockerfile alone cannot verify which versions remain available or whether an example version is supported.

Suggested safeguard: When reproducibility is required, verify the repository and supported package version before specifying package=version. Use maintained repository snapshots where needed and plan security updates; do not copy an unverified example version into production.

Alpine package cache management

The apk add --no-cache option avoids retaining the package-index cache in the image. Its presence is a positive resource-management observation, not an error. This flag does not pin package versions, remove every temporary file or prove the final image size.

Suggested safeguard: Keep --no-cache for installs that do not need a persistent local index cache. Assess other artifacts separately and remove temporary files in the instruction that creates them; no change is needed solely because --no-cache is already present.

Placeholder download URLs and unvalidated script content

An example.com download URL is a documentation placeholder, not a verified installer source. A Dockerfile that downloads and invokes a script without checking its contents or integrity provides no evidence that the installer is safe. The remote contents and their behavior cannot be verified from this file.

Suggested safeguard: Replace placeholders with the intended maintained source, review a fixed version and verify a trusted checksum or signature before invocation. Keep illustrative examples clearly separated from production Dockerfiles.

Package-index drift and reproducible installs

An unversioned apt install can resolve different package versions as repositories change. Pinning only the base digest does not pin package indexes. This is build reproducibility, not proof that installing a package twice is unsafe.

Suggested safeguard: Use a supported repository snapshot and explicit package versions when reproducibility is required, while planning security updates. Keep apt-get update and install together; do not switch distributions as a substitute for version control.

Container health checks

A missing HEALTHCHECK means the image declares no health probe. EXPOSE does not prove the application is an HTTP service; an orchestrator may configure its own probe outside this file.

Suggested safeguard: If a probe is needed, choose a command and endpoint supported by the actual application and final image. Return nonzero on failure and configure interval, timeout and start period.

Failure propagation in RUN commands

Shell-form RUN uses the configured shell (normally /bin/sh on Linux). A pipeline normally returns the last command status, so a failed download can be masked by a successful shell. Bash pipefail is not supported by every /bin/sh.

Suggested safeguard: Separate download, verification and execution with && or explicit failure guards. Only use pipefail after choosing a shell that supports it; do not blindly add set -euo pipefail to /bin/sh.

Build-context exclusions and secrets

COPY . . can copy every non-excluded file from the build context. A Dockerfile alone cannot prove whether .dockerignore excludes credentials, repository metadata or local dependencies.

Suggested safeguard: Inspect the actual build context and .dockerignore. Exclude sensitive and unnecessary files, copy only required paths and use build secret mounts instead of copying credentials.

Base distribution and package-manager compatibility

apt-get requires a compatible Debian-family base; apk requires Alpine. A floating image tag or image name alone cannot verify the actual distribution, installed commands or future contents.

Suggested safeguard: Verify the pinned base image documentation and match installation commands to that distribution. Do not recommend a distribution switch without checking libc and application compatibility.

Commands available in the final image

Build and runtime dependencies differ. Packages installed in a discarded stage are not automatically present in the final image; undeclared base-image contents cannot be verified from the Dockerfile.

Suggested safeguard: Check each stage, COPY --from and runtime command. Install only required runtime dependencies in the final stage and verify their supported versions.

External script dependencies and portability

The dependencies, shell syntax and behavior of a downloaded script cannot be verified without its contents. Supported curl flags do not establish that the remote script is portable or safe.

Suggested safeguard: Review the exact version of the script, verify its integrity, document its shell and command requirements and retain an auditable source. Treat any unprovided script contents as unknown.

Dependency layers and build caching

Copying dependency manifests before application source can preserve an installation layer when source changes. Remote downloads in RUN follow normal layer-cache rules; curl does not by itself force a download on every build.

Suggested safeguard: Copy manifests and lockfiles before npm ci, then copy application code. Keep cleanup in the layer that creates artifacts and use multiple stages only when the application needs them.

Optional recommended apt packages

apt-get may install recommended packages in addition to direct dependencies, increasing image size. Whether these packages are unnecessary depends on the application.

Suggested safeguard: Consider --no-install-recommends after checking required functionality. Keep update, install and package-list cleanup in the same RUN instruction.

Instruction order and documented build intent

WORKDIR sets the location for subsequent instructions. Manifest-first copying before npm ci is normally intentional for caching, not evidence of a defect. Missing comments alone do not establish a functional problem.

Suggested safeguard: Explain unusual instruction ordering when needed. Preserve valid manifest-first caching and distinguish optional readability advice from verified build failures.

Container signals and graceful shutdown

Exec-form CMD or ENTRYPOINT avoids an extra shell and allows the process to receive container signals. The application’s handling of SIGTERM cannot be verified without its source; a health check does not prove graceful shutdown.

Suggested safeguard: Use exec-form startup where appropriate and verify the application handles termination and closes resources. Assess health probes separately from signal handling.

Official references