How to make permanent changes to a Docker image immediately.
Docker images are immutable. Once built, they don't change. This ensures consistency, predictability, and stability. Every container created from the same image behaves exactly the same way, making version management secure and easy.
But what if you need to adjust something inside a running container, such as installing a package or updating the configuration? That's where the `docker commit` command comes in. It allows you to log changes in a running container and create a new image without touching the original image. This is great for testing fixes, iterating quickly, and deploying custom images without rebuilding from scratch.
Why isn't the Docker image changing?
A Docker image consists of multiple read-only layers. When you run a container, Docker adds a thin write layer on top called the container layer. Any changes you make only occur within this top layer. After the container is removed, all changes within that layer are lost, leaving the original image intact.
- This design offers several benefits:
- All containers from the same image behave the same way, ensuring consistency.
- Changes in one non-image container affect other containers, providing predictability.
You can safely tag specific image versions without risk. This design offers excellent stability, but it limits you when you want to make quick changes to a running container. That's where the `docker commit` command comes in.
Create a new image from a running container.
When you run the `docker commit` command, Docker records the current state of a running container and creates a new image from it. It captures the container's file system, saving any changes you've made, such as installed packages, updated configurations, or modified files, as a new image layer. This way, the original image remains unaffected, allowing for quick experimentation and iteration.
This makes it ideal for saving custom base settings for future reuse, applying minor bug fixes or configuration changes during testing, or sharing the updated image with your team without having to rebuild the Dockerfile from scratch.
You can use the `docker commit` command with the following syntax to create a new image from a running container:
docker commit [OPTIONS] CONTAINER_ID NEW_IMAGE_NAME[:TAG] Here, `CONTAINER_ID` is the ID or name of the container you want to capture, `NEW_IMAGE_NAME` is the name you want to give the new image, and `TAG` is optional, defaulting to `latest`.
Note : `docker commit` is an old alias for `docker container commit`; they are the same thing.
The `docker commit` command provides several options that allow you to add metadata, apply configuration changes, and control how the commit process works. The table below summarizes all supported options:
| Options | Long form | Describe | For example |
| -a | -author | Add the author's name to the metadata of the new image. | docker commit -a "Anees" my-container my-image |
| -c | -change | Apply directives from the Dockerfile such as ENV, LABEL, or CMD to the new Docker image. | docker commit -c "ENV APP_ENV=prod" my-container my-image |
| -m | -message | Add a short message describing the changes that have been made to the image. | docker commit -m "Installed curl" my-container my-image |
| -p | -pause | Pause the commit process within the container to ensure consistency (default: true). | docker commit --pause=false my-container my-image |
See how the `docker commit` command works.
Suppose you want to install curl in an Alpine container without rebuilding the Dockerfile. To do this, run a container from the original image:
docker run -it alpine:latest /bin/sh After entering the container, make the necessary changes:
apk update && apk add curl Now let's exit the container:
exit Next, commit the container as a new image:
docker commit alpine-with-curl:1.0 Verify your new image:
docker images Now you have a new image ready to run anywhere, with curl pre-installed.
Run the new image to check the saved changes.
After creating the new image, you can run a container from it to verify that the changes have been saved.
docker run -it alpine-with-curl:1.0 /bin/sh This command opens an interactive shell inside the container based on the image `alpine-with-curl:1.0`. Once inside, you can check if your changes are still intact.
curl --version This command demonstrates that the changes have been preserved in the new image.



