Docker commit Command
docker commit The commit command is used to save the current state of a container as a new Docker image.
docker commit The command is commonly used to create an image that preserves the state of a container so that the image can be reused or distributed later.
Syntax
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
OPTIONS description:
-
-a :The author of the committed image.
-
-c :Use Dockerfile instructions to create the image.
-
-m :The commit message.
-
-p :Pause the container before committing the image (default is true).
Save a container as a new image:
docker commit my_container my_new_image
Save the container named my_container as a new image named my_new_image.
Specify a tag:
docker commit my_container my_new_image:latest
Save the container as an image with the latest tag.
Add author and commit information:
docker commit -a "John Doe" -m "Added new features" my_container my_new_image
Save the container as a new image and add author and commit information.
Commit an image without pausing the container:
docker commit --pause=false my_container my_new_image
Save the container as a new image without pausing it.
Example
Start a container:
docker run -d -it --name my_container ubuntu bash
Make some changes:
docker exec my_container apt-get update
docker exec my_container apt-get install -y nginx
Commit the container as a new image:
docker commit -a "Your Name" -m "Installed nginx" my_container my_new_image
View the new image:
docker images
Common Scenarios
- Save Work in Progress: During development or testing, save the current state of a container as an image so it can be restored later.
- Create a Base Image: Create a custom base image for a specific application or environment configuration.
- Distribute Configurations: Save a specific configuration or application state as an image so it can be distributed to other team members or used in different environments.
docker commit The commit command is a powerful tool that lets users save the current state of a container as a new Docker image. With this command, users can create custom images for reuse or distribution later. Adding appropriate author and commit information helps track the history and changes of an image.
Docker Command Reference