Docker Images

What is a Docker Image?

A Docker Image is a read-only template used to create containers. It contains everything the application needs:

  • Operating system files
  • Runtime (Node.js, Python, Java, etc.)
  • Application code
  • Libraries and dependencies
  • Environment configuration

Think of an image as a recipe - it describes exactly how to build the application environment. You run the recipe (image) and get a cake (container).

Image Layers

Docker images are built in layers. Each instruction in a Dockerfile creates one layer.

Layer 4: COPY app files           <- your changes (small)
Layer 3: RUN npm install          <- your dependencies
Layer 2: RUN apt-get install curl <- your additions
Layer 1: FROM ubuntu:22.04        <- base OS layer (shared)

Key benefit: Layers are cached and shared.

Image A:  ubuntu -> curl -> nginx
Image B:  ubuntu -> curl -> nodejs

Both share the "ubuntu" and "curl" layers - stored only once on disk.

Image Naming Convention

[registry/][username/]image-name[:tag]

Examples:
  nginx                        <- official image, latest tag
  nginx:1.25                   <- official image, specific version
  ubuntu:22.04                 <- official Ubuntu 22.04
  irfan/my-app:1.0             <- your personal image
  registry.example.com/app:v2  <- from a private registry

If no tag is specified, Docker uses :latest by default.

All Docker Image Commands

docker pull - Download an Image

Downloads an image from a registry (Docker Hub by default).

# Pull the latest version
docker pull nginx

# Pull a specific version (tag)
docker pull nginx:1.25

# Pull Ubuntu 22.04
docker pull ubuntu:22.04

# Pull from a specific registry
docker pull registry.example.com/my-app:1.0

# Pull for a specific platform (useful on Apple Silicon / Windows)
docker pull --platform linux/amd64 nginx

docker images / docker image ls - List Images

Lists all images stored locally on your machine.

# List all local images
docker images

# Same command (newer syntax)
docker image ls

# List images with a specific name
docker images nginx

# List all images including intermediate layers
docker images -a

# Show only image IDs (useful for scripting)
docker images -q

# Show image digests (SHA256 hash)
docker images --digests

# Filter by label
docker images --filter "label=maintainer=irfan"

# Filter dangling images (untagged)
docker images --filter "dangling=true"

# Custom format output
docker images --format "{{.Repository}}:{{.Tag}} - {{.Size}}"

Example output:

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    a6bd71f48f68   2 weeks ago    187MB
ubuntu       22.04     a8780b506fa4   4 weeks ago    77.9MB
hello-world  latest    d2c94e258dcb   14 months ago  13.3kB

docker image inspect - Detailed Image Info

Shows detailed JSON information about an image.

# Inspect an image
docker image inspect nginx

# Inspect a specific field only
docker image inspect nginx --format "{{.Config.ExposedPorts}}"
docker image inspect nginx --format "{{.Architecture}}"
docker image inspect nginx --format "{{.Os}}"
docker image inspect nginx --format "{{.Config.Env}}"
docker image inspect nginx --format "{{.Config.Cmd}}"
docker image inspect nginx --format "{{.RootFS.Layers}}"

docker image history - Show Image Layers

Shows the build history of an image - all layers and their sizes.

# Show layer history
docker image history nginx

# Show full command (not truncated)
docker image history --no-trunc nginx

# Show only sizes and commands
docker image history --format "{{.Size}}\t{{.CreatedBy}}" nginx

Example output:

IMAGE          CREATED         CREATED BY                                SIZE
a6bd71f48f68   2 weeks ago    /bin/sh -c #(nop)  CMD ["nginx" "-g" "d   0B
<missing>      2 weeks ago    /bin/sh -c #(nop)  EXPOSE 80               0B
<missing>      2 weeks ago    /bin/sh -c set -x && addgroup --system    62.1MB

docker image tag - Tag an Image

Creates a new tag (alias) for an existing image. Used before pushing to a registry.

# Tag an image with a new name
docker image tag nginx my-nginx

# Tag with your Docker Hub username
docker image tag nginx irfan/my-nginx:1.0

# Tag with a specific registry
docker image tag nginx registry.example.com/nginx:production

# Re-tag an existing image
docker image tag nginx:latest nginx:stable

docker rmi / docker image rm - Remove Images

Deletes images from your local machine.

# Remove by name
docker rmi nginx

# Remove by image ID
docker rmi a6bd71f48f68

# Remove a specific tag
docker rmi nginx:1.25

# Remove multiple images at once
docker rmi nginx ubuntu hello-world

# Force remove (even if containers are using it)
docker rmi -f nginx

# Remove all dangling (untagged) images
docker image prune

# Remove ALL unused images (not just dangling)
docker image prune -a

# Remove without confirmation prompt
docker image prune -f
docker image prune -a -f

