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

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

The docker image command is used to manage Docker images. It provides various operations for listing, deleting, and inspecting images. This article will illustrate each of the use cases for this command.

Use case 1: List local Docker images

Code:

docker image ls

Motivation:

  • This example is useful to retrieve a list of all the locally available Docker images on the system.
  • It allows users to verify if the required images are present before running containers.

Explanation:

  • docker image ls lists all the Docker images available in the local image cache.
  • It provides detailed information about the images, including their repository, tag, image ID, and size.

Example output:

REPOSITORY     TAG       IMAGE ID       CREATED       SIZE
ubuntu         latest    1234567890     1 week ago    123MB
nginx          1.21      abcdefghij     2 weeks ago   76MB

Use case 2: Delete unused local Docker images

Code:

docker image prune

Motivation:

  • Over time, the local Docker image cache can accumulate unused images, taking up disk space.
  • This example helps to clean up the unused images, freeing up storage resources.

Explanation:

  • docker image prune removes unused images from the local image cache.
  • It prompts for confirmation before deleting the images.

Example output:

WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Deleted: sha256:abcdefghij
Deleted: sha256:klmnopqrst

Use case 3: Delete all unused images (not just those without a tag)

Code:

docker image prune --all

Motivation:

  • In addition to removing images without a tag, it may be necessary to delete all unused images, even those with a tag.
  • This example provides a way to clean up all unused images, regardless of their tags.

Explanation:

  • docker image prune --all removes all unused images from the local image cache, including those with a tag.
  • It prompts for confirmation before deleting the images.

Example output:

WARNING! This will remove all images (including tagged ones) that are not referenced by any container.
Are you sure you want to continue? [y/N] y
Deleted: sha256:abcdefghij
Deleted: sha256:klmnopqrst

Use case 4: Show the history of a local Docker image

Code:

docker image history image

Motivation:

  • This example allows users to inspect the history of a Docker image.
  • It provides information about the layers and commands used in building the image.

Explanation:

  • docker image history image displays the history of a specific Docker image.
  • It shows all the layers used in building the image, along with the corresponding commands.

Example output:

IMAGE          CREATED          CREATED BY                                      SIZE    COMMENT
abcdefghij     2 weeks ago      /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon …   0B
klmnopqrst     2 weeks ago      /bin/sh -c #(nop)  EXPOSE 80                    0B

Conclusion:

The docker image command is a powerful tool for managing Docker images. It allows users to list images, delete unused ones, show image history, and more. Understanding these use cases can help streamline Docker image management and ensure efficient use of storage resources.

Related Posts

How to Use the Command csvcut (with examples)

How to Use the Command csvcut (with examples)

The csvcut command is a tool included in csvkit that allows for filtering and truncating CSV files.

Read More
How to use the command 'subfinder' (with examples)

How to use the command 'subfinder' (with examples)

The ‘subfinder’ command is a subdomain discovery tool that is used to discover valid subdomains for websites.

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

How to use the command rustfmt (with examples)

The rustfmt command is a tool used for formatting Rust source code.

Read More