How to Commit Changes to a Docker Image
Docker is an open-source solution designed to deploy, scale, and manage applications inside lightweight, isolated containers. These containers bundle applications with all required dependencies to guarantee consistent performance across environments. Any modifications inside a running container, like installing packages or editing files, are only temporary. Once the container is deleted or recreated, those changes disappear unless volumes are used to persist data. However, when you stop a container, its state is preserved, making it possible to restart it with the data intact. Docker keeps the container’s filesystem and metadata until you explicitly remove it.
This guide demonstrates how to commit changes to a Docker image using the latest Docker CLI commands. It provides step-by-step examples, explanations of command flags, and best practices to help you understand when and how to use docker container commit
in your workflow.
docker container commit vs docker image push
Feature | docker container commit | docker image push |
---|---|---|
Purpose | Generates a new image from a running or stopped container | Uploads a local image to a remote registry |
Usage Context | Applied after container modifications to save its state | Used to share images between systems or with other users |
Source | Running or exited container | Existing Docker image |
Target | Local Docker image store | Remote registry |
Persistence | Saves container changes locally as a new image | Distributes images for use on different systems |
Typical Workflow | Development, debugging, or snapshotting container state | Deployment and versioning across environments |
The Short Answer
Here are the essential commands to commit changes from a running container, create a new image, and optionally upload it to a container registry:
# List running containers and identify the one to save
$ docker container ls
# Commit the container as a new image with a descriptive message
$ docker container commit -m "Save config" :
# List all local images to confirm creation
$ docker image ls
# Tag the image for your container registry
$ docker image tag : /:
# Push the image to a remote container registry
$ docker image push /:
Command Usage
The docker container commit
command builds a new image from the current state of a running or stopped container. Run it after adjusting a container, like installing packages or configuring software, to save those changes into a reusable Docker image.
The general syntax is:
docker container commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Command Options
Option | Description |
---|---|
-m, –message | Adds a commit message that explains the container changes. |
-a, –author | Defines the author’s name and email for the image (e.g., “Alice <alice@example.com>”). |
-p, –pause | Temporarily pauses the container during the commit process (enabled by default). |
-c, –change | Applies Dockerfile instructions such as CMD, ENV, or EXPOSE to the new image. |
Note: The docker container commit
command saves internal container modifications but does not include data from mounted volumes or external storage.
Commit Changes to a New Docker Image
Follow these steps to commit changes from a modified container into a new image.
First, start a container from a base image using the -it
flag to open an interactive terminal session:
$ docker container run -it ubuntu /bin/bash
This command fetches the latest Ubuntu image from Docker Hub (if not present locally) and opens a Bash shell as the root user.
Inside the container, apply modifications. For example, update the package list and install Nmap, a network scanning and security tool:
# apt update && apt install nmap -y
Exit the container. The process stops, but Docker retains its state, so you can commit the changes:
# exit
List all containers to locate the one you just modified:
$ docker container ls -a
The modified container will appear in the list. Copy its container ID for the next step. Example output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fbd1f006ee67 ubuntu "/bin/bash" About a minute ago Exited (0) 17 seconds ago gracious_dijkstra
Commit the container as a new image:
$ docker container commit -m "Installed Nmap" fbd1f006ee67 ubuntu-with-nmap:v1
Replace fbd1f006ee67
with the copied container ID and ubuntu-with-nmap:v1
with your preferred image name and tag. Docker outputs the image ID if successful:
sha256:4ab8382015ea03a117a76a38dab41d6e291ecaad3b570d018d336dc5c81b9ac7
Verify that the image has been created:
$ docker image ls
Your custom image and tag will appear in the output, similar to this:
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu-with-nmap v1 4ab8382015ea 31 seconds ago 158MB
ubuntu latest a0e45e2ce6e6 13 days ago 78.1MB
When to Commit Changes
Use the docker container commit
command to record modifications made inside a running container and save them as a new image. This is useful in situations such as:
- Install Tools or Packages: Add utilities like Nmap or custom scripts to a container, then save the configured environment for reuse or deployment.
- Apply Configuration Changes: Adjust system settings or define environment variables to optimise performance or behaviour, and commit those adjustments to preserve the setup.
- Capture a Debugging or Testing State: After troubleshooting or testing inside a container, commit the exact state so you can replicate it on another system or share with your team.
- Create a Snapshot Before Changes: Save a point-in-time version of your container before updates or package installations, giving you the ability to roll back if necessary.
While docker container commit
is useful for quick snapshots and smaller workflows, it is not recommended for production environments. For consistent and reproducible image builds in collaborative or production setups, use Dockerfiles with version control. Dockerfiles provide greater transparency, traceability, and control over image configurations.
Conclusion
In this tutorial, you created a new Docker image by committing changes from a running container using Docker CLI commands. You launched a container, made modifications inside it, and saved those changes as a reusable image. You also explored when to use the docker container commit
command and how it differs from docker image push
. This approach allows you to capture and reuse container modifications without writing a Dockerfile. For more advanced image management and best practices, consult the official Docker documentation.