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]
Code language: CSS (css)
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
Code language: CSS (css)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: CSS (css)
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)
Code language: JavaScript (javascript)
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
Code language: CSS (css)
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
Code language: CSS (css)
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 committo 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 commitin 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 commitfor debugging or quick experiments, then convert to a Dockerfile for long-term use. - Combine with
docker saveto create portable images for backup or transfer.
Common Errors and Solutions
- “No such container”
โ Ensure the container exists and is running/stopped. Usedocker ps -ato 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
Code language: CSS (css)
Restore the Image on Another Machine
docker load < my_backup_image.tar
Code language: CSS (css)
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals