Docker and Kubernetes Cheat Sheet
Running and managing containers, images, Dockerfiles, multi-stage builds, volumes, networks, Compose, cleaning up, debugging and kubectl.
Updated
Run containers#
docker run -d --name web -p 8080:80 nginx:1.30-alpine
docker run -it --rm alpine:3.24 sh # interactive, delete on exit
docker run -e KEY=value image # environment variable
docker run --env-file .env image
docker run -v data:/var/lib/data image # volume
docker run -v "$PWD":/app image # bind mount
docker run --network app-net image
docker run --restart unless-stopped image
docker run --memory=256m --cpus=1 imageManage containers#
docker ps # running
docker ps -a # all
docker logs -f web # follow logs
docker exec -it web sh # shell inside
docker stop web # SIGTERM, then SIGKILL
docker start web
docker restart web
docker rm -f web # stop and remove
docker cp file web:/path # copy a file
docker inspect web
docker stats # live resource useImages#
docker images
docker pull postgres:17-alpine
docker build -t app:1.0 .
docker build --target test -t app:test .
docker tag app:1.0 you/app:1.0
docker push you/app:1.0
docker rmi app:1.0
docker history app:1.0Dockerfile#
FROM python:3.13-slim
ENV PYTHONUNBUFFERED=1
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
USER nobody
EXPOSE 8000
HEALTHCHECK CMD curl -f http://localhost:8000/health
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "app:app"]Multi-stage build#
FROM node:24-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:1.30-alpine
COPY --from=build /app/dist /usr/share/nginx/htmlVolumes and networks#
docker volume ls
docker volume create data
docker volume rm data
docker network ls
docker network create app-net
docker network inspect app-netCompose#
services:
api:
build: .
ports:
- "8000:8000"
environment:
DATABASE_URL: postgres://app:pass@db:5432/app
depends_on:
db:
condition: service_healthy
db:
image: postgres:17-alpine
environment:
POSTGRES_PASSWORD: pass
volumes:
- db-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 5s
volumes:
db-data:docker compose up -d
docker compose up -d --build
docker compose ps
docker compose logs -f api
docker compose exec api sh
docker compose run --rm api npm test
docker compose down
docker compose down -v # also delete volumesClean up#
docker container prune # stopped containers
docker image prune -a # unused images
docker volume prune # unused volumes: careful
docker system df # disk use
docker system prune # most of the aboveDebugging#
docker ps -a
docker logs name
docker inspect --format '{{.State.ExitCode}}' name
docker run -it --rm --entrypoint sh imageKubernetes#
kubectl get pods,deploy,svc
kubectl apply -f app.yaml
kubectl delete -f app.yaml
kubectl describe pod name
kubectl logs -f name
kubectl logs --previous name
kubectl exec -it name -- sh
kubectl scale deployment web --replicas=3
kubectl set image deployment/web c=app:2.0
kubectl rollout status deployment/web
kubectl rollout undo deployment/web
kubectl port-forward service/web 8080:80
kubectl get events --sort-by=.lastTimestampWant the full explanations? Go to the Learn Docker and Kubernetes course →