How to use the command 'docker commit' (with examples)

How to use the command 'docker commit' (with examples)

The ‘docker commit’ command is used to create a new image from the changes made to a container. It allows you to save the state of a container as an image, which can be used to create new containers or share with others.

Use case 1: Create an image from a specific container

Code:

docker commit container image:tag

Motivation:

Creating an image from a specific container is useful when you have made changes to a running container and want to preserve those changes. By committing the container, you can create a new image with the changes included, making it easier to deploy the same configuration in other environments.

Explanation:

  • container: The ID or name of the container you want to commit.
  • image:tag: The name and tag you want to assign to the new image.

Example output:

SHA:2569509ba6abfddcc2ba48f4190d44af0890446e4d4a6dfaaada42c9d130605f

Use case 2: Apply a CMD Dockerfile instruction to the created image

Code:

docker commit --change="CMD command" container image:tag

Motivation:

When creating an image from a container, you can apply additional Dockerfile instructions to customize the image. The ‘–change=“CMD command”’ option allows you to specify a command to be executed when the container starts from the new image.

Explanation:

  • --change="CMD command": Specifies the command to run when the container starts.
  • container: The ID or name of the container you want to commit.
  • image:tag: The name and tag you want to assign to the new image.

Example output:

SHA:2569509ba6abfddcc2ba48f4190d44af0890446e4d4a6dfaaada42c9d130605f

Use case 3: Apply an ENV Dockerfile instruction to the created image

Code:

docker commit --change="ENV name=value" container image:tag

Motivation:

The ‘–change=“ENV name=value”’ option allows you to add environment variables to the new image. This is useful for setting configuration values or providing runtime parameters to the container when it is started.

Explanation:

  • --change="ENV name=value": Sets an environment variable with the specified name and value.
  • container: The ID or name of the container you want to commit.
  • image:tag: The name and tag you want to assign to the new image.

Example output:

SHA:2569509ba6abfddcc2ba48f4190d44af0890446e4d4a6dfaaada42c9d130605f

Use case 4: Create an image with a specific author in the metadata

Code:

docker commit --author="author" container image:tag

Motivation:

Adding an author to the metadata of an image can provide useful information about who created or modified the image. This can be helpful for attribution or troubleshooting purposes.

Explanation:

  • --author="author": Specifies the name of the author.
  • container: The ID or name of the container you want to commit.
  • image:tag: The name and tag you want to assign to the new image.

Example output:

SHA:2569509ba6abfddcc2ba48f4190d44af0890446e4d4a6dfaaada42c9d130605f

Use case 5: Create an image with a specific comment in the metadata

Code:

docker commit --message="comment" container image:tag

Motivation:

Adding a comment to the metadata of an image can provide additional context or information about the image. This can be useful for documenting changes or providing details about the container’s purpose.

Explanation:

  • --message="comment": Specifies the comment to add to the image’s metadata.
  • container: The ID or name of the container you want to commit.
  • image:tag: The name and tag you want to assign to the new image.

Example output:

SHA:2569509ba6abfddcc2ba48f4190d44af0890446e4d4a6dfaaada42c9d130605f

Use case 6: Create an image without pausing the container during commit

Code:

docker commit --pause=false container image:tag

Motivation:

By default, the ‘docker commit’ command pauses the container during the commit process to ensure consistency. However, in some cases, you may want to create an image without interrupting the container’s operations. The ‘–pause=false’ option allows you to skip the pause, enabling the container to continue running uninterrupted.

Explanation:

  • --pause=false: Disables the pause of the container during commit.
  • container: The ID or name of the container you want to commit.
  • image:tag: The name and tag you want to assign to the new image.

Example output:

SHA:2569509ba6abfddcc2ba48f4190d44af0890446e4d4a6dfaaada42c9d130605f

Use case 7: Display help

Code:

docker commit --help

Motivation:

When working with complex commands like ‘docker commit’, it’s helpful to have access to detailed information on the available options and their usage. By running ‘docker commit –help’, you can view the command’s documentation directly in the terminal.

Example output:

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

Create a new image from a container's changes

Options:
  --author value               Author (e.g., "John Hannibal Smith <hannibal@a-team.com>") (default: )
  -c, --change value           Apply Dockerfile instruction to the created image (default: [])
  --message value              Set commit message on the created image (default: )
  --pause                      Pause container during commit (default: true)

Conclusion:

The ‘docker commit’ command is a powerful tool for creating images from containers and customizing them with additional instructions and metadata. With the ability to specify changes, environment variables, authors, comments, and more, you can easily capture and share container configurations with others.

Related Posts

How to use the command 'Wait-Process' (with examples)

How to use the command 'Wait-Process' (with examples)

The ‘Wait-Process’ command is used to wait for the specified process or processes to be stopped before accepting more input.

Read More
How to use the command scutil (with examples)

How to use the command scutil (with examples)

scutil is a command-line tool on macOS that allows users to manage various system configuration parameters.

Read More
How to use the command rbac-lookup (with examples)

How to use the command rbac-lookup (with examples)

The rbac-lookup command is a tool that allows you to find roles and cluster roles attached to any user, service account, or group name in your Kubernetes cluster.

Read More