← Back to Blog

Docker Cheat Sheet 2026: Commands, Dockerfile Best Practices, and Compose

Docker is the foundation of modern deployment. Whether you are containerizing a Node.js API or running a multi-service stack with Compose, this cheat sheet covers every command and pattern you need.

Container Lifecycle

# Run a container
docker run -d --name myapp -p 8080:3000 myimage:latest
docker run -it --rm ubuntu:22.04 bash  # interactive, auto-remove on exit

# Lifecycle
docker start myapp
docker stop myapp           # graceful shutdown (SIGTERM, then SIGKILL after 10s)
docker restart myapp
docker kill myapp            # immediate SIGKILL
docker rm myapp              # remove stopped container
docker rm -f myapp           # force remove (even if running)

# List containers
docker ps                    # running
docker ps -a                 # all (including stopped)
docker ps -q                 # just IDs (useful for scripting)

# Cleanup
docker container prune       # remove all stopped containers
docker system prune -a       # remove everything unused (containers, images, networks, cache)

Image Management

# Build
docker build -t myapp:1.0 .
docker build -t myapp:1.0 -f docker/Dockerfile.prod .  # custom Dockerfile
docker build --no-cache -t myapp:1.0 .                  # ignore cache
docker build --build-arg NODE_ENV=production -t myapp .  # build arguments

# List and inspect
docker images
docker image inspect myapp:1.0
docker history myapp:1.0     # see layer sizes and commands

# Tag and push
docker tag myapp:1.0 registry.example.com/myapp:1.0
docker push registry.example.com/myapp:1.0

# Cleanup
docker image prune           # remove dangling images
docker image prune -a        # remove all unused images

Dockerfile Best Practices

Multi-Stage Build (Production Pattern)

# Stage 1: Build
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production && npm cache clean --force
COPY . .
RUN npm run build

# Stage 2: Production
FROM node:20-alpine
WORKDIR /app
RUN addgroup -g 1001 appgroup && adduser -u 1001 -G appgroup -s /bin/sh -D appuser

COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./

USER appuser
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://localhost:3000/health || exit 1
CMD ["node", "dist/server.js"]

Key Rules

  • Order layers by change frequency: Put COPY package*.json before COPY . . to cache the npm install layer.
  • Use .dockerignore: Exclude node_modules, .git, .env, test files. Generate one with our .gitignore Generator (similar patterns).
  • Use npm ci not npm install in Dockerfiles. ci is deterministic and faster in CI.
  • Run as non-root: Always create and switch to a non-root user with USER.
  • Use Alpine or distroless base images. Alpine is ~5MB vs ~120MB for Debian.
  • Pin versions: Use node:20.11-alpine not node:latest.
  • Add HEALTHCHECK: Enables Docker and orchestrators to detect unhealthy containers.

Docker Compose

# docker-compose.yml
services:
  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      DATABASE_URL: postgres://user:pass@db:5432/mydb
      REDIS_URL: redis://cache:6379
    depends_on:
      db:
        condition: service_healthy
    restart: unless-stopped

  db:
    image: postgres:16-alpine
    volumes:
      - pgdata:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: pass
      POSTGRES_DB: mydb
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

  cache:
    image: redis:7-alpine
    command: redis-server --maxmemory 256mb --maxmemory-policy allkeys-lru

volumes:
  pgdata:
# Compose commands
docker compose up -d             # start all services
docker compose down              # stop and remove containers
docker compose down -v           # also remove volumes
docker compose logs -f app       # follow logs for one service
docker compose exec app sh       # shell into running container
docker compose build --no-cache  # rebuild images
docker compose ps                # list running services

Debugging Containers

# View logs
docker logs myapp                # all logs
docker logs -f myapp             # follow (tail)
docker logs --tail 100 myapp     # last 100 lines
docker logs --since 1h myapp     # last hour

# Execute commands in running container
docker exec -it myapp sh         # get a shell
docker exec myapp cat /etc/hosts # run single command

# Inspect container details
docker inspect myapp             # full JSON metadata
docker inspect --format='{{.NetworkSettings.IPAddress}}' myapp
docker stats                     # live resource usage (CPU, memory, I/O)
docker top myapp                 # processes inside container

# Copy files
docker cp myapp:/app/logs/error.log ./error.log
docker cp ./config.json myapp:/app/config.json

Networking

# List networks
docker network ls

# Create a custom network (containers can resolve each other by name)
docker network create mynet
docker run -d --name api --network mynet myapp
docker run -d --name db --network mynet postgres

# From the api container, 'db' resolves to the postgres container IP
# No need for IP addresses or links

Volumes and Data Persistence

# Named volume (managed by Docker, survives container removal)
docker volume create mydata
docker run -v mydata:/app/data myimage

# Bind mount (host directory mapped into container)
docker run -v $(pwd)/src:/app/src myimage

# tmpfs mount (in-memory, fast, ephemeral)
docker run --tmpfs /app/tmp myimage

Format Your Docker Configs

Validate YAML for Docker Compose files, format JSON configs, and generate .gitignore files for Docker projects.

Open YAML Validator

The Bottom Line

Master the container lifecycle, write efficient multi-stage Dockerfiles, use Compose for local development, and always run as non-root in production. When debugging, docker logs, docker exec, and docker stats will solve 90% of issues.

Related tools: YAML Validator (for Compose files), JSON Formatter (for Docker inspect output), .gitignore Generator, and 50+ more free tools.