SkillEnglish

Docker for local development: Dockerfile, compose, volumes and cleanup

Practical Docker and Compose v2 patterns for dev environments: multi-stage Dockerfiles, bind mounts and named volumes, healthchecks, compose watch, debugging and safe disk cleanup.

Download SKILL.md Raw file (Markdown)

Install in Claude Code

Put the file in a folder named after the skill inside Claude Code's skills folder, then start a new session. This command does it:

bash
mkdir -p ~/.claude/skills/docker-dev && \
  curl -fsSL https://md2.dev/library/raw/docker-dev.md \
  -o ~/.claude/skills/docker-dev/SKILL.md

Preview

Docker for local development

When to use

Containerizing an app, adding services (database, cache) to a dev stack, reproducing CI locally, or debugging a container that exits or cannot be reached.

BASH
docker --version && docker compose version     # Compose v2 plugin: "docker compose", not "docker-compose"
docker info --format '{{.ServerVersion}}'      # daemon reachable?

Workflow

  1. Write .dockerignore first (node_modules, .git, .env*, dist).
  2. Write a multi-stage Dockerfile: install dependencies in a cached layer, then copy sources.
  3. Describe the dev stack in compose.yaml; no version: key is needed.
  4. docker compose up --build, check ps and logs, fix, repeat.
  5. Keep data in named volumes, secrets in env files outside git.

Dockerfile (Node example)

DOCKERFILE
# syntax=docker/dockerfile:1
FROM node:22-slim AS base
WORKDIR /app

FROM base AS deps
COPY package.json package-lock.json ./
RUN --mount=type=cache,target=/root/.npm npm ci

FROM deps AS dev
COPY . .
CMD ["npm", "run", "dev"]

FROM deps AS build
COPY . .
RUN npm run build && npm prune --omit=dev

FROM base AS prod
ENV NODE_ENV=production
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s CMD node -e "fetch('http://localhost:3000/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"
CMD ["node", "dist/server.js"]

Rules: pin major versions (node:22-slim, not latest), put rarely changing steps first, one process per container, exec-form CMD ["..."], run as non-root.

compose.yaml

YAML
services:
  app:
    build: { context: ., target: dev }
    ports: ["127.0.0.1:3000:3000"]        # bind to localhost only
    env_file: [.env]
    volumes:
      - .:/app                            # live code (bind mount)
      - node_modules:/app/node_modules    # keep container-built deps
    depends_on:
      db: { condition: service_healthy }
    develop:
      watch:
        - { action: sync, path: ./src, target: /app/src }
        - { action: rebuild, path: package.json }
  db:
    image: postgres:17
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD in .env}
    volumes: [pgdata:/var/lib/postgresql/data]
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      retries: 10
volumes:
  node_modules:
  pgdata:

Everyday commands

BASH
docker compose config                       # validate and print the resolved file
docker compose up -d --build                # start in background
docker compose ps
docker compose logs -f --tail 100 app
docker compose exec app sh                  # shell in a running service
docker compose run --rm app npm test        # one-off container
docker compose watch                        # sync/rebuild on file changes
docker compose down                         # stop and remove containers (keeps volumes)
docker compose down -v                      # also deletes named volumes: data loss

docker run --rm -it -v "$PWD":/work -w /work --user "$(id -u):$(id -g)" node:22-slim bash
docker build -t myapp:dev --target dev .
docker inspect --format '{{.State.ExitCode}} {{.State.Error}}' <container>
docker stats --no-stream

Debugging a container that exits

  1. docker compose logs app for the last error; check the exit code with docker inspect.
  2. Override the command to look inside: docker compose run --rm --entrypoint sh app.
  3. Port unreachable: the app must listen on 0.0.0.0, not 127.0.0.1, inside the container; verify the published port with docker compose port app 3000.
  4. Files owned by root on the host: run with --user "$(id -u):$(id -g)" or set user: in compose.
  5. Slow bind mounts on macOS/Windows: use named volumes for node_modules and caches.
  6. Stale image: docker compose build --no-cache app.

Cleanup (check before deleting)

BASH
docker system df                       # what uses space
docker image prune                     # dangling images only
docker image prune -a --filter "until=168h"
docker builder prune --filter "until=168h"
docker container prune
docker volume ls -f dangling=true      # inspect first: volumes hold data
docker system prune                    # stopped containers, unused networks, dangling images

Avoid docker system prune -a --volumes unless the user explicitly wants to delete every unused image and volume.

Gotchas

  • Secrets in ENV or ARG stay in image layers; mount them at runtime or use BuildKit secrets.
  • A bind mount hides files the image put at the same path (for example node_modules).
  • depends_on without condition: service_healthy only waits for start, not readiness.
  • Containers talk to each other by service name (db:5432), never localhost.
  • Rebuild when dependency files change; changing sources alone is covered by the bind mount.

Verify

BASH
docker compose ps --format 'table {{.Service}}\t{{.State}}\t{{.Health}}'
curl -fsS http://127.0.0.1:3000/health && echo ok

Back to Skills