Docker Commit

Difficulty: Intermediate

Question

What does docker commit do, and why is it generally not recommended for building images?

Answer

docker commit <container> <image:tag> takes a container's writable layer, everything changed since it started, and saves it as a new layer on top of the original image, creating a new image. It is not recommended for building images because the result is not reproducible or reviewable: nobody can tell from the image how it was made, it often captures junk and secrets, and it cannot be rebuilt by CI. A Dockerfile is the standard way to build images; commit is mainly useful for debugging and forensics.

What commit captures and what it does not

  • Captured: every file added, changed or deleted in the container's filesystem (what docker diff shows).
  • Not captured: data in volumes and bind mounts, which is outside the container layer. People are often surprised that committing a running database container does not include its data, because the official images store it in a volume.
  • Config: the new image inherits the original config. You can change it with --change, which accepts Dockerfile instructions like CMD, ENTRYPOINT, ENV, EXPOSE, LABEL, USER, WORKDIR and VOLUME.
  • By default the container is paused during the commit to get a consistent snapshot (--pause=false disables this).

Why it is a bad way to build images

  1. Not reproducible: the steps exist only in someone's shell history. Next month, nobody can rebuild the image with a patched base, so security fixes become guesswork.
  2. Not reviewable: a Dockerfile goes through code review; a committed image is an opaque blob. docker history just shows a commit with no detail.
  3. Hidden junk and secrets: package caches, .bash_history, temp files and any credentials typed into the session get baked in.
  4. No build cache or CI integration: you cannot rebuild only what changed, run it in a pipeline or sign it with build provenance.
  5. Drift: the image no longer matches any source in Git. When an incident happens, you cannot trace what is running back to code.

For a team that has to pass audits, for example a fintech handling UPI transactions, an image with no traceable build is a compliance problem as well as a technical one.

When commit is genuinely useful

  • Forensics: snapshot a compromised or misbehaving container before removing it, so you can analyse it later without keeping it running.
  • Debugging a crash: commit a stopped container and start a shell in the new image to look around (docker run -it --entrypoint sh snapshot).
  • Quick experiments: trying out packages interactively, after which you write the steps into a Dockerfile.

Treat a committed image as temporary and never push it as a release.

docker commit versus a Dockerfile. Both produce an image, but only one gives you a repeatable, reviewable recipe.
docker commitDockerfile build
ReproducibleNo, steps are not recordedYes, rebuild any time
Code reviewOpaque image layerReviewed in Git like code
Build cache and CINot availableLayer cache, pipelines, provenance
Risk of leaked junkHigh, captures the whole sessionLow with .dockerignore and secrets
Best useForensics and debugging snapshotsEvery image you ship

Code examples

Snapshot a broken container for analysis

# Freeze the current state of a crashed container into an image
docker commit -m "snapshot for incident 4821" payments-api payments-api:incident-4821

# Explore it with a shell, without the original entrypoint
docker run -it --rm --entrypoint sh payments-api:incident-4821

# Optionally change config while committing
docker commit --change 'CMD ["sleep", "infinity"]' payments-api debug:latest

Committing preserves the filesystem state for later analysis. The --change flag applies Dockerfile instructions to the new image.

The reproducible alternative

# Instead of: docker run -it ubuntu, apt-get install curl, docker commit
# Write the steps down:
FROM ubuntu:24.04
RUN apt-get update \
 && apt-get install -y --no-install-recommends curl ca-certificates \
 && rm -rf /var/lib/apt/lists/*
CMD ["bash"]

The same result as an interactive session plus commit, but versioned, reviewable and rebuildable when the base image is patched.

Key points

  • docker commit saves a container's writable layer as a new image layer.
  • Volume and bind mount data are not included.
  • The container is paused during commit by default.
  • Committed images are not reproducible or reviewable; use a Dockerfile for anything you ship.
  • Good uses: forensic snapshots, debugging crashed containers, quick experiments.

Common follow-up questions

  • Why does committing a running Postgres container not include the database data?
  • How would you investigate a compromised container safely?
  • What does --change do in docker commit?
  • How do you find out what changed in a container before committing it?

Interview tips

  • Explain what it does, then give the reproducibility argument; one-word answers like "bad practice" are weak.
  • Name at least one legitimate use, such as forensics, to show nuance.
  • Mention that volume data is not captured; interviewers often follow up on that.

Concepts covered

docker commit, Reproducible builds, Dockerfile, Image provenance, Forensics

More Docker & Containers questions