|
| 1 | +--- |
| 2 | +id: docker-compose |
| 3 | +title: Docker Compose Basics |
| 4 | +sidebar_label: 1. Basics |
| 5 | +sidebar_position: 5 |
| 6 | +tags: |
| 7 | + [ |
| 8 | + docker, |
| 9 | + docker-compose, |
| 10 | + multi-container, |
| 11 | + basics |
| 12 | + ] |
| 13 | +description: Learn Docker Compose fundamentals - basic structure, service configuration, and simple multi-container applications. |
| 14 | +--- |
| 15 | + |
| 16 | +# Docker Compose Basics |
| 17 | + |
| 18 | +Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services, networks, and volumes. |
| 19 | + |
| 20 | +## What is Docker Compose? |
| 21 | + |
| 22 | +Docker Compose allows you to: |
| 23 | + |
| 24 | +- **Define** multi-container applications in a single file |
| 25 | +- **Start** all services with one command |
| 26 | +- **Scale** services up or down easily |
| 27 | +- **Manage** application lifecycle |
| 28 | +- **Share** configurations with your team |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## Basic docker-compose.yml Structure |
| 33 | + |
| 34 | +Understanding the fundamental structure of Docker Compose files. |
| 35 | + |
| 36 | +### Simple Example |
| 37 | + |
| 38 | +```yaml |
| 39 | +version: '3.8' |
| 40 | + |
| 41 | +services: |
| 42 | + web: |
| 43 | + image: nginx:alpine |
| 44 | + ports: |
| 45 | + - "8080:80" |
| 46 | + |
| 47 | + database: |
| 48 | + image: postgres:13 |
| 49 | + environment: |
| 50 | + POSTGRES_PASSWORD: mypassword |
| 51 | +``` |
| 52 | +
|
| 53 | +### Complete Structure |
| 54 | +
|
| 55 | +```yaml |
| 56 | +version: '3.8' |
| 57 | + |
| 58 | +services: |
| 59 | + # Service definitions |
| 60 | + |
| 61 | +networks: |
| 62 | + # Network definitions |
| 63 | + |
| 64 | +volumes: |
| 65 | + # Volume definitions |
| 66 | + |
| 67 | +configs: |
| 68 | + # Config definitions (Swarm mode) |
| 69 | + |
| 70 | +secrets: |
| 71 | + # Secret definitions (Swarm mode) |
| 72 | +``` |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +## Service Configuration |
| 77 | + |
| 78 | +Configure individual services within your Docker Compose application. |
| 79 | + |
| 80 | +### Basic Service Options |
| 81 | + |
| 82 | +```yaml |
| 83 | +services: |
| 84 | + web: |
| 85 | + # Use existing image |
| 86 | + image: nginx:alpine |
| 87 | + |
| 88 | + # Build from Dockerfile |
| 89 | + build: . |
| 90 | + |
| 91 | + # Build with context and dockerfile |
| 92 | + build: |
| 93 | + context: . |
| 94 | + dockerfile: Dockerfile.prod |
| 95 | + args: |
| 96 | + - NODE_ENV=production |
| 97 | + |
| 98 | + # Container name |
| 99 | + container_name: my-web-app |
| 100 | + |
| 101 | + # Restart policy |
| 102 | + restart: unless-stopped |
| 103 | + |
| 104 | + # Port mapping |
| 105 | + ports: |
| 106 | + - "8080:80" |
| 107 | + - "443:443" |
| 108 | + |
| 109 | + # Environment variables |
| 110 | + environment: |
| 111 | + - NODE_ENV=production |
| 112 | + - DEBUG=false |
| 113 | + |
| 114 | + # Environment file |
| 115 | + env_file: |
| 116 | + - .env |
| 117 | + - .env.prod |
| 118 | +``` |
| 119 | +
|
| 120 | +### Advanced Service Options |
| 121 | +
|
| 122 | +```yaml |
| 123 | +services: |
| 124 | + app: |
| 125 | + image: my-app:latest |
| 126 | + |
| 127 | + # Dependencies |
| 128 | + depends_on: |
| 129 | + - database |
| 130 | + - redis |
| 131 | + |
| 132 | + # Health check dependency |
| 133 | + depends_on: |
| 134 | + database: |
| 135 | + condition: service_healthy |
| 136 | + |
| 137 | + # Volume mounts |
| 138 | + volumes: |
| 139 | + - ./src:/app/src |
| 140 | + - app-data:/app/data |
| 141 | + - /host/logs:/app/logs:ro |
| 142 | + |
| 143 | + # Networks |
| 144 | + networks: |
| 145 | + - frontend |
| 146 | + - backend |
| 147 | + |
| 148 | + # Health check |
| 149 | + healthcheck: |
| 150 | + test: ["CMD", "curl", "-f", "http://localhost:3000/health"] |
| 151 | + interval: 30s |
| 152 | + timeout: 10s |
| 153 | + retries: 3 |
| 154 | + start_period: 40s |
| 155 | +``` |
| 156 | +
|
| 157 | +--- |
| 158 | +
|
| 159 | +## Simple Examples |
| 160 | +
|
| 161 | +Practical examples to get started with Docker Compose. |
| 162 | +
|
| 163 | +### Web Application Stack |
| 164 | +
|
| 165 | +```yaml |
| 166 | +version: '3.8' |
| 167 | + |
| 168 | +services: |
| 169 | + # Frontend |
| 170 | + frontend: |
| 171 | + build: ./frontend |
| 172 | + ports: |
| 173 | + - "3000:3000" |
| 174 | + environment: |
| 175 | + - REACT_APP_API_URL=http://localhost:5000 |
| 176 | + depends_on: |
| 177 | + - backend |
| 178 | + |
| 179 | + # Backend API |
| 180 | + backend: |
| 181 | + build: ./backend |
| 182 | + ports: |
| 183 | + - "5000:5000" |
| 184 | + environment: |
| 185 | + - DATABASE_URL=postgresql://user:password@database:5432/myapp |
| 186 | + depends_on: |
| 187 | + - database |
| 188 | + |
| 189 | + # Database |
| 190 | + database: |
| 191 | + image: postgres:13-alpine |
| 192 | + environment: |
| 193 | + POSTGRES_DB: myapp |
| 194 | + POSTGRES_USER: user |
| 195 | + POSTGRES_PASSWORD: password |
| 196 | + volumes: |
| 197 | + - postgres-data:/var/lib/postgresql/data |
| 198 | + |
| 199 | +volumes: |
| 200 | + postgres-data: |
| 201 | +``` |
| 202 | +
|
| 203 | +### WordPress with MySQL |
| 204 | +
|
| 205 | +```yaml |
| 206 | +version: '3.8' |
| 207 | + |
| 208 | +services: |
| 209 | + wordpress: |
| 210 | + image: wordpress:latest |
| 211 | + ports: |
| 212 | + - "8080:80" |
| 213 | + environment: |
| 214 | + WORDPRESS_DB_HOST: mysql:3306 |
| 215 | + WORDPRESS_DB_USER: wordpress |
| 216 | + WORDPRESS_DB_PASSWORD: wordpress_password |
| 217 | + WORDPRESS_DB_NAME: wordpress |
| 218 | + volumes: |
| 219 | + - wordpress-data:/var/www/html |
| 220 | + depends_on: |
| 221 | + - mysql |
| 222 | + |
| 223 | + mysql: |
| 224 | + image: mysql:8.0 |
| 225 | + environment: |
| 226 | + MYSQL_DATABASE: wordpress |
| 227 | + MYSQL_USER: wordpress |
| 228 | + MYSQL_PASSWORD: wordpress_password |
| 229 | + MYSQL_ROOT_PASSWORD: root_password |
| 230 | + volumes: |
| 231 | + - mysql-data:/var/lib/mysql |
| 232 | + |
| 233 | +volumes: |
| 234 | + wordpress-data: |
| 235 | + mysql-data: |
| 236 | +``` |
| 237 | +
|
| 238 | +--- |
| 239 | +
|
| 240 | +## Networks and Volumes |
| 241 | +
|
| 242 | +Manage container networking and data persistence. |
| 243 | +
|
| 244 | +### Basic Networks |
| 245 | +
|
| 246 | +```yaml |
| 247 | +version: '3.8' |
| 248 | + |
| 249 | +services: |
| 250 | + web: |
| 251 | + image: nginx |
| 252 | + networks: |
| 253 | + - frontend |
| 254 | + |
| 255 | + api: |
| 256 | + image: node:alpine |
| 257 | + networks: |
| 258 | + - frontend |
| 259 | + - backend |
| 260 | + |
| 261 | + database: |
| 262 | + image: postgres |
| 263 | + networks: |
| 264 | + - backend |
| 265 | + |
| 266 | +networks: |
| 267 | + frontend: |
| 268 | + backend: |
| 269 | +``` |
| 270 | +
|
| 271 | +### Basic Volumes |
| 272 | +
|
| 273 | +```yaml |
| 274 | +version: '3.8' |
| 275 | + |
| 276 | +services: |
| 277 | + database: |
| 278 | + image: postgres:13 |
| 279 | + volumes: |
| 280 | + - db-data:/var/lib/postgresql/data |
| 281 | + - ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro |
| 282 | + environment: |
| 283 | + POSTGRES_PASSWORD: password |
| 284 | + |
| 285 | +volumes: |
| 286 | + db-data: |
| 287 | +``` |
| 288 | +
|
| 289 | +--- |
| 290 | +
|
| 291 | +## Environment Variables |
| 292 | +
|
| 293 | +Manage configuration through environment variables. |
| 294 | +
|
| 295 | +### Using .env Files |
| 296 | +
|
| 297 | +**docker-compose.yml** |
| 298 | +```yaml |
| 299 | +version: '3.8' |
| 300 | + |
| 301 | +services: |
| 302 | + app: |
| 303 | + image: my-app:${TAG:-latest} |
| 304 | + environment: |
| 305 | + - NODE_ENV=${NODE_ENV:-development} |
| 306 | + - DATABASE_URL=${DATABASE_URL} |
| 307 | + env_file: |
| 308 | + - .env |
| 309 | +``` |
| 310 | +
|
| 311 | +**.env file** |
| 312 | +```bash |
| 313 | +TAG=v1.0.0 |
| 314 | +NODE_ENV=production |
| 315 | +DATABASE_URL=postgresql://user:pass@db:5432/myapp |
| 316 | +``` |
| 317 | + |
| 318 | +--- |
| 319 | + |
| 320 | +## Basic Commands |
| 321 | + |
| 322 | +Essential Docker Compose commands for daily operations. |
| 323 | + |
| 324 | +```bash |
| 325 | +# Start services |
| 326 | +docker-compose up |
| 327 | + |
| 328 | +# Start in background |
| 329 | +docker-compose up -d |
| 330 | + |
| 331 | +# Stop services |
| 332 | +docker-compose down |
| 333 | + |
| 334 | +# View logs |
| 335 | +docker-compose logs |
| 336 | + |
| 337 | +# Execute commands |
| 338 | +docker-compose exec web bash |
| 339 | + |
| 340 | +# View running services |
| 341 | +docker-compose ps |
| 342 | + |
| 343 | +# Scale services |
| 344 | +docker-compose up --scale web=3 |
| 345 | +``` |
| 346 | + |
| 347 | +--- |
| 348 | + |
| 349 | +## Next Steps |
| 350 | + |
| 351 | +Now that you understand Docker Compose basics: |
| 352 | + |
| 353 | +1. **Practice with Examples** - Try the provided examples in your projects |
| 354 | +2. **Learn Advanced Features** - Explore production configurations and scaling |
| 355 | +3. **Master Complex Architectures** - Build microservices and multi-environment setups |
| 356 | + |
| 357 | +Ready for advanced topics? Check out [Docker Compose Advanced](./docker-compose-advanced.md) for production-ready configurations! |
0 commit comments