Docker Config Reference (docker image)
We go over many examples of the docker image command!
The docker image command serves as a parent or management command within the Docker CLI, providing a suite of subcommands for handling Docker images. It encompasses operations such as listing (docker image ls), removing (docker image rm), pruning unused images (docker image prune), inspecting details (docker image inspect), and more. This structure allows for comprehensive image management, enabling users to perform targeted actions on images stored locally or interact with registries. In essence, docker image acts as a gateway to a broader set of image-related functionalities, promoting organized and extensible workflows in containerized environments.
In contrast, the docker images command is a legacy alias or shortcut specifically for docker image ls, which lists locally available Docker images. It does not support subcommands and is limited to enumeration tasks, displaying attributes like repository, tag, image ID, creation time, and size. While functionally equivalent to docker image ls in output, docker images is retained for backward compatibility but is less versatile, as it cannot access the full range of image management capabilities offered by the parent docker image command. Users are encouraged to adopt docker image ls for consistency in modern Docker practices.
Example 1
docker image ls
This command lists all locally stored Docker images, displaying details such as repository, tag, image ID, creation time, and size, which is essential for inventory management in a development environment.
Example 2
docker image ls --all
This command lists all images, including intermediate layers from builds, providing a comprehensive view for cleanup or debugging failed builds.
Example 3
docker image ls --filter "dangling=true"
This command filters and lists dangling images (untagged and not referenced by any container), aiding in identifying unused resources for pruning.
Example 4
docker image ls --format "{{.ID}} {{.Repository}}:{{.Tag}}"
This command formats the output to show only image IDs and repository:tag combinations, facilitating scripted parsing for automation tasks.
Example 5
docker image ls --quiet
This command lists only the image IDs in quiet mode, which is suitable for piping into other commands like removal scripts.
Example 6
docker image rm nginx:latest
This command removes the "nginx:latest" image, freeing disk space, provided no containers are using it, which is useful in resource-constrained environments.
Example 7
docker image rm --force alpine:3.18
This command forcibly removes the "alpine:3.18" image, even if in use by stopped containers, ensuring cleanup in maintenance scenarios.
Example 8
docker image rm sha256:abcdef123456
This command removes an image by its digest "sha256:abcdef123456", offering precision when tags are ambiguous or multiple.
Example 9
docker image prune
This command removes all unused images, prompting for confirmation, which helps reclaim storage in automated build pipelines.
Example 10
docker image prune --all --force
This command forcibly prunes all unused images, including those not dangling, without confirmation, for aggressive cleanup in production hosts.
Example 11
docker image prune --filter "until=24h"
This command prunes images unused for the last 24 hours, allowing time-based cleanup for temporary build artifacts.
Example 12
docker image history ubuntu:24.04
This command displays the layer history of the "ubuntu:24.04" image, including commands and sizes, for auditing base image composition.
Example 13
docker image history --no-trunc node:20
This command shows the full, untruncated history of the "node:20" image, revealing complete build commands for detailed analysis.
Example 14
docker image history --quiet mysql:8.0
This command lists only the layer IDs of the "mysql:8.0" image history in quiet mode, useful for extracting identifiers.
Example 15
docker image inspect nginx:latest
This command inspects the "nginx:latest" image, outputting JSON metadata including architecture, configuration, and layers, for compatibility checks.
Example 16
docker image inspect --format '{{.Architecture}}' alpine:3.18
This command formats the output to show only the architecture of the "alpine:3.18" image, aiding in multi-platform verification.
Example 17
docker image pull redis:7
This command pulls the "redis:7" image from Docker Hub, downloading layers if not present, essential for initial setup of caching services.
Example 18
docker image pull --all-tags postgres
This command pulls all tagged versions of the "postgres" image, supporting comprehensive local repositories for testing multiple versions.
Example 19
docker image pull --platform linux/arm64 tensorflow/tensorflow:latest
This command pulls the "tensorflow/tensorflow:latest" image for ARM64 platform, enabling cross-architecture deployments.
Example 20
docker image push myrepo/app:1.0
This command pushes the local image "myrepo/app:1.0" to a remote registry, facilitating distribution in CI/CD workflows.
Example 21
docker image push --all-tags myrepo/base
This command pushes all tags of the "myrepo/base" image to the registry, ensuring version availability.
Example 22
docker image save nginx:latest > nginx.tar
This command saves the "nginx:latest" image to a tar file "nginx.tar", allowing offline transport or backups.
Example 23
docker image save --output alpine.tar alpine:3.18
This command saves the "alpine:3.18" image directly to "alpine.tar" using the --output flag, streamlining archival processes.
Example 24
docker image load -i ubuntu.tar
This command loads an image from the tar file "ubuntu.tar" into the local Docker store, restoring from backups or transfers.
Example 25
docker image load --quiet < node.tar
This command quietly loads an image from "node.tar" via stdin redirection, suppressing output for scripted integrations.
Example 26
docker image tag mysql:8.0 myrepo/mysql:custom
This command tags the "mysql:8.0" image as "myrepo/mysql:custom", preparing for custom registry pushes or versioning.
Example 27
docker image tag sha256:abcdef123456 myrepo/base:dev
This command tags an image by its digest "sha256:abcdef123456" as "myrepo/base:dev", ensuring immutable referencing.
Example 28
docker image build -t app:1.0 .
This command builds an image "app:1.0" from the current directory's Dockerfile, incorporating local context for application packaging.
Example 29
docker image build --no-cache -t base:clean .
This command builds "base:clean" without caching, forcing fresh layer creation for dependency verification.
Example 30
docker image build --build-arg ENV=prod -t prodapp:latest .
This command builds "prodapp:latest" passing a build argument "ENV=prod", customizing for production environments.