$npx -y skills add tranhieutt/software_development_department --skill docker-patternsProvides Docker and Docker Compose patterns including multi-stage builds, networking, volumes, and production configurations. Use when working with Dockerfile or docker-compose.yml, or when the user mentions Docker, containers, or containerization.
| 1 | # Docker Patterns |
| 2 | |
| 3 | Docker and Docker Compose best practices for containerized development. |
| 4 | |
| 5 | ## When to Activate |
| 6 | |
| 7 | - Setting up Docker Compose for local development |
| 8 | - Designing multi-container architectures |
| 9 | - Troubleshooting container networking or volume issues |
| 10 | - Reviewing Dockerfiles for security and size |
| 11 | - Migrating from local dev to containerized workflow |
| 12 | |
| 13 | ## Docker Compose for Local Development |
| 14 | |
| 15 | ### Standard Web App Stack |
| 16 | |
| 17 | ```yaml |
| 18 | # docker-compose.yml |
| 19 | services: |
| 20 | app: |
| 21 | build: |
| 22 | context: . |
| 23 | target: dev # Use dev stage of multi-stage Dockerfile |
| 24 | ports: |
| 25 | - "3000:3000" |
| 26 | volumes: |
| 27 | - .:/app # Bind mount for hot reload |
| 28 | - /app/node_modules # Anonymous volume -- preserves container deps |
| 29 | environment: |
| 30 | - DATABASE_URL=postgres://postgres:postgres@db:5432/app_dev |
| 31 | - REDIS_URL=redis://redis:6379/0 |
| 32 | - NODE_ENV=development |
| 33 | depends_on: |
| 34 | db: |
| 35 | condition: service_healthy |
| 36 | redis: |
| 37 | condition: service_started |
| 38 | command: npm run dev |
| 39 | |
| 40 | db: |
| 41 | image: postgres:16-alpine |
| 42 | ports: |
| 43 | - "5432:5432" |
| 44 | environment: |
| 45 | POSTGRES_USER: postgres |
| 46 | POSTGRES_PASSWORD: postgres |
| 47 | POSTGRES_DB: app_dev |
| 48 | volumes: |
| 49 | - pgdata:/var/lib/postgresql/data |
| 50 | - ./scripts/init-db.sql:/docker-entrypoint-initdb.d/init.sql |
| 51 | healthcheck: |
| 52 | test: ["CMD-SHELL", "pg_isready -U postgres"] |
| 53 | interval: 5s |
| 54 | timeout: 3s |
| 55 | retries: 5 |
| 56 | |
| 57 | redis: |
| 58 | image: redis:7-alpine |
| 59 | ports: |
| 60 | - "6379:6379" |
| 61 | volumes: |
| 62 | - redisdata:/data |
| 63 | |
| 64 | mailpit: # Local email testing |
| 65 | image: axllent/mailpit |
| 66 | ports: |
| 67 | - "8025:8025" # Web UI |
| 68 | - "1025:1025" # SMTP |
| 69 | |
| 70 | volumes: |
| 71 | pgdata: |
| 72 | redisdata: |
| 73 | ``` |
| 74 | |
| 75 | ### Development vs Production Dockerfile |
| 76 | |
| 77 | ```dockerfile |
| 78 | # Stage: dependencies |
| 79 | FROM node:22-alpine AS deps |
| 80 | WORKDIR /app |
| 81 | COPY package.json package-lock.json ./ |
| 82 | RUN npm ci |
| 83 | |
| 84 | # Stage: dev (hot reload, debug tools) |
| 85 | FROM node:22-alpine AS dev |
| 86 | WORKDIR /app |
| 87 | COPY --from=deps /app/node_modules ./node_modules |
| 88 | COPY . . |
| 89 | EXPOSE 3000 |
| 90 | CMD ["npm", "run", "dev"] |
| 91 | |
| 92 | # Stage: build |
| 93 | FROM node:22-alpine AS build |
| 94 | WORKDIR /app |
| 95 | COPY --from=deps /app/node_modules ./node_modules |
| 96 | COPY . . |
| 97 | RUN npm run build && npm prune --production |
| 98 | |
| 99 | # Stage: production (minimal image) |
| 100 | FROM node:22-alpine AS production |
| 101 | WORKDIR /app |
| 102 | RUN addgroup -g 1001 -S appgroup && adduser -S appuser -u 1001 |
| 103 | USER appuser |
| 104 | COPY --from=build --chown=appuser:appgroup /app/dist ./dist |
| 105 | COPY --from=build --chown=appuser:appgroup /app/node_modules ./node_modules |
| 106 | COPY --from=build --chown=appuser:appgroup /app/package.json ./ |
| 107 | ENV NODE_ENV=production |
| 108 | EXPOSE 3000 |
| 109 | HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://localhost:3000/health || exit 1 |
| 110 | CMD ["node", "dist/server.js"] |
| 111 | ``` |
| 112 | |
| 113 | ### Override Files |
| 114 | |
| 115 | ```yaml |
| 116 | # docker-compose.override.yml (auto-loaded, dev-only settings) |
| 117 | services: |
| 118 | app: |
| 119 | environment: |
| 120 | - DEBUG=app:* |
| 121 | - LOG_LEVEL=debug |
| 122 | ports: |
| 123 | - "9229:9229" # Node.js debugger |
| 124 | |
| 125 | # docker-compose.prod.yml (explicit for production) |
| 126 | services: |
| 127 | app: |
| 128 | build: |
| 129 | target: production |
| 130 | restart: always |
| 131 | deploy: |
| 132 | resources: |
| 133 | limits: |
| 134 | cpus: "1.0" |
| 135 | memory: 512M |
| 136 | ``` |
| 137 | |
| 138 | ```bash |
| 139 | # Development (auto-loads override) |
| 140 | docker compose up |
| 141 | |
| 142 | # Production |
| 143 | docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d |
| 144 | ``` |
| 145 | |
| 146 | ## Networking |
| 147 | |
| 148 | ### Service Discovery |
| 149 | |
| 150 | Services in the same Compose network resolve by service name: |
| 151 | ``` |
| 152 | # From "app" container: |
| 153 | postgres://postgres:postgres@db:5432/app_dev # "db" resolves to the db container |
| 154 | redis://redis:6379/0 # "redis" resolves to the redis container |
| 155 | ``` |
| 156 | |
| 157 | ### Custom Networks |
| 158 | |
| 159 | ```yaml |
| 160 | services: |
| 161 | frontend: |
| 162 | networks: |
| 163 | - frontend-net |
| 164 | |
| 165 | api: |
| 166 | networks: |
| 167 | - frontend-net |
| 168 | - backend-net |
| 169 | |
| 170 | db: |
| 171 | networks: |
| 172 | - backend-net # Only reachable from api, not frontend |
| 173 | |
| 174 | networks: |
| 175 | frontend-net: |
| 176 | backend-net: |
| 177 | ``` |
| 178 | |
| 179 | ### Exposing Only What's Needed |
| 180 | |
| 181 | ```yaml |
| 182 | services: |
| 183 | db: |
| 184 | ports: |
| 185 | - "127.0.0.1:5432:5432" # Only accessible from host, not network |
| 186 | # Omit ports entirely in production -- accessible only within Docker network |
| 187 | ``` |
| 188 | |
| 189 | ## Volume Strategies |
| 190 | |
| 191 | ```yaml |
| 192 | volumes: |
| 193 | # Named volume: persists across container restarts, managed by Docker |
| 194 | pgdata: |
| 195 | |
| 196 | # |