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

Docker commit 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.

  • Run a container: First, run a container from a base image. For this example, we'll use the 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.

  • Make changes inside the container: Inside the running container, make some changes. For example, create a new file:
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.

  • Verify the new image: Use the docker images command to list the available images on your system, and verify that your new image is present:
docker images
  • Run a container from the new image: To test the new image, run a container from it and verify that the changes you made are present:
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.

  1. How to Use Docker Commit Command:

    • Description: The docker commit command creates a new image from changes made to a container.
    • Code: Example of using docker commit:
      docker commit container_id my_custom_image:tag
      
  2. Docker Commit Command Options:

    • Description: docker commit supports options like -a for author and -m for commit message.
    • Code: Example of using options with docker commit:
      docker commit -a "John Doe" -m "Added custom configuration" container_id my_custom_image:tag
      
  3. Tagging and Naming Images with Docker Commit:

    • Description: Use the -a and -m options to set author and commit message, and specify the image name and tag.
    • Code: Example of tagging and naming with docker commit:
      docker commit -a "John Doe" -m "Snapshotting container" container_id my_custom_image:tag
      
  4. Versioning Docker Images Using Commit:

    • Description: Versioning with docker commit involves tagging images with version numbers or labels.
    • Code: Example of versioning with docker commit:
      docker commit container_id my_custom_image:v1