Docker Images
Docker images are read-only templates used to create containers. This page covers all essential commands for managing Docker images.
Quick Reference
| Task | Command |
|---|---|
| Pull image | docker pull <image>:<tag> |
| List images | docker image list --all |
| Remove image | docker image rm <image> |
| Remove unused | docker image prune --all |
| Build image | docker build -t <name>:<tag> . |
| Tag image | docker tag <image> <new_name>:<tag> |
| Push image | docker push <image>:<tag> |
| Inspect image | docker image inspect <image> |
Pulling Images
Pull from Docker Hub
bash
# Pull latest version
docker pull ubuntu
# Pull specific version
docker pull ubuntu:22.04
# Pull from Microsoft Container Registry
docker pull mcr.microsoft.com/dotnet/core/runtime:3.1
# Pull from specific registry
docker pull myregistry.com:5000/myimage:v1.0Common Base Images
bash
# Official images
docker pull ubuntu:latest
docker pull alpine:latest
docker pull node:20
docker pull python:3.12
docker pull nginx:latest
docker pull redis:latest
# .NET images
docker pull mcr.microsoft.com/dotnet/sdk:8.0
docker pull mcr.microsoft.com/dotnet/aspnet:8.0
docker pull mcr.microsoft.com/dotnet/runtime:8.0Listing Images
Basic Listing
bash
# List all images
docker image list
# List all images (including intermediate)
docker image list --all
# Short form
docker images
# List specific images
docker image list ubuntuFormatted Output
bash
# Custom format
docker image list --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
# Show only image IDs
docker image list --quiet
# Filter by name
docker image list --filter "reference=ubuntu:*"Example Output
REPOSITORY TAG IMAGE ID CREATED SIZE
mcr.microsoft.com/dotnet/core/runtime 3.1 abc123def456 2 weeks ago 180MB
ubuntu 22.04 def456ghi789 3 weeks ago 77.8MB
node 20 ghi789jkl012 1 month ago 1.09GBRemoving Images
Remove Single Image
bash
# By name and tag
docker image rm ubuntu:22.04
# By image ID
docker image rm abc123def456
# Force remove (even if container exists)
docker image rm -f ubuntu:22.04
# Remove multiple images
docker image rm ubuntu:22.04 node:20 python:3.12Remove All Unused Images
bash
# Remove dangling images (not tagged and not referenced)
docker image prune
# Remove all unused images (not just dangling)
docker image prune --all
# Remove without confirmation prompt
docker image prune --all --forceRemove Images by Filter
bash
# Remove images older than 24 hours
docker image prune --all --filter "until=24h"
# Remove images matching pattern
docker image list --filter "reference=temp-*" --quiet | xargs docker image rmBuilding Images
Basic Build
bash
# Build from current directory
docker build -t myapp:latest .
# Build from specific Dockerfile
docker build -f Dockerfile.production -t myapp:prod .
# Build with build arguments
docker build --build-arg VERSION=1.0 -t myapp:1.0 .
# Build without cache
docker build --no-cache -t myapp:latest .Multi-stage Build Example
dockerfile
# Dockerfile
# Stage 1: Build
FROM node:20 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Stage 2: Production
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY package*.json ./
RUN npm ci --production
CMD ["node", "dist/index.js"]bash
# Build multi-stage image
docker build -t myapp:latest .Tagging Images
Create Tags
bash
# Tag existing image
docker tag ubuntu:22.04 myubuntu:latest
# Tag for registry
docker tag myapp:latest myregistry.com:5000/myapp:v1.0
# Tag with multiple versions
docker tag myapp:latest myapp:1.0
docker tag myapp:latest myapp:1.0.5Pushing Images
Push to Registry
bash
# Login to Docker Hub
docker login
# Push to Docker Hub
docker push username/myapp:latest
# Login to private registry
docker login myregistry.com:5000
# Push to private registry
docker push myregistry.com:5000/myapp:v1.0Inspecting Images
View Image Details
bash
# Full inspection (JSON format)
docker image inspect ubuntu:22.04
# View specific fields
docker image inspect --format='{{.Architecture}}' ubuntu:22.04
# View image layers
docker image history ubuntu:22.04
# View image size
docker image inspect --format='{{.Size}}' ubuntu:22.04Useful Inspection Queries
bash
# Get creation date
docker image inspect --format='{{.Created}}' myapp:latest
# Get environment variables
docker image inspect --format='{{.Config.Env}}' myapp:latest
# Get exposed ports
docker image inspect --format='{{.Config.ExposedPorts}}' myapp:latest
# Get entrypoint and command
docker image inspect --format='{{.Config.Entrypoint}} {{.Config.Cmd}}' myapp:latestSaving and Loading Images
Export and Import
bash
# Save image to tar file
docker save -o myapp.tar myapp:latest
# Save multiple images
docker save -o images.tar myapp:latest ubuntu:22.04
# Load image from tar file
docker load -i myapp.tar
# Export from stdin/stdout
docker save myapp:latest | gzip > myapp.tar.gz
docker load < myapp.tar.gzTransfer Between Hosts
bash
# Save and compress
docker save myapp:latest | gzip > myapp.tar.gz
# Transfer (example with scp)
scp myapp.tar.gz user@remotehost:/tmp/
# Load on remote host
ssh user@remotehost "docker load < /tmp/myapp.tar.gz"Image Cleanup Strategies
Manual Cleanup
bash
# Remove all stopped containers first
docker container prune
# Then remove unused images
docker image prune --all
# Check space saved
docker system dfAutomated Cleanup Script
bash
#!/bin/bash
# cleanup-docker.sh
echo "Cleaning up Docker images..."
# Remove exited containers
docker container prune -f
# Remove dangling images
docker image prune -f
# Remove images older than 30 days
docker image prune --all --filter "until=720h" -f
echo "Cleanup complete!"
docker system dfBest Practices
Image Naming
- Use descriptive names:
mycompany/projectname:version - Tag with semantic versions:
1.0.0,1.0,latest - Use environment tags:
dev,staging,prod
Image Optimization
- Use smaller base images: Alpine Linux images are typically smaller
- Multi-stage builds: Separate build and runtime dependencies
- .dockerignore: Exclude unnecessary files
- Minimize layers: Combine RUN commands
Security
- Use official images: From verified publishers
- Scan for vulnerabilities:
docker scan <image> - Don't store secrets: Use environment variables or secrets management
- Update regularly: Keep base images current
Troubleshooting
Image Pull Failures
bash
# Check network connectivity
ping docker.io
# Login if private image
docker login
# Try with full registry path
docker pull docker.io/library/ubuntu:22.04Image Not Found
bash
# List all images including intermediates
docker image list --all
# Check image name and tag
docker image list ubuntu
# Search Docker Hub
docker search ubuntuOut of Disk Space
bash
# Check disk usage
docker system df
# Detailed breakdown
docker system df -v
# Clean up
docker image prune --all
docker system prune --all --volumesSee Also
- Docker Containers - Container management
- Docker Compose - Multi-container apps
- Docker Overview - Docker introduction
- .NET Core Publishing - Build .NET Docker images