Difficulty: Intermediate
What does docker commit do, and why is it generally not recommended for building images?
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.
docker diff shows).--change, which accepts Dockerfile instructions like CMD, ENTRYPOINT, ENV, EXPOSE, LABEL, USER, WORKDIR and VOLUME.--pause=false disables this).docker history just shows a commit with no detail..bash_history, temp files and any credentials typed into the session get baked in.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.
docker run -it --entrypoint sh snapshot).Treat a committed image as temporary and never push it as a release.
| docker commit | Dockerfile build | |
|---|---|---|
| Reproducible | No, steps are not recorded | Yes, rebuild any time |
| Code review | Opaque image layer | Reviewed in Git like code |
| Build cache and CI | Not available | Layer cache, pipelines, provenance |
| Risk of leaked junk | High, captures the whole session | Low with .dockerignore and secrets |
| Best use | Forensics and debugging snapshots | Every image you ship |
# 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.
# 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.
docker commit, Reproducible builds, Dockerfile, Image provenance, Forensics