Skip to main content

Working with Docker Images

1. Pulling Images

Command Description Example
docker pull Download an image from a registry docker pull nginx
docker pull <image>:<tag> Download a specific image tag docker pull node:18-alpine

Example: Pulling a specific image tag

docker pull python:3.11-slim
Note: If no tag is specified, latest is assumed by default.

2. Listing Images

Command Description Options Example
docker images List all images on the local system -a (all), --format, --filter docker images
docker image ls Alias for docker images Same as docker images docker image ls
docker images --format Format output using Go template --format with Go template syntax docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"
docker images --filter Filter images by criteria --filter with key=value pairs (e.g., reference=nginx) docker images --filter "reference=nginx"
Note: By default, docker images shows only the latest image per repository and tag. Use -a to show all images including intermediate ones.

3. Searching Images

Command Description Example
docker search Search Docker Hub for images docker search nginx
--filter Filter search results by criteria docker search --filter is-official=true nginx
--limit Limit number of results (max 100) docker search --limit 10 nginx

Example: Search for official Nginx images

docker search --filter is-official=true nginx

Example: Limit search results to 5

docker search --limit 5 node
Note: Docker Hub is the default registry. For private registries, use docker search [registry]/[image].

4. Inspecting Images

Command Description Example
docker inspect Get detailed low-level information about an image or container docker inspect nginx
docker image inspect Alias for docker inspect when used with images docker image inspect node:18
docker history Show the history of an image including layers and commands docker history alpine:latest

Example: Inspecting an Image

docker inspect nginx

Returns a JSON object with:

  • Image configuration
  • Mounts and volumes
  • Environment variables
  • Exposed ports
  • Entrypoint and CMD

Example: Viewing Image History

docker history node:18

Shows:

  • Each layer's command
  • Size of each layer
  • Creation time
Note: The docker inspect command returns a large JSON object. Use tools like jq to filter fields if needed.

5. Viewing Image History

Command Description Options Example
docker history Shows the history of an image --no-trunc - Show full command lengths
--format - Format output using Go template
docker history nginx:latest
Note: The docker history command does not show the full command that was used to create each layer. It only shows the commands that were run during the build process.

6. Tagging Images

Tag Format Description Example
repository:tag Standard format for tagging images nginx:1.21
registry/repository:tag With registry prefix (e.g., Docker Hub) docker.io/library/nginx:1.21
repository:latest Default tag when no tag is specified nginx:latest

Example: Tagging an Image

docker tag nginx:latest my-nginx:production

Example: Pushing Tagged Image

docker push my-nginx:production
Note: Tags are mutable; re-tagging the same image ID will update the tag reference.

7. Removing Images

Action Command Description
Remove image by ID or tag docker rmi [image-id|image-name] Deletes the specified image from local storage
Force remove image docker rmi -f [image-id|image-name] Removes image even if it is in use by a container
Remove image and all unused parents docker rmi --no-prune [image-id|image-name] Removes image and any parent images that are not referenced by other images

Example: Remove a Docker Image

docker rmi nginx:latest
Note: If the image is currently being used by a running container, Docker will prevent removal unless you use docker rmi -f.
Warning: Removing an image will delete all layers associated with it, freeing up disk space. Be cautious when removing base images that may be used by other containers or images.

8. Pruning Unused Images

Example: Remove unused images

docker image prune
Option Values Notes
-a true Remove all unused images, not just dangling ones
--filter label=<key>=<value> Filter images by label
--force true Do not prompt for confirmation
Note: Dangling images are those with no tag and no containers using them.

Example: Remove images with specific label

docker image prune --filter label=environment=staging

Example: Remove all unused images without confirmation

docker image prune -a --force

9. Saving Images to Archive

Command Description Options Example
docker save Saves one or more images to a tar archive (streamed to STDOUT by default) --output, -o to specify file output docker save -o my-image.tar nginx:latest
Note: The archive includes all image layers and metadata, allowing for easy transport or backup.
Warning: Using docker save with multiple images requires specifying the -o option to avoid overwriting.

10. Loading Images from Archive

Action Command Description
Load image from tar archive docker load < image.tar Load a Docker image from a tar archive file
Load image with explicit file path docker load --input image.tar Load an image using the --input flag with file path
Load image and tag it docker load --input image.tar && docker tag old-name new-name Load image and apply a new tag to it after loading

Example: Loading a Docker Image Archive

# Save an image to a tar archive
docker save -o myapp.tar myapp:latest

# Load the image from the tar archive
docker load &lt; myapp.tar
Note: The docker load command reads the image from standard input or a file specified with --input. It can handle multiple images in one archive.

11. Filtering Images

Filter Type Flag Description Example
Name --filter name= Filter by image name pattern docker images --filter name=nginx
Reference --filter reference= Filter by image reference (tag or digest) docker images --filter reference=latest
Label --filter label= Filter by label key=value docker images --filter label=version=1.0
Before --filter before= Filter images created before a specific image ID or name docker images --filter before=nginx:latest
Since --filter since= Filter images created after a specific image ID or name docker images --filter since=alpine:latest
Dangling --filter dangling=true Filter dangling images (untagged images) docker images --filter dangling=true

Example: Filtering by Label

docker images --filter label=environment=production

Example: Combining Filters

docker images --filter name=nginx --filter label=version=1.0

12. Displaying Image Digests

Command Description Example
docker images --digests Lists all images with their digests docker images --digests
docker inspect <image> --format '{{.Id}}' Displays the image ID and digest docker inspect alpine:latest --format '{{.Id}}'
docker image inspect <image> Inspects image details including digest docker image inspect nginx:latest
Note: Image digests are unique identifiers for image layers and help ensure content integrity.

Was this article helpful?