Docker Commit Container: Explained with Practical Examples

docker commit container examples

Docker is a powerful tool for containerization. It allows you to package applications and their dependencies into a container that can run on any system. One of the commands you’ll use in Docker is docker commit. This command helps you create a new image from a container. This is useful when you’ve made changes inside a container and want to save those changes for later use.

In this article, we’ll explore how to use the docker commit command with practical examples.

What is Docker Commit?

The docker commit command allows you to capture the current state of a running container and save it as a new Docker image. This command can be useful when you make changes directly inside a running container.

For example, installing new software, configuring services, or if you want to preserve those changes in a reusable image. The docker commit can be valuable in scenarios where you need to capture a container’s state quickly or when rapid prototyping.

Here are common use cases for Docker commit:

  • Saving the state of a container after manual changes.
  • Capturing a snapshot of an environment during development.
  • Creating quick images without needing a Dockerfile.

Basic Syntax

The docker commit command creates a new image from a container’s changes. Here’s the basic syntax:

 # docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Let’s break down the options.

  • CONTAINER: The ID or name of the container you want to commit.
  • REPOSITORY[:TAG]: The name of the image you want to create, with an optional tag.
  • OPTIONS: Additional options like –author, –message, and –pause.

Now, let’s look at how to use this in practice.

Creating a Docker Image from a Running Container

Imagine you have a running container where you’ve installed some software or made some configuration changes. You want to save these changes for future use. Here’s how.

1. Start with a Running Container.

Let’s say you have a container running an Ubuntu image. You want to install Apache on it.

 # docker run -it ubuntu bash

This command opens a bash shell in a new container.

2. Install Software or Make Changes.

Install Apache inside the container.

 # apt-get update && apt-get install -y apache2

After the installation is complete, your container has Apache installed.

3. Commit the Changes

Now, commit these changes to a new image.

 # docker commit -m "Installed Apache" -a "Your Name" container_id myimage:apache

Replace container_id with your actual container ID. The -m option adds a commit message, and -a sets the author name.

4. Verify the New Image

After committing the changes, verify that the new image has been created.

 # docker images

Output.

REPOSITORY          TAG         IMAGE ID        CREATED          SIZE
myimage             apache      5f0d9c5a7a1f    10 seconds ago   220MB
ubuntu              latest      2c014f0dfbf5    2 weeks ago      64.2MB

Your new image should be listed with the REPOSITORY name myimage and the TAG apache.

Tagging and Versioning Images

Tagging helps you version your images. This makes it easier to manage and deploy them. For example, you can tag your image with a version number:

 # docker commit -m "Added new configuration" container_id myimage:v1

You now have an image tagged as v1.

Assigning an Author to the Image

You can also specify the author of the commit using the -a option. This is helpful when working in a team or maintaining a clear record of who made changes.

 # docker commit -a "Hitesh Jethva <hitjethva@example.com>" container_id myimage

This command captures the container’s state and includes the author’s information, making it easier to track contributions.

Conclusion

The docker commit command is a powerful tool for saving changes in a container. Whether you’re saving a hotfix or creating a custom environment, it helps you preserve your work. However, remember to use it wisely and consider alternatives like Dockerfiles for more complex tasks.

With these practical examples, you should now feel confident using docker commit in your projects. Happy Dockerizing!

FAQs

1. Can I specify a commit message when using docker commit?

Yes, you can add a commit message with the -m flag.

2. Can I use docker commit to make changes to a stopped container?

Yes, docker commit works on both running and stopped containers to create new images.

3. How do I view the history of changes made to an image created using docker commit?

You can view the history of an image with: docker history image_name

4. Can I change the ENTRYPOINT or CMD of an image using docker commit?

Yes, you can use the -c option to modify the CMD or ENTRYPOINT.

About Hitesh Jethva

I am Hitesh Jethva, Founder and Author at Code2DevOps.com. With over 15 years of experience in DevOps and open source technologies, I am passionate about empowering teams through automation, continuous integration, and scalable solutions.

View all posts by Hitesh Jethva