Skip to the content.

Docker & DevOps Cheatsheet

A comprehensive reference for Docker containers, Docker Compose multi-container services, Kubernetes cluster administration, Helm chart management, Terraform IaC, GitHub Actions CI/CD workflows, and core DevOps pipeline principles.

Docker Commands

# Images (Artifacts)
docker images                 # List local images
docker pull <image>            # Pull an image from registry (e.g. Docker Hub)
docker build -t <name>:<tag> . # Build image from Dockerfile in current directory (.)
docker rmi <image>            # Delete a local image
docker tag <image> <new_tag>  # Retag an existing image (for pushing/renaming)
docker push <image>           # Push an image to a remote registry

# Containers (Runtimes)
docker ps                     # List currently running containers
docker ps -a                  # List all containers (running, stopped, crashed)
docker run -d -p 80:80 <img > # Run container in background (detached) with port forwarding (host:container)
docker run --name <n> <img >  # Run a container with a custom name
docker stop <id/name>         # Stop a running container gracefully
docker start <id/name>        # Start a stopped container
docker rm <id/name>           # Delete a container (must be stopped)
docker rm -f $(docker ps -aq) # Force delete ALL local containers!

# Debugging & Management
docker logs <id/name>         # View container logs
docker logs -f <id/name>      # Follow and view container logs in real-time
docker exec -it <id/name> sh  # Execute interactive terminal inside a running container (sh/bash)
docker inspect <id/name>      # View detailed low-level JSON metadata of a container or image
docker stats                  # View real-time resource utilization (CPU, memory, net, disk)

# Volumes & Networks
docker volume ls              # List local volumes
docker volume rm <name>       # Delete a volume
docker network ls             # List container networks
docker network inspect <net>  # Inspect specific network topology

# Cleanup
docker system prune           # Delete all unused containers, networks, and dangling images
docker system prune -a --volumes # Deep clean: delete all stopped containers, unused networks, and ALL unused images/volumes!

Docker Compose

# docker-compose.yml Syntax Basics
version: "3.8"
services:
  web:
    build: .                  # Build from local Dockerfile
    ports:
      - "80:80"
    environment:
      - NODE_ENV=production
    volumes:
      - db-data:/data
volumes:
  db-data:                    # Named persistent volume
docker compose up             # Build, create, and start all services in foreground
docker compose up -d          # Build, create, and start all services in background (detached)
docker compose down           # Stop and delete containers and networks created by up
docker compose down -v        # Stop services and DESTROY all associated persistent volumes!
docker compose ps             # View status of services managed by Compose file
docker compose logs -f <svc>  # Follow and view logs of a specific service
docker compose exec <svc> sh  # Open interactive shell inside service container
docker compose build          # Force rebuild services containing build instructions

Kubernetes kubectl

# Basic Queries & Context
kubectl config get-contexts   # List all configured cluster contexts
kubectl config use-context <c># Switch active cluster context
kubectl get nodes             # List all nodes in cluster
kubectl get namespaces        # List all namespaces
kubectl get pods -n <ns>      # List pods in specific namespace (-A for all namespaces)
kubectl get services          # List cluster services (with ports/IPs)
kubectl get deployments       # List deployments and replica sets

# Debugging Pods
kubectl describe pod <name>   # View comprehensive low-level event logs and configurations of a pod
kubectl logs <pod-name>       # View output logs of a pod
kubectl logs -f <pod-name>    # Follow and view logs of a pod in real-time
kubectl exec -it <pod> -- sh  # Open interactive shell inside a running pod container

# Applying & Changing Resources
kubectl apply -f manifest.yaml# Apply or update resources declared in a YAML configuration file
kubectl delete -f manifest.yml# Delete resources defined in a YAML configuration file
kubectl delete pod <pod-name> # Delete a specific pod (replica controller will re-create it)
kubectl scale deploy <d> --replicas=5 # Scale deployment dynamically to 5 replica pods
kubectl rollout status deploy <d> # Monitor deployment rollout progress/status
kubectl rollout undo deploy <d> # Rollback deployment to the previous version

Helm (Kubernetes Package Manager)

# Repository Management
helm repo add <name> <url>    # Add a remote Helm chart repository
helm repo update              # Fetch and sync the latest charts from all repositories
helm search repo <keyword>    # Search configured repositories for charts

# Deployments & Releases
helm list                     # List all releases in the active namespace (-A for all namespaces)
helm install <name> <chart>   # Install a chart release on the cluster
helm install <name> <chart> -f values.yaml # Install chart overriding configurations with custom values file
helm upgrade <name> <chart>   # Upgrade an existing release to a new version or values config
helm rollback <name> <rev>    # Rollback a release to a specific revision number
helm uninstall <name>         # Uninstall and delete a release from the cluster
helm status <name>            # View status details and instructions of a release

Terraform (Infrastructure as Code)

# Configuration Lifecycle
terraform init                # Initialize directory: download providers and backend modules
terraform validate            # Validate syntax and configuration integrity of TF files
terraform fmt                 # Automatically format all TF files according to canonical style
terraform plan                # Generate and review execution plan: view what will be created/changed/deleted
terraform plan -out=plan.tfplan # Save execution plan safely to a file
terraform apply               # Execute plan and provision real infrastructure resources
terraform apply plan.tfplan   # Apply a pre-generated saved execution plan file
terraform destroy             # Tear down and destroy all infrastructure provisioned by this folder

# State Management
terraform state list          # List all tracked resources in active Terraform state
terraform state show <res>    # Show detailed state metadata of a specific tracked resource
terraform refresh             # Sync local state file with actual real-world infrastructure

GitHub Actions

# .github/workflows/ci.yml Syntax Basics
name: CI Pipeline
on:
  push:
    branches: [ main ]        # Trigger on pushes to main branch
  pull_request:
    branches: [ main ]        # Trigger on PRs targeting main branch

jobs:
  build-and-test:
    runs-on: ubuntu-latest    # Hosted runner environment
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 18

      - name: Install & Test
        run: |
          npm install
          npm run test
        env:
          DATABASE_URL: $ # Decrypt repository secret variables

CI/CD Basics

Continuous Integration (CI)
- The practice of frequently merging code changes into a shared central repository.
- Core Goal: Detect bugs early and ensure codebase stability.
- Core Steps: Automated Checkout -> Dependency Install -> Linter -> Automated Unit Tests -> Build Compilation.

Continuous Delivery (CD)
- Code is automatically tested, built, and staged for release.
- Deployment to production requires a manual approval trigger.

Continuous Deployment (CD)
- Code changes pass through the entire pipeline and are automatically deployed to production with zero manual gates.

Key Indicators & Practices
- Fast feedback loops (pipelines should complete under 10 minutes).
- Build artifacts once (use the same container image/archive across Test, Staging, and Production environments).
- Blue-Green Deployments: Deploying new version to an identical parallel environment (Green) and flipping traffic instantly to avoid downtime.
- Canary Deployments: Incrementally rolling out updates to a small fraction of users first to mitigate risk.