Docker and kubectl Cheat Sheet: Containers, Images, Compose, and Kubernetes
Introduction
Docker packages applications into containers, and kubectl manages them in Kubernetes. This cheat sheet lists the commands you reach for daily. Use it with the Docker and Kubernetes lessons and the CKA/CKAD certification guide on this site.
Prerequisites
- Docker installed
- kubectl and a cluster (kind or minikube) for the Kubernetes part
Step-by-Step Guide
Step 1: Run and manage containers
docker run creates and starts a container. Use -d to run in the background and -p to publish a port.
docker run --rm hello-world
docker run -d --name web -p 8080:80 nginx
docker ps # running containers
docker ps -a # all containers
docker logs -f web
docker exec -it web sh # shell inside the container
docker stop web && docker rm web
docker run --rm -v "$(pwd)":/app -w /app node:20 npm testStep 2: Images
Build images from a Dockerfile and tag them. Remove unused images to save disk space.
docker build -t myapp:1.0 .
docker images
docker pull python:3.12-slim
docker tag myapp:1.0 myuser/myapp:1.0
docker push myuser/myapp:1.0
docker rmi myapp:1.0
docker system prune # remove unused data (asks first)Step 3: A Dockerfile
Copy dependency files first so Docker can cache the install step. Run as a non-root user where you can.
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
ENV NODE_ENV=production
EXPOSE 3000
USER node
CMD ["node", "server.js"]Step 4: Docker Compose
Compose runs several containers together from one file. Never commit real passwords; read them from an environment file instead.
# compose.yaml
services:
web:
build: .
ports:
- "3000:3000"
env_file: .env
depends_on:
- db
db:
image: mysql:8
env_file: .env
volumes:
- dbdata:/var/lib/mysql
volumes:
dbdata:
docker compose up -d
docker compose ps
docker compose logs -f web
docker compose downStep 5: kubectl: inspect resources
get lists resources, describe shows details and events, and logs prints container output.
kubectl config get-contexts
kubectl config use-context kind-kind
kubectl get nodes
kubectl get pods -A # all namespaces
kubectl get deploy,svc,ingress -n dev
kubectl get pod web-1 -o yaml
kubectl describe pod web-1 # events explain most failures
kubectl logs web-1 -f
kubectl logs web-1 -c sidecar --previous
kubectl get events --sort-by=.metadata.creationTimestampStep 6: kubectl: create and change resources
Apply manifests declaratively. Use dry-run to generate YAML instead of writing it from scratch.
kubectl apply -f deployment.yaml
kubectl create deployment web --image=nginx --replicas=3
kubectl expose deployment web --port=80 --type=ClusterIP
kubectl scale deployment web --replicas=5
kubectl set image deployment/web nginx=nginx:1.27
kubectl rollout status deployment/web
kubectl rollout undo deployment/web
kubectl run tmp --image=nginx --dry-run=client -o yaml > pod.yaml
kubectl delete -f deployment.yamlStep 7: kubectl: debug
Open a shell, forward a port, and check resource usage when something misbehaves.
kubectl exec -it web-1 -- sh
kubectl port-forward svc/web 8080:80
kubectl top pods # needs metrics-server
kubectl get pod web-1 -o jsonpath='{.status.phase}'
kubectl auth can-i create pods
kubectl explain deployment.spec.strategyFAQs
- A pod is stuck in CrashLoopBackOff. Where do I start?
- Run kubectl describe pod to read the events, then kubectl logs with --previous to see why the last run exited. Check the image, command, environment variables, and probes.
Summary
Use docker run, ps, logs, and exec for containers, a cache-friendly Dockerfile and Compose for multi-container apps, and kubectl get, describe, logs, apply, rollout, exec, and port-forward for Kubernetes.
Related tutorials
- Linux Command Line Cheat Sheet: Files, Permissions, Processes, and Networking
- SQL Cheat Sheet: SELECT, Joins, Aggregates, and Data Changes
- Git Cheat Sheet: Everyday Commands, Branching, and Undoing Mistakes
- Langflow Cheat Sheet: Install, Components, API Calls, and Custom Components
- LangGraph Cheat Sheet: StateGraph, Routing, Memory, and Human-in-the-Loop
Comments
Log in or create a free account to join the discussion.