
A Docker image is a self-contained package that includes everything needed to run a software application, such as the code, runtime, libraries, environment variables, and configuration files. Images are built in layers, with each layer representing a specific change. This layered architecture makes images efficient to store and distribute.
This article shows you how to manage Docker images using the Docker CLI commands. You will learn how to pull, push, tag, remove, and inspect images. You will also see how to check an image's layers.
The later sections of this article cover each command in detail
docker image pullDocker images are stored in registries, such as Docker Hub, which host prebuilt images for popular operating systems, frameworks, and applications.
The docker image pull command downloads an image from a remote registry to your local machine. This makes the image available for creating and running containers on your system.
NAME[:TAG] – The name of the image and an optional tag (for example, nginx:latest).[OPTIONS] – Common options that modify the command behavior, like --all-tags to pull all versions.Pull the latest nginx image.
The command above pulls the nginx image from the registry with the latest tag.
Pull all tags of an nginx.
The command above pulls Docker images for every available tag of nginx from the registry.
docker image pushThe docker image push command uploads a local image to a remote registry. Before pushing, you must properly tag the image using the correct format. Tags help identify and version images in the registry. The standard tagging format is [registry-hostname[:port]]/repository[:tag].
NAME[:TAG] - The target repository and tag.OPTIONS - Common options that modify the command behavior, like --quiet to reduce output.Tag the local image.
This command does not change the original app:latest image. It simply creates a new tag, myuser/app:v1, pointing to the same image ID.
Replace myuser with your Docker registry username.
Push it to a remote registry.
You must run docker login and authenticate before pushing to your Docker repository.
docker image rmThe docker image rm command deletes one or more Docker images from your local system. This is useful for freeing up disk space or removing unused or outdated image versions from your development environment.
IMAGE... - The image name, tag, or ID.OPTIONS - Common options that modify the command behavior, like --force to forcibly remove an image even if it's in use.List the images and locate the name and ID of the image you want to remove. You can then pass the ID or name to docker image rm
Remove the nginx image.
This command removes the nginx image from your system. By default, it removes the latest tag, but you can specify a different tag if needed.
You can specify multiple images to remove them.
This command removes the mysql and alpine images with the latest tag from your system.
You can also use the docker image prune command to remove all the unused dangling (untagged and not referenced by any containers) images.
docker image inspectThe docker image inspect command allows you to inspect an image and see how it was constructed (its layers). This command works on images, containers, secrets, and other Docker objects.
OPTIONS - Common options that modify the command behavior, like --format to set the output format.[OBJECT] - The name or ID of the object, in this case, the image name or image ID of the image you want to inspect.Inspect the nginx image.
Inspect the image by its image ID.
Both commands return the image details in a JSON output.
docker image historyThe docker image history command lists the commands that created the image's layers. This command gives you an insight into what the image is built from.
[OPTIONS] - Modify the command behavior, like --no-trunc, which gives you complete information on the output fields.[IMAGE] - Is the image ID or image name.Check the history of alpine image.
The command shows the layers of the alpine image.
See the complete data of the output fields.
The command shows the complete data of every output field.
In this article, you learned how to pull, push, tag, remove, and inspect Docker images using the Docker CLI commands. You also learned how to check the layers of an Image. For more information, refer to the Docker documentation.
0 Comments
Be the first to comment and share your perspective with the community.