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.
Anatomy of a Docker Command
1 Docker Container Lifecycle
Daily commands to start, monitor, debug, stop, and remove running containers.
docker run -d -p 8080:80 --name web nginxnginx detached (-d), maps host port 8080 to container
port 80, and names it web.
docker ps -adocker ps alone shows only
running).
docker logs -f web-f) stdout/stderr output from container web. Press
Ctrl+C to exit.
docker exec -it web sh-it) into a running container to
debug files or processes.
docker stop webSIGTERM so the app can finish requests and shut down cleanly.
docker rm web-f (docker rm -f web) to force
remove a running one.
docker inspect web2 Docker Images, Volumes & Compose
Build custom images, inspect persistent storage and networks, and launch multi-container stacks.
docker build -t myapp:1.0 .-t) the image as myapp:1.0 using the
Dockerfile in the current folder (.).
docker imagesdocker pull postgres:17-alpinedocker volume lsdocker network lsdocker compose up -dcompose.yaml (or docker-compose.yml) and starts all
services in the background.
docker compose down-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;"]).
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 -ARunning, CrashLoopBackOff,
Pending). Omit -A for current namespace only.
kubectl get svc,nodesReady.
kubectl describe pod <pod>ImagePullBackOff or scheduling errorsโscroll
to the bottom Events section.
kubectl logs -f <pod>-c <container> if the Pod runs multiple containers, or
--previous for a crashed container.
kubectl exec -it <pod> -- sh-- separating kubectl flags from the
container command.
kubectl apply -f app.yamlkubectl delete -f app.yamlapp.yaml.
kubectl get events --sort-by='.metadata.creationTimestamp'
5 Danger Zone & Beginner Traps
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.
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.
-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.
kubectl delete namespace <ns> deletes every Pod, Service,
Secret, and PersistentVolumeClaim inside that namespace immediately.