Managing Images
Effective image management keeps your Docker environment clean and organized. This lesson covers essential commands for managing your local image cache.
Listing Images
# List all images
docker images
# Alternative syntax
docker image ls
# Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest a6bd71f48f68 2 days ago 187MB
node 18 b3db67c7d3a3 5 days ago 1.1GB
postgres 15 ceccf204404e 1 week ago 379MB
# Show all images including intermediate layers
docker images -a
# Show only image IDs
docker images -q
# Show with digests
docker images --digests
# Format output
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
Filtering Images
# Filter by reference (name pattern)
docker images --filter "reference=node"
docker images --filter "reference=*:alpine"
# Filter dangling images (untagged)
docker images --filter "dangling=true"
# Filter by label
docker images --filter "label=maintainer=myname"
# Filter by time
docker images --filter "before=nginx:latest"
docker images --filter "since=ubuntu:20.04"
# Combine filters
docker images --filter "reference=node" --filter "dangling=false"
Removing Images
# Remove a specific image
docker rmi nginx
# Remove by image ID
docker rmi a6bd71f48f68
# Remove multiple images
docker rmi nginx postgres redis
# Force remove (even if used by containers)
docker rmi -f nginx
# Remove all dangling images
docker image prune
# Remove all unused images
docker image prune -a
# Remove without confirmation
docker image prune -f
# Remove images older than 24 hours
docker image prune -a --filter "until=24h"
Dangling Images
Dangling images are layers that have no tag and are not referenced by any other image:
# List dangling images
docker images -f "dangling=true"
# Remove dangling images
docker image prune
# Dangling images often appear when:
# - Rebuilding with the same tag
# - Failed builds
# - Pulling updated images
Tagging Images
Tags let you create multiple references to the same image:
# Tag an existing image
docker tag nginx:latest myrepo/nginx:v1.0
# Tag by image ID
docker tag a6bd71f48f68 myrepo/nginx:production
# Common tagging patterns
docker tag myapp:latest myapp:1.0.0
docker tag myapp:latest myapp:stable
docker tag myapp:latest registry.example.com/myapp:latest
Tagging Best Practices
# Version tags
docker tag myapp myrepo/myapp:1.0.0
docker tag myapp myrepo/myapp:1.0
docker tag myapp myrepo/myapp:1
# Environment tags
docker tag myapp myrepo/myapp:production
docker tag myapp myrepo/myapp:staging
# Git-based tags
docker tag myapp myrepo/myapp:$(git rev-parse --short HEAD)
Saving and Loading Images
Transfer images without a registry:
# Save image to a tar file
docker save nginx > nginx.tar
docker save -o nginx.tar nginx
# Save multiple images
docker save -o images.tar nginx postgres redis
# Save with compression
docker save nginx | gzip > nginx.tar.gz
# Load image from tar file
docker load < nginx.tar
docker load -i nginx.tar
# Load compressed image
gunzip -c nginx.tar.gz | docker load
Import and Export
Different from save/load - works with container filesystems:
# Export container filesystem to tar
docker export mycontainer > container.tar
# Import as a new image
docker import container.tar myimage:imported
# Import with commit message
docker import --message "Imported from container" container.tar myimage:v1
Save vs Export
| Operation | Works On | Includes |
|---|---|---|
| save/load | Images | All layers, metadata, history |
| export/import | Containers | Single flattened filesystem |
Image History
View the layers and commands that created an image:
# Show image history
docker history nginx
# Output:
IMAGE CREATED CREATED BY SIZE
a6bd71f48f68 2 days ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daem… 0B
<missing> 2 days ago /bin/sh -c #(nop) STOPSIGNAL SIGQUIT 0B
<missing> 2 days ago /bin/sh -c #(nop) EXPOSE 80 0B
# Show full commands (not truncated)
docker history --no-trunc nginx
# Format output
docker history --format "{{.ID}}: {{.Size}}" nginx
Disk Usage
Monitor space used by Docker:
# Summary of disk usage
docker system df
# Output:
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 15 5 5.23GB 3.1GB (59%)
Containers 10 2 200MB 150MB (75%)
Local Volumes 8 4 1.5GB 800MB (53%)
Build Cache 50 0 2GB 2GB (100%)
# Detailed breakdown
docker system df -v
# Just images
docker images --format "{{.Repository}}:{{.Tag}} - {{.Size}}"
Cleaning Up
Targeted Cleanup
# Remove specific images
docker rmi image1 image2 image3
# Remove images by pattern
docker images | grep "none" | awk '{print $3}' | xargs docker rmi
# Remove images older than 7 days
docker image prune -a --filter "until=168h"
Full Cleanup
# Remove all unused images (not just dangling)
docker image prune -a
# Nuclear option: Remove everything
docker system prune -a
# This removes:
# - All stopped containers
# - All unused networks
# - All unused images
# - All build cache
Inspecting Images
Get detailed information:
# Full JSON output
docker inspect nginx
# Specific fields
docker inspect -f '{{.Config.Env}}' nginx
docker inspect -f '{{.Config.Cmd}}' nginx
docker inspect -f '{{.Config.ExposedPorts}}' nginx
docker inspect -f '{{.RootFS.Layers}}' nginx
# Size information
docker inspect -f '{{.Size}}' nginx
docker inspect -f '{{.VirtualSize}}' nginx
Image Layer Analysis
Using docker history
docker history --no-trunc myimage | head -20
Using dive (third-party tool)
# Install dive
brew install dive # macOS
# Analyze image
dive nginx
Copying Images Between Environments
Using a Registry
# Pull from source
docker pull source-registry/myimage:tag
# Tag for destination
docker tag source-registry/myimage:tag dest-registry/myimage:tag
# Push to destination
docker push dest-registry/myimage:tag
Using Files
# On source machine
docker save myimage:tag | gzip > myimage.tar.gz
scp myimage.tar.gz user@destination:/path/
# On destination machine
gunzip -c myimage.tar.gz | docker load
Key Takeaways
- Use
docker imagesto list and filter local images - Remove unused images with
docker image prune - Tag images for versioning and registry destinations
- Use
docker save/loadto transfer images as files - Monitor disk usage with
docker system df - Clean up regularly to manage disk space
- Use
docker inspectfor detailed image information - Dangling images waste space - prune them regularly
التالي · الدرس 10 من 32
Dockerfile Basics
قراءة 4 دقيقة
أنهيت 9 من 32 درسًا

اسأل Byte عن هذا الدرس
محادثة خاصة حول هذا الدرس. قرأ Byte الدرس كاملًا، ويجيب خلال ثوانٍ، ويُظهر القسم الذي تأتي منه كل إجابة. يمكنك السؤال بأي لغة.
الأسئلة والأجوبة
أسئلة عامة حول هذا الدرس. يجيب AI Tutor فورًا بلغتك، ويراجع فريق FreeAcademy الإجابات، ويمكن لكل المتعلمين قراءتها.