docker image prune - Clean Up Unused Images

# Remove only dangling images (images with no tag)
docker image prune

# Remove all unused images (not used by any container)
docker image prune -a

# Remove without asking for confirmation
docker image prune -f

# Filter by age
docker image prune --filter "until=24h"
docker image prune --filter "until=2024-01-01"

docker search - Search Docker Hub

Searches for images on Docker Hub.

# Search for an image
docker search nginx

# Search for official images only
docker search --filter "is-official=true" nginx

# Search for automated builds
docker search --filter "is-automated=true" nginx

# Limit results
docker search --limit 5 nginx

# Filter by star count
docker search --filter "stars=100" nginx

# Custom output format
docker search --format "{{.Name}}\t{{.StarCount}}" nginx

Example output:

NAME         DESCRIPTION                    STARS   OFFICIAL   AUTOMATED
nginx        Official build of Nginx.       19000   [OK]
bitnami/nginx Bitnami nginx Docker Image    180                [OK]

docker save - Export Image to a File

Saves an image to a .tar file so you can transfer it to another machine without a registry.

# Save a single image
docker save nginx > nginx.tar

# Save with -o flag (output file)
docker save -o nginx.tar nginx

# Save a specific tag
docker save -o nginx-1.25.tar nginx:1.25

# Save multiple images into one file
docker save -o images.tar nginx ubuntu hello-world

# Save and compress
docker save nginx | gzip > nginx.tar.gz

docker load - Import Image from a File

Loads an image from a .tar file created by docker save.

# Load an image from file
docker load < nginx.tar

# Load with -i flag (input file)
docker load -i nginx.tar

# Load compressed file
docker load -i nginx.tar.gz

# Load and show output
docker load -i nginx.tar --quiet

docker image build - Build Image from Dockerfile

Builds an image using a Dockerfile.

# Build from Dockerfile in current directory
docker build .

# Build and tag with a name
docker build -t my-app .

# Build with a name and version tag
docker build -t my-app:1.0 .

# Build from a specific Dockerfile path
docker build -f /path/to/Dockerfile -t my-app .

# Build and pass build arguments
docker build --build-arg VERSION=1.0 -t my-app .

# Build without using cache (fresh build)
docker build --no-cache -t my-app .

# Build and show verbose output
docker build --progress=plain -t my-app .

# Build for a specific platform
docker build --platform linux/amd64 -t my-app .

# Build with a specific target stage (multi-stage builds)
docker build --target production -t my-app .

docker image push - Push Image to Registry

Uploads an image to Docker Hub or another registry.

# Push to Docker Hub (must be tagged with your username)
docker push irfan/my-app:1.0

# Push all tags of an image
docker push --all-tags irfan/my-app

# Push to a private registry
docker push registry.example.com/my-app:1.0

> You must run docker login before pushing.

Image Commands Quick Reference

CommandWhat it does
docker pull <image>Download image from registry
docker imagesList all local images
docker image lsSame as above (new syntax)
docker image inspect <image>Show detailed info
docker image history <image>Show layers and build steps
docker image tag <src> <dest>Tag/rename an image
docker rmi <image>Delete an image
docker image rm <image>Same as above (new syntax)
docker image pruneRemove unused images
docker image prune -aRemove all unused images
docker search <term>Search Docker Hub
docker save -o file.tar <image>Export image to file
docker load -i file.tarImport image from file
docker build -t <name> .Build from Dockerfile
docker push <image>Upload image to registry

Practical Examples

Example 1 - Pull, inspect, and run nginx

# Pull the image
docker pull nginx:1.25

# Inspect it
docker image inspect nginx:1.25

# See its layers
docker image history nginx:1.25

# Run a container from it
docker run -d -p 8080:80 nginx:1.25

# Stop container
docker stop $(docker ps -q)

# Remove the image
docker rmi nginx:1.25

Example 2 - Save and transfer an image

# On Machine A: save the image
docker save -o my-app.tar my-app:1.0

# Transfer the file to Machine B (via USB, SCP, etc.)

# On Machine B: load the image
docker load -i my-app.tar

# Verify it loaded
docker images

FAQ

Should I memorize every Docker command?+

No. Memorize the core workflow first: build, run, list, inspect, logs, exec, stop, remove, and clean up. Then learn specialized commands when you need them.

Is Docker only for developers?+

No. Docker is useful for system administrators, infrastructure engineers, DevOps engineers, cloud engineers, support engineers, and learners who want repeatable labs.

What should I do after reading this guide?+

Run the examples, write down what each command changes, rebuild the workflow with Docker Compose, and then add one CI/CD step that builds the image automatically.

Need help applying Docker in a real project?

Work directly with Muhammad Irfan Aslam for Docker, Linux, DevOps, CI/CD, cloud deployment, or infrastructure troubleshooting support.

Hire Me for Support