Skip to content
Search

Docker:Commit

lastmod 2025-05-20created 2020-04-075 edits

docker commit: a command that even revives stopped containers

The docker exec command has one critical problem: you can only access the container environment while the container is running. Unfortunately, for system administrators, performing an autopsy on the state of a dead container is more important than exploring the runtime environment of a live one. And a container that died from an internal problem often dies again right away even when re-run with restart. So for dead containers, the exec command is useless.

In such cases, one option to consider is docker commit. commit is a command that turns a specific state of a container into an image as-is. This command is used during the Docker build process, but you will rarely need to use it directly. For more on how image builds work and on commit, see the following post.

Understanding Docker images by building them: how Docker image builds work and OverlayFS

Options

--change
The --change option will apply Dockerfile instructions to the image that is created.
Supported Dockerfile instructions - CMD|ENTRYPOINT|ENV|EXPOSE|LABEL|ONBUILD|USER|VOLUME|WORKDIR

How to create a hotfix image from a stopped container

Change the entrypoint to bash as follows, then debug and fix:

docker run --name ddrm-hotfix -it --entrypoint bash ddrm:0.0.9

If you need administrator privileges, log in as the root user:

docker run -u root --name ddrm-hotfix -it --entrypoint bash ddrm:0.0.9

After finishing the fix with vi or similar, quit with exit.

Check the container state with the command using the -l flag (last created container):

docker ps -l

It will probably be in the "Existed" state.

Do not commit right away. If you check the Entrypoint of the container you just ran with the inspect command, you can see it has changed to bash.

docker inspect ddrm-hotfix | grep -A 2 Entrypoint

The output will probably look like this:

            "Entrypoint": [
                "bash"
            ],

When committing, restore the original ENTRYPOINT that goes in the Dockerfile with the --change option.

docker commit --change='ENTRYPOINT ["python", "-OO", "-m", "ddrm"]' ddrm-hotfix ddrm:0.0.9-hotfix

If you logged in as root, the user may have changed. Check the user of the original image: (look at the Config > User field)

docker inspect ddrm:0.0.9
docker commit --change='USER app' --change='ENTRYPOINT ["python", "-OO", "-m", "ddrm"]' ddrm-hotfix ddrm:0.0.9-hotfix

Check the created image:

docker images

Check the Entrypoint of the created image:

docker inspect ddrm:0.0.9-hotfix | grep -A 5 Entrypoint

The output will probably look like this:

            "Entrypoint": [
                "python",
                "-OO",
                "-m",
                "ddrm"
            ],

The container created for the hotfix still exists.

docker ps -l

Remove it if necessary:

docker rm ddrm-hotfix

If in doubt, check the changes inside the newly created image:

docker run --rm -it --entrypoint bash ddrm:0.0.9-hotfix

See also

author Zer0 sitemap