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 ```bash # Images (Artifacts) docker images # List local images docker pull # Pull an image from registry (e.g. Docker Hub) docker build -t : . # Build image from Dockerfile in current directory (.) docker rmi # Delete a local image docker tag # Retag an existing image (for pushing/renaming) docker push # 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 # Run container in background (detached) with port forwarding (host:container) docker run --name # Run a container with a custom name docker stop # Stop a running container gracefully docker start # Start a stopped container docker rm # Delete a container (must be stopped) docker rm -f $(docker ps -aq) # Force delete ALL local containers! # Debugging & Management docker logs # View container logs docker logs -f # Follow and view container logs in real-time docker exec -it sh # Execute interactive terminal inside a running container (sh/bash) docker inspect # 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 # Delete a volume docker network ls # List container networks docker network inspect # 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 ```yaml # 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 ``` ```bash 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 # Follow and view logs of a specific service docker compose exec sh # Open interactive shell inside service container docker compose build # Force rebuild services containing build instructions ``` ## Kubernetes kubectl ```bash # Basic Queries & Context kubectl config get-contexts # List all configured cluster contexts kubectl config use-context # Switch active cluster context kubectl get nodes # List all nodes in cluster kubectl get namespaces # List all namespaces kubectl get pods -n # 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 # View comprehensive low-level event logs and configurations of a pod kubectl logs # View output logs of a pod kubectl logs -f # Follow and view logs of a pod in real-time kubectl exec -it -- 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 # Delete a specific pod (replica controller will re-create it) kubectl scale deploy --replicas=5 # Scale deployment dynamically to 5 replica pods kubectl rollout status deploy # Monitor deployment rollout progress/status kubectl rollout undo deploy # Rollback deployment to the previous version ``` ## Helm (Kubernetes Package Manager) ```bash # Repository Management helm repo add # Add a remote Helm chart repository helm repo update # Fetch and sync the latest charts from all repositories helm search repo # Search configured repositories for charts # Deployments & Releases helm list # List all releases in the active namespace (-A for all namespaces) helm install # Install a chart release on the cluster helm install -f values.yaml # Install chart overriding configurations with custom values file helm upgrade # Upgrade an existing release to a new version or values config helm rollback # Rollback a release to a specific revision number helm uninstall # Uninstall and delete a release from the cluster helm status # View status details and instructions of a release ``` ## Terraform (Infrastructure as Code) ```bash # 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 # Show detailed state metadata of a specific tracked resource terraform refresh # Sync local state file with actual real-world infrastructure ``` ## GitHub Actions ```yaml # .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 ```markdown 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. ```