How to use the command 'docker image' (with examples)
Docker is a powerful platform that enables developers to automate the deployment of applications inside lightweight, portable containers. These containers include everything needed to run an application, such as the code, runtime, libraries, and system tools. At the heart of this functionality lies Docker images, which are essentially the building blocks of Docker containers. The docker image
command is a central piece of Docker’s command-line interface (CLI) used for managing these Docker images. This command facilitates various operations necessary in the lifecycle of an image, such as listing, cleaning up, and understanding the history of image creation. In this article, we explore some common use cases of the docker image
command with practical examples to help illustrate its functionality.
Use Case 1: Listing Local Docker Images
Code:
docker image ls
Motivation:
Listing images is one of the most common tasks when dealing with Docker. This functionality allows developers and system administrators to have a clear view of all images available locally on their system. By running the docker image ls
command, users can gain insights into the repositories, tags, and IDs of images they might consider using, deleting, or updating. This command essentially acts as an inventory check, ensuring you understand what resources are available to you on your local environment.
Explanation:
- The command
docker image
is used to invoke Docker’s image management functionalities. ls
is a subcommand that stands for ’list’. It fetches and displays a list of images stored locally on your machine.
Example Output: The command will produce a tabulated list of images, showing columns like REPOSITORY, TAG, IMAGE ID, CREATED, and SIZE. For instance:
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest a9eb17255234 2 weeks ago 72.9MB
alpine 3.10 b7b28af77ffe 3 months ago 5.58MB
nginx stable dafe5c5c032f 1 month ago 125MB
Use Case 2: Deleting Unused Local Docker Images
Code:
docker image prune
Motivation:
Over time, Docker users accumulate numerous images that are no longer in use. These orphaned images can take up significant disk space, leading to inefficient use of resources. Deleting unused local Docker images with docker image prune
is a valuable practice to maintain a clean and efficient workspace. This operation helps manage storage by removing images that aren’t associated with any running containers, thereby freeing up disk space for other uses and simplifying the image list you work with regularly.
Explanation:
- The
docker image
part of the command is used to access the image-specific functions of Docker. prune
is a subcommand that signifies a cleaning operation. It identifies and removes all dangling images (those without a tag).
Example Output: Upon execution, Docker will provide a summary of the images it has removed, such as:
Deleted Images:
untagged: sha256:a4b2e53d7e5a
untagged: sha256:72de5119fbea
Total reclaimed space: 1.5GB
Use Case 3: Deleting All Unused Docker Images
Code:
docker image prune --all
Motivation:
Sometimes it’s necessary to conduct a more thorough cleanup of Docker images. Unlike docker image prune
, which only targets dangling images, the --all
option extends the cleaning process to include any image not currently used by an existing container. This is especially useful in development environments where images may be frequently updated, and old versions are no longer needed. By employing this command, developers can achieve a more extensive clearing of unused data, ensuring optimal use of system resources like storage and maintaining a streamlined set of images.
Explanation:
docker image
accesses the command section for image-related functionalities.prune
initiates the cleanup of images.--all
is a flag that enhances the command by instructing Docker to remove all unused images, not just those that lack a tag.
Example Output: Executing this command will print out a list of all images removed, which might look like this:
Deleted Images:
untagged: sha256:a9eb17255234
untagged: sha256:b7b28af77ffe
untagged: sha256:dafe5c5c032f
Total reclaimed space: 7.5GB
Use Case 4: Showing the History of a Local Docker Image
Code:
docker image history image
Motivation: Understanding the history of a Docker image provides valuable insights into its construction, such as the steps taken to build it and the individual layers that comprise it. This knowledge is critical for debugging, ensuring compliance with best practices, and optimizing image size. Developers can use this to inspect who’s made changes, what those changes entailed, and to verify the integrity and intentions behind each alteration in the image’s history. The command is a powerful tool for those who need to ensure their images are built correctly and efficiently.
Explanation:
docker image
accesses image management functionalities.history
directs Docker to display the build history of an image.image
should be replaced by the actual name or ID of the image you are interested in.
Example Output: Running this command provides an ordered list of commands executed during the image’s build process, each with details like their size and creation time. For example:
IMAGE CREATED CREATED BY SIZE
a9eb17255234 2 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 2 weeks ago /bin/sh -c apt-get update 5MB
<missing> 2 weeks ago /bin/sh -c apt-get install -y python3 85MB
Conclusion:
The docker image
command is an essential component of Docker’s CLI, offering extensive capabilities for managing the lifecycle of images within Docker. Whether you aim to list available images, efficiently maintain your system by pruning unnecessary data, or dive deep into the history of an image’s composition, this command provides the necessary tools. By mastering these use cases, users can significantly streamline their workflow, optimizing both their images and their overall Docker environment. These examples illustrate the power and flexibility of Docker, empowering users to build, deploy, and manage applications with increased confidence and efficiency.