Container & Orchestration Quick Reference

Docker & Kubernetes Commands Cheat Sheet

Docker packages your app and its dependencies into a portable container. Kubernetes (kubectl) runs, scales, and heals those containers across a cluster of machines.

โฑ๏ธ 10 min reference๐ŸŸข Beginner Friendly๐Ÿ“… Updated: 2026

Anatomy of a Docker Command

docker run -d -p 8080:80 --name web nginx:latest
CLI Tool docker Talks to the Docker Engine
Subcommand run Create & start a container
Flags / Options -d -p 8080:80 --name web Background, port map, name
Image:Tag nginx:latest Blueprint to launch from
Container Pipeline: From Code to Running App Dockerfile โ†’ Image โ†’ Container
1. DOCKERFILE Recipe / Instructions FROM node:22-alpine COPY . . / CMD [...] build 2. DOCKER IMAGE Read-Only Template myapp:1.0 (Layers) Stored in Registry run 3. CONTAINER(S) Live Isolated Process Own FS + Network + PID Managed by Docker / K8s
Memory Trick: Think of a Dockerfile as a cake recipe, an Image as the frozen cake mold, and a Container as the actual cake you bake and serve. One image can spawn many containers!

1 Docker Container Lifecycle

Daily commands to start, monitor, debug, stop, and remove running containers.

docker run -d -p 8080:80 --name web nginx
Run a Container in Background Starts nginx detached (-d), maps host port 8080 to container port 80, and names it web.
docker ps -a
List All Containers Shows running and stopped containers (docker ps alone shows only running).
docker logs -f web
Stream Live Logs Follows (-f) stdout/stderr output from container web. Press Ctrl+C to exit.
docker exec -it web sh
Open Interactive Shell Inside Attaches an interactive terminal (-it) into a running container to debug files or processes.
docker stop web
Gracefully Stop a Container Sends SIGTERM so the app can finish requests and shut down cleanly.
docker rm web
Remove a Stopped Container Deletes the container. Add -f (docker rm -f web) to force remove a running one.
docker inspect web
View Full Low-Level JSON Details Reveals IP address, mounted volumes, environment variables, and restart policy.

2 Docker Images, Volumes & Compose

Build custom images, inspect persistent storage and networks, and launch multi-container stacks.

docker build -t myapp:1.0 .
Build Image from Dockerfile Tags (-t) the image as myapp:1.0 using the Dockerfile in the current folder (.).
docker images
List Local Images Shows repository, tag, image ID, and disk size of images stored on your machine.
docker pull postgres:17-alpine
Download Image from Registry Fetches an image from Docker Hub without starting a container yet.
docker volume ls
List Persistent Volumes Volumes keep database data safe even after containers are deleted.
docker network ls
List Docker Networks Shows bridge, host, and custom networks used for container-to-container DNS.
docker compose up -d
Start Multi-Container Stack Reads compose.yaml (or docker-compose.yml) and starts all services in the background.
docker compose down
Stop & Remove Compose Stack Stops and removes containers and networks created by Compose (keeps named volumes safe unless -v is added).

3 Dockerfile Instructions Quick Reference

A Dockerfile is a plain-text script that tells Docker how to assemble an image layer by layer.

Instruction What It Does Example
FROM Sets the base OS/runtime image. Always the first instruction. FROM node:22-alpine
WORKDIR Sets the working directory inside the image (creates it if missing). WORKDIR /app
COPY Copies files/folders from your host into the image. Prefer over ADD. COPY package*.json ./
RUN Executes a command at build time (installs packages, builds code). RUN npm ci --omit=dev
ENV Sets persistent environment variables available at build and runtime. ENV NODE_ENV=production
EXPOSE Documents which port the app listens on (does not publish itโ€”use -p). EXPOSE 3000
CMD Default command/args run when the container starts. Easily overridden on docker run. CMD ["node", "server.js"]
ENTRYPOINT Fixed executable that always runs; extra docker run args append to it. ENTRYPOINT ["python3", "app.py"]
CMD vs ENTRYPOINT: Use ENTRYPOINT for the fixed executable (e.g., ["nginx"]) and CMD for default flags that a user might override (e.g., ["-g", "daemon off;"]).
Dockerfile (Minimal Node.js Example)
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
ENV NODE_ENV=production
EXPOSE 3000
USER node
CMD ["node", "server.js"]

4 Kubernetes (kubectl) Essentials

Use kubectl to inspect cluster state, troubleshoot failing Pods, and apply declarative YAML manifests.

kubectl Pattern: Almost every command follows kubectl <verb> <resource> <name> โ€” for example, kubectl get pods or kubectl describe pod api-1.
kubectl get pods -A
List Pods Across All Namespaces Shows Pod status (Running, CrashLoopBackOff, Pending). Omit -A for current namespace only.
kubectl get svc,nodes
List Services & Cluster Nodes Checks exposed Service ports/ClusterIPs and whether worker nodes are Ready.
kubectl describe pod <pod>
Inspect Pod Events & Failure Reasons Your #1 debugging tool for ImagePullBackOff or scheduling errorsโ€”scroll to the bottom Events section.
kubectl logs -f <pod>
Stream Pod Application Logs Add -c <container> if the Pod runs multiple containers, or --previous for a crashed container.
kubectl exec -it <pod> -- sh
Shell Into a Running Pod Note the double dash -- separating kubectl flags from the container command.
kubectl apply -f app.yaml
Create or Update Resources from YAML Declaratively applies Deployments, Services, or ConfigMaps. Safe to re-run after edits.
kubectl delete -f app.yaml
Delete Resources Defined in YAML Cleanly tears down everything declared in app.yaml.
kubectl get events --sort-by='.metadata.creationTimestamp'
View Chronological Cluster Events Spot OOMKills, failed health probes, and scheduling warnings across the namespace.

5 Danger Zone & Beginner Traps

โš ๏ธ Watch Out Before You Hit Enter
1. docker system prune -af --volumes Wipes Unused Volumes

Adding --volumes permanently deletes every Docker volume not currently attached to a running container. If your database container is stopped, your data is gone forever with no undo.

2. Running Containers as root

By default, processes inside many base images run as UID 0 (root). Always add a non-root user (e.g., USER node or USER 1001) in your Dockerfile to limit blast radius.

3. Forgetting -p Port Mapping

Running docker run nginx without -p 8080:80 starts Nginx inside an isolated container network. EXPOSE 80 in a Dockerfile is documentation onlyโ€”you must pass -p host:container to reach it from localhost.

4. Deleting a Kubernetes Namespace

kubectl delete namespace <ns> deletes every Pod, Service, Secret, and PersistentVolumeClaim inside that namespace immediately.