Docker

What is the Docker Commit?

While working on Docker, when users create or build a container from an image, any modifications the user makes to the container’s file system are only made within that container instance. The recent changes are not saved when the container is deleted or stopped. In this situation, the “docker commit” command is used to create a new image that includes the changes made to the running container and saves them.

This article will illustrate the following content:

What is Docker Commit?

Docker commit is a command in Docker that permits users to create a new image from a particular container’s modifications. It is helpful when users want to save the state of a container as a reusable image, or when they want to customize an existing image for their own purposes. The syntax is provided below:

Syntax:

docker commit [OPTIONS] <container-name/ID> <new-image-name>

What are the Different Options for Docker Commit Command?

The common options used for Docker commit command are given below:

Name Shorthand Description
–author -a It is used to specify the author’s name e.g., Laiba <[email protected]>.
–change -c It applies Docker file instructions to the created image.
–message -m It is used for the commit message.
–pause -p It pauses the container during the commit process.

How to Commit a Docker Container?

To commit a Docker container, follow the below-provided steps:

  • List all Docker images.
  • Select the specific Docker image.
  • Build the container from the selected image and access it.
  • Make some changes in the running container.
  • Commit changes to the new image using the “docker commit <container-name/ID> <new-image-name>” command.
  • Verification

Step 1: Display All Docker Images

First, list available Docker images and select the desired image:

docker images

In the above screenshot, all the Docker images can be seen. We have selected the “nginx” image.

Step 2: Build and Access Container

To build and start the Docker container from the Docker image, execute the below-provided command:

docker run -it --name nginxCont nginx bash

Here:

  • -it” flag is used to start the interactive terminal session within the container.
  • –name” sets the name of the container to “nginxCont”.
  • nginx” is the Docker image to use for the container.
  • bash” is utilized to start the bash shell in the container:

The bash of the Nginx container has opened.

Step 3: Make Changes in Container

Next, make some modifications to the running container. For instance, we have created a new file named “test.txt” file with some content:

echo "This is test file" > test.txt

The output shows that the “test.txt” file has been created with relevant content.

Step 4: Verification

To verify whether the file has been created or not, list all the container’s content using the “ls” command and then execute the “cat <file-name>” command to view the file’s content:

ls

cat test.txt

The above output shows that the “test.txt” file has been created and its content can also be seen.

Step 5: Commit Changes

After that, keep the current container running and open a new terminal. Then, run the “docker commit <container-name> <new-image-name>” command to save modifications to a new image:

docker commit nginxCont nginx-img:V1.0

Step 6: Verification

To verify whether the changes have been saved to the new image or not, display all Docker images:

docker images

In the above output, the new image i.e., “nginx-img” with tag “V1.0” has been created successfully with recent changes.

How to Commit a Docker Container with New Configurations (–change)?

To commit a Docker container with the new configuration, utilize the “docker commit –change “ENV DEBUG=true” <container-name/ID> <new-image-name>” command. For verification, the “docker inspect -f “{{ .Config.Env }}” <new-image-name>” command is used.

Step 1: Commit Container with Configuration

To commit a particular container with configuration, utilize the “–change” option with the “docker commit” command and specify the desired configurations:

docker commit --change "ENV DEBUG=true" nginxCont mynginx-img:V3.0

Step 2: Verification

To ensure that the commit has been made with the desired configuration or not, execute the below-provided command:

docker inspect -f "{{ .Config.Env }}" mynginx-img:V3.0

The above output indicates that the commit has been made successfully with the specified configurations.

Conclusion

Docker commit is a Docker command that creates a new image from the modifications made to an existing container. It permits users to capture the current state of a container and save it as a new image that can be utilized to create a similar container in the future. The syntax of the Docker commit command is “docker commit [OPTIONS] <container-name/ID> <new-image-name>”. This article has explained Docker commit, its options, and the method of committing a container.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.