What Are Docker Images?
A Docker image is a read-only template that contains the application code, runtime, libraries, environment variables, and configuration files needed to run a container. Images are built in layers, with each layer representing a set of filesystem changes.
Image Layers
Each instruction in a Dockerfile creates a new layer. Layers are cached and reused, making builds faster and images more efficient.
Working with Images
Pulling Images
# Pull an image from Docker Hub
docker pull nginx
docker pull node:20-alpine
docker pull postgres:16
# Pull a specific version
docker pull nginx:1.25
# Pull from a different registry
docker pull ghcr.io/owner/image:tag
Listing Images
# List all local images
docker images
# Filter images
docker images --filter "reference=node*"
# Show image sizes
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
Inspecting Images
# Get detailed image information
docker inspect nginx
# See image history (layers)
docker history nginx
# Show image digest
docker images --digests
Image Tags
Tags are labels that point to specific image versions. The latest tag is the default
but using specific version tags in production is a best practice.
# Common tag patterns
node:20 # Major version
node:20.11 # Minor version
node:20-alpine # Alpine Linux variant (smaller)
node:20-slim # Slim variant
node:latest # Latest version (avoid in production)
# Tag an existing image
docker tag myapp:latest myapp:v1.0.0
# Tag for pushing to a registry
docker tag myapp:latest username/myapp:v1.0.0
Managing Images
# Remove an image
docker rmi nginx
# Remove all unused images
docker image prune
# Remove ALL images (careful!)
docker image prune -a
# Save an image to a tar archive
docker save -o myapp.tar myapp:latest
# Load an image from a tar archive
docker load -i myapp.tar
Docker Hub
Docker Hub is the default public registry for Docker images. It hosts
official images (like nginx, node, postgres) and
community-created images.
# Login to Docker Hub
docker login
# Search for images
docker search nginx
# Push an image to Docker Hub
docker push username/myapp:v1.0.0
# Logout
docker logout
Best Practices
- 🏷️ Always use specific version tags in production (never
latest) - 📦 Use Alpine-based images for smaller image sizes
- 🧹 Regularly clean up unused images with
docker image prune - 🔒 Only pull images from trusted sources and official repositories