Docker

How to Commit a Running Docker Container?

In Docker, developers use Docker containers to package their software applications with dependencies into a self-contained environment. It helps deploy and run the application consistently across various environments. Users may need to save the current state of the container or create a backup of the container. In this situation, they can commit the running container to save the current changes to the new image and use that image in the future for creating new containers.

This article will illustrate the procedure to commit to a running Docker container.

How to Commit a Running Docker Container?

To commit a running Docker container, check out the below-listed steps:

Step 1: View and Select a Running Container

First, display all the running containers and select a specific container:

docker ps

The above output shows that there is only one running container i.e., “Cont1” and we will use it in upcoming steps.

Step 2: Access Running Container

Then, execute the “docker exec -it <container-name> bash” to open the Bash shell inside the running container:

docker exec -it Cont1 bash

The above-provided command has opened a Bash shell and now users can execute the command within the running container.

Step 3: Make Changes in the Running Container

After that, make some changes 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 content has been stored in the “test.txt” file.

Step 4: Verification

Type out the “ls” command and list all the container’s content to view the newly created file. Then, run the “cat <file-name>” command to view its content:

ls
cat test.txt

In the above output, the newly created file “test.txt” and its content can also be seen in the terminal.

Step 5: Commit the Running Container

Now, keep the current container running and open a new terminal window. Then, enter the “docker commit <container-name> <new-image-name>” command to save the latest changes to a new image:

docker commit Cont1 myimg1:V1.0

Step 6: Verify Committed Changes

For the verification, first, list all Docker images to view the new Docker image where the changes have been saved:

docker images

The new image i.e., “myimg1” with tag “V1.0” has been created successfully with new modifications.

Now, build and start a new container from the newly created Docker image and access it is using the below-listed command:

docker run -it --name Cont2 myimg1:V1.0 bash

Here:

  • -it” flag is utilized to start the interactive terminal session in the specified container.
  • –name” sets the container’s name to “Cont2”.
  • myimg1:V1.0” is the Docker image to use for the container.
  • bash” is used to start the bash shell in the container:

After that, use the “ls” to list the content of the new container and verify whether its content is the same as the previous container. Then, utilize the “cat <file-name>” command to view the file’s content:

ls
cat test.txt

It can be observed that the content of the new container “Cont2” is the same as the previous container “Cont2”.

Conclusion

To commit a running Docker container, first, display all running containers and select a desired one. Then, access the running container and make some changes to it. Next, commit a running container via the “docker commit <container-name> <new-image-name>” command and verify changes. This article has explained the method to commit to a running Docker 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.