Docker Tutorial
Docker Installation
How to use Docker
Docker Instance
Docker Container Lifecycle Command
Docker Container Operation Command
Docker Container rootfs Command
Docker Mirror Repository Command
Docker Local Image Management Command
Docker info|version Command
The docker commit
command is used to create a new image from a container's changes. This is helpful if you've made modifications to a running container and want to save those changes as a new image. In this tutorial, we'll cover how to use the docker commit
command.
ubuntu
image:docker run -it --name=my-ubuntu ubuntu
This command runs an Ubuntu container in interactive mode with a terminal attached, and names it my-ubuntu
.
echo "Hello, Docker!" > /hello.txt
Exit the container: Type exit
or press Ctrl-d
to exit the container. The container will stop running.
Commit the changes: Use the docker commit
command to create a new image from the container's changes:
docker commit my-ubuntu my-ubuntu-image
This command creates a new image named my-ubuntu-image
from the changes made in the my-ubuntu
container. Replace my-ubuntu
with the name of your container, and my-ubuntu-image
with your desired image name.
docker images
command to list the available images on your system, and verify that your new image is present:docker images
docker run -it --rm my-ubuntu-image cat /hello.txt
In this example, we run a container from the my-ubuntu-image
image and use the cat
command to display the contents of the /hello.txt
file. The --rm
flag tells Docker to automatically remove the container when it exits.
The docker commit
command is a useful tool for creating new images from modified containers. However, it's generally recommended to use Dockerfiles for creating images, as they provide a more reproducible and maintainable approach to image creation. Use docker commit
for quick tests and prototyping, but rely on Dockerfiles for creating production images.
How to Use Docker Commit Command:
docker commit
command creates a new image from changes made to a container.docker commit
:docker commit container_id my_custom_image:tag
Docker Commit Command Options:
docker commit
supports options like -a
for author and -m
for commit message.docker commit
:docker commit -a "John Doe" -m "Added custom configuration" container_id my_custom_image:tag
Tagging and Naming Images with Docker Commit:
-a
and -m
options to set author and commit message, and specify the image name and tag.docker commit
:docker commit -a "John Doe" -m "Snapshotting container" container_id my_custom_image:tag
Versioning Docker Images Using Commit:
docker commit
involves tagging images with version numbers or labels.docker commit
:docker commit container_id my_custom_image:v1