Here’s a complete tutorial on docker commit
, covering what it does, examples, and use cases.
What is docker commit
?
docker commit
is a Docker command used to create a new image from an existing container’s state. This allows you to save the current state of a container, including its file system and configuration, as a reusable image.
Key Features:
- Creates a new Docker image from a modified or running container.
- Useful for snapshotting the container state.
- Allows for quick prototyping or manual image creation without a
Dockerfile
. - Supports adding custom metadata (author, message).
Basic Syntax
docker commit [OPTIONS] CONTAINER IMAGE[:TAG]
Common Options:
-a
,--author
: Set the image author.-m
,--message
: Add a commit message.-p
,--pause
: Pause the container during commit (default: true).
Examples of docker commit
1. Create an Image from a Running Container
docker commit my_container my_new_image:latest
This creates a new image named my_new_image:latest
from my_container
.
2. Add an Author and Message
docker commit -a "John Doe" -m "Updated with new configurations" my_container my_image:v1
This sets John Doe
as the image author and adds a commit message.
3. Create an Image Without Pausing the Container
docker commit --pause=false my_container my_image:latest
This commits the container without pausing it, which is useful for real-time services.
4. Create an Image from a Stopped Container
docker commit stopped_container my_backup_image
You can commit a stopped container to create a backup image.
5. Use the Container ID Instead of the Name
docker commit a1b2c3d4e5f6 my_custom_image:dev
This commits the container with ID a1b2c3d4e5f6
to my_custom_image:dev
.
6. Automate Commit with a Script
#!/bin/bash
docker commit -a "DevOps Team" -m "Nightly snapshot" my_app nightly_snapshot:$(date +%Y%m%d)
This script creates a snapshot image with the current date as the tag.
7. Create a Base Image with Pre-Installed Dependencies
If you have a container with installed software or dependencies:
docker commit my_python_container python_base:latest
This saves the container state as a base image for future Python projects.
8. Export Configuration Changes
After changing configuration files inside a running container:
docker commit my_nginx_container nginx_custom:1.0
This creates an image with your customized NGINX configuration.
Use Cases for docker commit
1. Snapshotting and Backups
- Create images from containers to snapshot the current state for backup purposes.
- Example: Backup a database container with all its data.
2. Quick Prototyping and Manual Changes
- Save an image after manual configuration or software installation in a container.
- Example: Install a library in a running Python container, then commit it for future use.
3. Debugging and Hotfixes
- Modify a running container to apply a quick fix and commit it as a temporary image.
- Example: Fix a misconfiguration in a running NGINX container and save the new state.
4. Custom Base Images
- Create custom base images with pre-installed dependencies or configurations.
- Example: Build a Python base image with specific libraries installed.
5. Preserving Legacy Containers
- Preserve old containers by committing them as images for future use.
6. CI/CD Snapshots
- Use
docker commit
to snapshot application state during continuous integration and testing.
List of Common docker commit
Commands
Command | Description |
---|---|
docker commit my_container my_image:latest | Create an image from a container |
docker commit -a "John Doe" -m "Update configs" | Add author and commit message to the image |
docker commit --pause=false my_container my_image | Commit without pausing the container |
docker commit stopped_container my_backup | Create an image from a stopped container |
docker commit a1b2c3d4e5f6 my_custom_image | Commit using container ID |
docker commit my_app nightly_snapshot:20230207 | Automate commit with a timestamp in the tag |
docker commit my_nginx_container nginx_custom | Save a container with custom configurations |
Best Practices for Using docker commit
:
- Avoid using
docker commit
in production—prefer Dockerfiles for reproducibility and version control. - Add descriptive tags and messages to track changes.
- Pause the container during commit for consistent snapshots (
--pause=true
). - Use
docker commit
for debugging or quick experiments, then convert to a Dockerfile for long-term use. - Combine with
docker save
to create portable images for backup or transfer.
Common Errors and Solutions
- “No such container”
→ Ensure the container exists and is running/stopped. Usedocker ps -a
to verify. - “Image is too large”
→ Remove unnecessary files or use a multi-stage build to reduce the image size. - Changes not reflected in the new image
→ Ensure you’re modifying the correct container and committing it after the changes. - “Permission denied”
→ Run the command with elevated privileges (sudo
) or ensure your user has Docker permissions.
Combining docker commit
with Other Commands
Backup and Transfer the Committed Image
docker commit my_container my_backup_image
docker save my_backup_image > my_backup_image.tar
Restore the Image on Another Machine
docker load < my_backup_image.tar
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND