This is a complete, hands-on Docker cheat sheet: install, configure, operate, automate and secure it - with copy-paste commands grouped by task. Bookmark it for day-to-day work, incidents and interviews alike.
Docker builds, ships and runs applications inside containers - isolated processes that bundle the code, runtime and libraries so 'works on my machine' stops being a problem.
Why you reach for it
- Reproducible builds: the same image runs identically on a laptop, CI and production
- Fast, isolated environments without the overhead of full virtual machines
- A universal packaging format that every orchestrator and cloud understands
Installation
# Install Docker Engine on Ubuntu
curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker "$USER" # run docker without sudo (re-login after)
docker versionConfiguration reference
A small, cache-friendly multi-stage Dockerfile for a Node service.
# syntax=docker/dockerfile:1
FROM node:20-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-alpine AS run
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]Core commands you use daily
docker build -t myapp:1.0 .
docker run --rm -p 3000:3000 --name myapp myapp:1.0
docker ps
docker logs -f myapp
docker exec -it myapp shImages
docker build -t app:1.0 .
docker tag app:1.0 ghcr.io/acme/app:1.0
docker push ghcr.io/acme/app:1.0
docker images
docker history app:1.0
docker inspect app:1.0Containers
docker run -d --name web -p 8080:80 nginx
docker exec -it web sh
docker logs -f --tail 100 web
docker stats --no-stream
docker cp web:/etc/nginx/nginx.conf ./nginx.confCleanup (reclaim disk)
docker system df
docker system prune -af --volumes # careful: removes unused data
docker image prune -af
docker volume prune -fUse it in CI/CD
Build and push an image from GitHub Actions using Buildx and layer caching.
name: build-image
on: { push: { branches: [main] } }
jobs:
image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v6
with:
push: true
tags: ghcr.io/${{ github.repository }}:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=maxBest-practice checklist
- Pin base image tags (or digests) instead of :latest for reproducibility
- Order layers from least- to most-frequently changed to maximize cache hits
- Use multi-stage builds to keep runtime images tiny and attack surface small
- Add a HEALTHCHECK and run as a non-root user
Common pitfalls to avoid
- Running as root inside the container - always add a non-root USER
- Fat images from a single-stage build; use multi-stage and slim/alpine bases
- Baking secrets into layers with COPY or ENV - they stay in the image history
- No .dockerignore, so node_modules and .git bloat the build context
Security quick wins
- Scan images in CI (Trivy/Grype) and fail the build on high-severity CVEs
- Use BuildKit secret mounts (--mount=type=secret) instead of ENV for build-time secrets
- Drop Linux capabilities and run read-only root filesystems where possible
Works well with
- Kubernetes - Declarative orchestration for containers at scale - self-healing, rolling updates and service discovery.
- Trivy - A fast, all-in-one scanner for container images, filesystems, IaC and Kubernetes.
- GitHub Actions - CI/CD built into GitHub - event-driven workflows defined in YAML, with a huge marketplace of actions.
A cheat sheet is muscle memory on paper - keep it close until the commands live in your fingers.
Want senior help putting Docker to work - or a team bootcamp on it? CodeWizard runs Israel's largest DevOps community and embeds named senior engineers with a transparent hours log. Book a free consult or explore the Academy.