Skip to content

Docker Images

Docker images are read-only templates used to create containers. This page covers all essential commands for managing Docker images.

Quick Reference

TaskCommand
Pull imagedocker pull <image>:<tag>
List imagesdocker image list --all
Remove imagedocker image rm <image>
Remove unuseddocker image prune --all
Build imagedocker build -t <name>:<tag> .
Tag imagedocker tag <image> <new_name>:<tag>
Push imagedocker push <image>:<tag>
Inspect imagedocker 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.0

Common 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.0

Listing 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 ubuntu

Formatted 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.09GB

Removing 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.12

Remove 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 --force

Remove 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 rm

Building 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.5

Pushing 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.0

Inspecting 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.04

Useful 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:latest

Saving 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.gz

Transfer 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 df

Automated 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 df

Best Practices

Image Naming

  1. Use descriptive names: mycompany/projectname:version
  2. Tag with semantic versions: 1.0.0, 1.0, latest
  3. Use environment tags: dev, staging, prod

Image Optimization

  1. Use smaller base images: Alpine Linux images are typically smaller
  2. Multi-stage builds: Separate build and runtime dependencies
  3. .dockerignore: Exclude unnecessary files
  4. Minimize layers: Combine RUN commands

Security

  1. Use official images: From verified publishers
  2. Scan for vulnerabilities: docker scan <image>
  3. Don't store secrets: Use environment variables or secrets management
  4. 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.04

Image 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 ubuntu

Out 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 --volumes

See Also

Released under the MIT License.