Cheatsheet

Docker Cheatsheet

Every Docker command and Dockerfile directive you use in real work. Build to compose to production.

Docker's surface area is huge but the commands you use daily are small. This cheatsheet covers the daily set plus the Dockerfile directives you write when authoring images.

Basic commands

`docker build -t name .` — build image from current directory.

`docker run --rm -it -p 3000:3000 name` — run interactive with port.

`docker ps` and `docker ps -a` — list running or all containers.

`docker logs -f <container>` — follow logs.

`docker exec -it <container> sh` — shell into a running container.

Images and volumes

`docker images` and `docker rmi <image>` — list and remove images.

`docker volume create data` and `docker run -v data:/app/data ...` — persistent storage.

`docker system prune -a` — nuclear cleanup (destructive).

Dockerfile essentials

The directives you write most often.

  • `FROM node:20-alpine` — base image (prefer alpine for size)
  • `WORKDIR /app` — set working dir
  • `COPY package*.json ./` then `RUN npm ci` — leverage layer caching
  • `COPY . .` — copy source (after deps for cache)
  • `EXPOSE 3000` — documentation only, not enforced
  • `CMD ["node", "server.js"]` — default command
  • `HEALTHCHECK` — optional but useful for orchestrators

Multi-stage builds

Build in one stage, copy artifacts to a smaller runtime image.

`FROM node:20 AS build` ... `FROM node:20-alpine` ... `COPY --from=build /app/dist ./dist`

docker-compose

`docker-compose up -d` — start services detached.

`docker-compose logs -f service` — follow service logs.

`docker-compose down -v` — stop and remove volumes (destructive).

Frequently asked questions

alpine or slim base images?

Alpine is smallest but uses musl libc; some Node native modules don't build. Slim (debian-slim) is a safer default.

Should I use latest tag in production?

No. Pin to a version (`node:20.11-alpine`) or a digest. `latest` breaks on rebuild.

COPY vs ADD?

Prefer COPY. ADD auto-extracts tarballs and fetches URLs — surprising in a Dockerfile.

How do I keep image size down?

Multi-stage builds, `.dockerignore` for node_modules and .git, minimal base image, combine RUN commands to reduce layers.

docker-compose in production?

Fine for single-host. For multi-host, use Kubernetes, ECS, or Docker Swarm.

Keep exploring

Made for exam season

Pass that exam.

Turn your notes into flashcards and quizzes in seconds. Study smarter — start free today.