DevOpsil
Docker
83%
Fresh

Docker CLI Cheat Sheet

Sarah ChenSarah Chen3 min read

Images

# Build
docker build -t myapp:latest .
docker build -t myapp:v1.0 -f Dockerfile.prod .
docker build --no-cache -t myapp:latest .

# List & manage
docker images
docker image ls --filter "dangling=true"
docker image prune -a                  # Remove unused images
docker rmi myapp:latest

# Pull & push
docker pull nginx:1.25-alpine
docker tag myapp:latest registry.com/myapp:v1.0
docker push registry.com/myapp:v1.0

Containers

# Run
docker run -d --name myapp -p 8080:80 myapp:latest
docker run -it --rm ubuntu:22.04 bash  # Interactive, auto-remove
docker run -d -v ./data:/app/data myapp:latest
docker run -d --env-file .env myapp:latest
docker run -d --restart=unless-stopped myapp:latest

# Manage
docker ps                              # Running
docker ps -a                           # All (including stopped)
docker stop myapp
docker start myapp
docker restart myapp
docker rm myapp
docker rm $(docker ps -aq)             # Remove all stopped

# Interact
docker exec -it myapp bash
docker logs myapp
docker logs -f --tail 100 myapp        # Follow last 100 lines
docker cp myapp:/app/config.json ./config.json

Volumes

docker volume create mydata
docker volume ls
docker volume inspect mydata
docker volume rm mydata
docker volume prune                    # Remove unused

Networks

docker network create mynet
docker network ls
docker network connect mynet myapp
docker network disconnect mynet myapp
docker network inspect mynet

Docker Compose

docker compose up -d
docker compose down
docker compose logs -f
docker compose ps
docker compose exec app bash
docker compose build --no-cache
docker compose pull
docker compose restart app

Cleanup

# Nuclear option — reclaim ALL space
docker system prune -a --volumes

# Selective cleanup
docker container prune               # Stopped containers
docker image prune -a                # Unused images
docker volume prune                  # Unused volumes
docker builder prune                 # Build cache

# Check disk usage
docker system df

Debugging

# Inspect container details
docker inspect myapp
docker inspect --format='{{.State.Health.Status}}' myapp
docker inspect --format='{{.NetworkSettings.IPAddress}}' myapp

# Resource usage
docker stats
docker stats myapp --no-stream

# History of an image
docker history myapp:latest

# View running processes inside a container
docker top myapp

# Show port mappings
docker port myapp

# Stream container events in real time
docker events --filter container=myapp

# Export container filesystem as tar archive
docker export myapp > myapp-fs.tar

# Show changes to files in container
docker diff myapp

Multi-Platform Builds

# Create a buildx builder
docker buildx create --name multiarch --use

# Build for multiple architectures
docker buildx build --platform linux/amd64,linux/arm64 -t myapp:latest --push .

# Inspect build cache
docker buildx du

# List available builders
docker buildx ls

Registry & Authentication

# Log in to a registry
docker login registry.com
docker login ghcr.io -u USERNAME --password-stdin

# Log out
docker logout registry.com

# Search Docker Hub
docker search nginx --limit 5

# Save and load images as tar files
docker save myapp:latest -o myapp.tar
docker load -i myapp.tar

Resource Constraints

# Run with memory and CPU limits
docker run -d --memory=512m --cpus=1.5 myapp:latest

# Update limits on a running container
docker update --memory=1g --cpus=2 myapp

# Run with a specific restart policy
docker run -d --restart=on-failure:5 myapp:latest
Share:
Sarah Chen
Sarah Chen

CI/CD Engineering Lead

Automation evangelist who believes no deployment should require a human. I write pipelines, break pipelines, and write about both. Code-first, always.

Related Articles

AWSQuick RefFresh

AWS CLI: Cheat Sheet

AWS CLI cheat sheet with copy-paste commands for EC2, S3, IAM, Lambda, ECS, CloudFormation, SSM, and Secrets Manager operations.

Dev Patel·
3 min read
TerraformQuick RefFresh

Terraform CLI: Cheat Sheet

Terraform CLI cheat sheet with commands organized by workflow — init, plan, apply, destroy, state manipulation, imports, and workspace management.

Zara Blackwood·
3 min read