$npx -y skills add BagelHole/DevOps-Security-Agent-Skills --skill docker-composeDefine and run multi-container Docker applications using Docker Compose. Create compose files, manage service dependencies, configure networks and volumes, and orchestrate local development environments. Use when setting up multi-service applications or development environments.
| 1 | # Docker Compose |
| 2 | |
| 3 | Orchestrate multi-container applications with declarative YAML configuration. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | Use this skill when: |
| 8 | - Running multi-container applications locally |
| 9 | - Setting up development environments |
| 10 | - Defining service dependencies and networking |
| 11 | - Managing application stacks with multiple services |
| 12 | - Creating reproducible development setups |
| 13 | |
| 14 | ## Prerequisites |
| 15 | |
| 16 | - Docker Engine with Compose plugin (v2) |
| 17 | - Basic Docker knowledge |
| 18 | - YAML syntax understanding |
| 19 | |
| 20 | ## Basic Configuration |
| 21 | |
| 22 | ### Simple Application Stack |
| 23 | |
| 24 | ```yaml |
| 25 | # docker-compose.yml |
| 26 | version: '3.8' |
| 27 | |
| 28 | services: |
| 29 | web: |
| 30 | build: . |
| 31 | ports: |
| 32 | - "3000:3000" |
| 33 | environment: |
| 34 | - NODE_ENV=development |
| 35 | - DATABASE_URL=postgres://postgres:secret@db:5432/myapp |
| 36 | depends_on: |
| 37 | - db |
| 38 | - redis |
| 39 | |
| 40 | db: |
| 41 | image: postgres:15-alpine |
| 42 | environment: |
| 43 | POSTGRES_USER: postgres |
| 44 | POSTGRES_PASSWORD: secret |
| 45 | POSTGRES_DB: myapp |
| 46 | volumes: |
| 47 | - postgres-data:/var/lib/postgresql/data |
| 48 | ports: |
| 49 | - "5432:5432" |
| 50 | |
| 51 | redis: |
| 52 | image: redis:7-alpine |
| 53 | ports: |
| 54 | - "6379:6379" |
| 55 | |
| 56 | volumes: |
| 57 | postgres-data: |
| 58 | ``` |
| 59 | |
| 60 | ## Service Configuration |
| 61 | |
| 62 | ### Build Options |
| 63 | |
| 64 | ```yaml |
| 65 | services: |
| 66 | app: |
| 67 | build: |
| 68 | context: ./app |
| 69 | dockerfile: Dockerfile.dev |
| 70 | args: |
| 71 | NODE_VERSION: "20" |
| 72 | target: development |
| 73 | cache_from: |
| 74 | - myapp:cache |
| 75 | image: myapp:dev |
| 76 | ``` |
| 77 | |
| 78 | ### Environment Variables |
| 79 | |
| 80 | ```yaml |
| 81 | services: |
| 82 | app: |
| 83 | environment: |
| 84 | - NODE_ENV=production |
| 85 | - API_KEY=${API_KEY} # From shell or .env file |
| 86 | env_file: |
| 87 | - .env |
| 88 | - .env.local |
| 89 | ``` |
| 90 | |
| 91 | ### Port Mapping |
| 92 | |
| 93 | ```yaml |
| 94 | services: |
| 95 | web: |
| 96 | ports: |
| 97 | - "3000:3000" # HOST:CONTAINER |
| 98 | - "127.0.0.1:9229:9229" # Bind to localhost only |
| 99 | - "8080-8090:8080-8090" # Port range |
| 100 | expose: |
| 101 | - "3000" # Internal only (no host binding) |
| 102 | ``` |
| 103 | |
| 104 | ### Volume Mounts |
| 105 | |
| 106 | ```yaml |
| 107 | services: |
| 108 | app: |
| 109 | volumes: |
| 110 | # Named volume |
| 111 | - app-data:/app/data |
| 112 | # Bind mount |
| 113 | - ./src:/app/src |
| 114 | # Read-only bind mount |
| 115 | - ./config:/app/config:ro |
| 116 | # Anonymous volume (for node_modules) |
| 117 | - /app/node_modules |
| 118 | |
| 119 | volumes: |
| 120 | app-data: |
| 121 | driver: local |
| 122 | ``` |
| 123 | |
| 124 | ### Dependencies |
| 125 | |
| 126 | ```yaml |
| 127 | services: |
| 128 | web: |
| 129 | depends_on: |
| 130 | db: |
| 131 | condition: service_healthy |
| 132 | redis: |
| 133 | condition: service_started |
| 134 | |
| 135 | db: |
| 136 | image: postgres:15 |
| 137 | healthcheck: |
| 138 | test: ["CMD-SHELL", "pg_isready -U postgres"] |
| 139 | interval: 10s |
| 140 | timeout: 5s |
| 141 | retries: 5 |
| 142 | ``` |
| 143 | |
| 144 | ## Networking |
| 145 | |
| 146 | ### Custom Networks |
| 147 | |
| 148 | ```yaml |
| 149 | services: |
| 150 | frontend: |
| 151 | networks: |
| 152 | - frontend-net |
| 153 | |
| 154 | backend: |
| 155 | networks: |
| 156 | - frontend-net |
| 157 | - backend-net |
| 158 | |
| 159 | db: |
| 160 | networks: |
| 161 | - backend-net |
| 162 | |
| 163 | networks: |
| 164 | frontend-net: |
| 165 | driver: bridge |
| 166 | backend-net: |
| 167 | driver: bridge |
| 168 | internal: true # No external access |
| 169 | ``` |
| 170 | |
| 171 | ### Network Aliases |
| 172 | |
| 173 | ```yaml |
| 174 | services: |
| 175 | db: |
| 176 | networks: |
| 177 | backend: |
| 178 | aliases: |
| 179 | - database |
| 180 | - postgres |
| 181 | |
| 182 | networks: |
| 183 | backend: |
| 184 | ``` |
| 185 | |
| 186 | ## Resource Limits |
| 187 | |
| 188 | ```yaml |
| 189 | services: |
| 190 | app: |
| 191 | deploy: |
| 192 | resources: |
| 193 | limits: |
| 194 | cpus: '2' |
| 195 | memory: 1G |
| 196 | reservations: |
| 197 | cpus: '0.5' |
| 198 | memory: 256M |
| 199 | ``` |
| 200 | |
| 201 | ## Multiple Compose Files |
| 202 | |
| 203 | ### Override Files |
| 204 | |
| 205 | ```yaml |
| 206 | # docker-compose.yml (base) |
| 207 | services: |
| 208 | web: |
| 209 | image: myapp:latest |
| 210 | ports: |
| 211 | - "3000:3000" |
| 212 | |
| 213 | # docker-compose.override.yml (development - auto-loaded) |
| 214 | services: |
| 215 | web: |
| 216 | build: . |
| 217 | volumes: |
| 218 | - ./src:/app/src |
| 219 | environment: |
| 220 | - DEBUG=true |
| 221 | |
| 222 | # docker-compose.prod.yml (production) |
| 223 | services: |
| 224 | web: |
| 225 | deploy: |
| 226 | replicas: 3 |
| 227 | environment: |
| 228 | - DEBUG=false |
| 229 | ``` |
| 230 | |
| 231 | ### Using Multiple Files |
| 232 | |
| 233 | ```bash |
| 234 | # Development (uses override automatically) |
| 235 | docker compose up |
| 236 | |
| 237 | # Production |
| 238 | docker compose -f docker-compose.yml -f docker-compose.prod.yml up |
| 239 | |
| 240 | # Merge and view final config |
| 241 | docker compose -f docker-compose.yml -f docker-compose.prod.yml config |
| 242 | ``` |
| 243 | |
| 244 | ## Profiles |
| 245 | |
| 246 | ```yaml |
| 247 | services: |
| 248 | web: |
| 249 | image: myapp |
| 250 | |
| 251 | db: |
| 252 | image: postgres:15 |
| 253 | |
| 254 | debug: |
| 255 | image: busybox |
| 256 | profiles: |
| 257 | - debug |
| 258 | |
| 259 | monitoring: |
| 260 | image: prometheus |
| 261 | profiles: |
| 262 | - monitoring |
| 263 | ``` |
| 264 | |
| 265 | ```bash |
| 266 | # Run without profiles (web, db only) |
| 267 | docker compose up |
| 268 | |
| 269 | # Run with debug profile |
| 270 | docker compose --profile debug up |
| 271 | |
| 272 | # Run with multiple profiles |
| 273 | docker compose --profile debug --profile monitoring up |
| 274 | ``` |
| 275 | |
| 276 | ## Commands |
| 277 | |
| 278 | ### Lifecycle |
| 279 | |
| 280 | ```bash |
| 281 | # Start services |
| 282 | docker compose up -d |
| 283 | |
| 284 | # Start specific service |
| 285 | docker compose up -d web |
| 286 | |
| 287 | # Stop services |
| 288 | docker compose stop |
| 289 | |
| 290 | # Stop and remove containers |
| 291 | docker compose down |
| 292 | |
| 293 | # Stop and remove everything incl |