DM
Technical reference

Docker Cheatsheet

Package and run applications consistently

Must Know

bash

Core Commands

docker build -t my-app:dev .
docker run --rm -p 3000:3000 --env-file .env my-app:dev
docker ps
docker logs -f CONTAINER
docker

Basic Dockerfile

Pin major versions and use a lockfile.

FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
CMD ["npm", "start"]

Important Patterns

docker

Multi-Stage Build

Keep build tools out of the runtime image.

FROM node:22-alpine AS build
WORKDIR /app
COPY . .
RUN npm ci && npm run build

FROM node:22-alpine AS runtime
ENV NODE_ENV=production
COPY --from=build /app/.next/standalone ./
CMD ["node", "server.js"]
yaml

Compose

services:
  app:
    build: .
    ports: ['3000:3000']
    depends_on: [db]
  db:
    image: postgres:17
    volumes: [db-data:/var/lib/postgresql/data]
volumes:
  db-data:

Useful Recipes

bash

.dockerignore

Reduce build context and keep secrets out.

node_modules
.next
.git
.env*
npm-debug.log
coverage
docker

Health Check

Health endpoints should test readiness without expensive work.

HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://localhost:3000/health || exit 1

Pitfalls & Production

docker

Run as Non-Root

Use a minimal image and a non-root runtime user.

RUN addgroup -S app && adduser -S app -G app
USER app

Image Safety

Containers isolate processes; they are not a complete security boundary.

never bake secrets into ARG/ENV layers
scan images
pin dependencies
rebuild for security patches
set CPU/memory limits