Field intelligence for AI-first professionalsVol. II · Nº 56 · Saturday, August 15, 2026
← All skills
Containers & Orchestrationv1.0Updated 2026-03-04Free

Docker

Container building, Compose orchestration, networking, and production optimization

Gives Claude Code expertise in Dockerfile best practices, multi-stage builds, Docker Compose, networking, volume management, image optimization, and security hardening for containerized applications.

Use case: Dockerfile writing, multi-stage build optimization, Docker Compose services, container networking, volume management, image size reduction, security scanning


Download
§  The skill file

name: docker

description: Use when writing Dockerfiles, configuring Docker Compose, managing containers, optimizing images, or setting up container networking. Covers multi-stage builds, Compose services, volumes, and security.


# Docker Skill

Quick Start

Essential Commands

bash
docker build -t myapp:v1 .                    # Build image
docker run -d -p 8080:80 --name web myapp:v1   # Run container
docker compose up -d                            # Start all services
docker compose down                             # Stop and remove
docker logs web --tail=100 -f                   # Follow logs
docker exec -it web /bin/sh                     # Shell into container
docker ps                                       # List running containers
docker images                                   # List local images

Multi-Stage Dockerfile (Node.js)

dockerfile
# Stage 1: Build
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --production=false
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
CMD ["node", "dist/index.js"]

API Reference

Dockerfile Instructions

InstructionPurposeExample
FROMBase imageFROM node:20-alpine
WORKDIRSet working directoryWORKDIR /app
COPYCopy files from hostCOPY package*.json ./
RUNExecute command in buildRUN npm ci
CMDDefault command (overridable)CMD ["node", "app.js"]
ENTRYPOINTFixed command (args appended)ENTRYPOINT ["python"]
ENVSet environment variableENV NODE_ENV=production
EXPOSEDocument port (informational)EXPOSE 3000
VOLUMECreate mount pointVOLUME ["/data"]
USERSet runtime userUSER appuser
ARGBuild-time variableARG VERSION=latest
HEALTHCHECKContainer health checkHEALTHCHECK CMD curl -f http://localhost/

Docker Compose

yaml
# docker-compose.yml
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:3000"
    environment:
      - DATABASE_URL=postgres://user:pass@db:5432/app
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - ./src:/app/src  # Dev: mount source for hot reload
    restart: unless-stopped

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

volumes:
  pgdata:

Common Patterns

Layer Caching Optimization

dockerfile
# BAD: Invalidates cache on any file change
COPY . .
RUN npm ci && npm run build

# GOOD: Cache dependencies separately
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

Networking

bash
# Create custom network
docker network create app-network

# Run containers on same network (can reach each other by name)
docker run -d --name db --network app-network postgres:16
docker run -d --name web --network app-network -p 8080:3000 myapp

# Inside web container: db resolves to the postgres container

Compose creates a default network automatically. Services reach each other by service name.

Volume Types

bash
# Named volume (managed by Docker, persistent)
docker volume create mydata
docker run -v mydata:/app/data myapp

# Bind mount (host directory, for development)
docker run -v $(pwd)/src:/app/src myapp

# tmpfs (memory only, no persistence)
docker run --tmpfs /tmp myapp

Image Optimization

dockerfile
# Use Alpine base (5MB vs 100MB+)
FROM node:20-alpine

# Combine RUN commands (fewer layers)
RUN apk add --no-cache curl && \
    npm ci --production && \
    npm cache clean --force

# .dockerignore (exclude from COPY)
# node_modules, .git, .env, *.md, tests/

Error Handling

IssueCauseFix
"port already allocated"Port in use on hostChange host port or stop conflicting container
"no space left on device"Docker disk fulldocker system prune -a (removes unused images/containers)
"OOMKilled"Container exceeded memory limitIncrease mem_limit in Compose or fix memory leak
Build cache not workingCOPY before dependency installRestructure Dockerfile for layer caching
"network not found"Container referencing removed networkRecreate network or use Compose (auto-creates)

Best Practices

  • Non-root user: Always add USER appuser in production Dockerfiles. Never run containers as root.
  • Multi-stage builds: Separate build dependencies from runtime. Final image should only contain what's needed to run.
  • .dockerignore: Always include one. Exclude node_modules, .git, .env, tests, docs. Speeds up builds and reduces image size.
  • Pin versions: Use specific image tags (node:20.11-alpine) not latest. Pin package versions in RUN commands.
  • Health checks: Add HEALTHCHECK in Dockerfile or healthcheck in Compose. Enables restart policies and orchestrator health monitoring.
  • Log rotation: Set in daemon.json: {"log-driver":"json-file","log-opts":{"max-size":"100m","max-file":"3"}}. Without this, logs grow unbounded.
  • Secrets: Never COPY .env into images. Use Docker secrets, environment variables at runtime, or mount secret files as volumes.
§  Sources
https://docs.docker.com/reference/dockerfile/https://docs.docker.com/compose/https://docs.docker.com/engine/reference/commandline/cli/https://docs.docker.com/build/building/multi-stage/