Introduction to Docker Compose
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.
Basic docker-compose.yml Structure
A typical docker-compose.yml file defines services, networks, and volumes. Here's a simple example for a Laravel application.
version: '3.8'
services:
app:
build: .
ports:
- "8000:8000"
volumes:
- .:/var/www/html
depends_on:
- db
db:
image: mysql:8.0
environment:
MYSQL_DATABASE: laravel
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:Common Docker Compose Commands
Starting Services
- docker-compose up -d (start in background)
- docker-compose up --build (rebuild images)
- docker-compose logs (view logs)
- docker-compose down (stop and remove)
Networking in Docker Compose
By default, Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
networks:
frontend:
driver: bridge
backend:
driver: bridge
services:
web:
networks:
- frontend
- backend
api:
networks:
- backend
db:
networks:
- backendThis network configuration allows for better isolation and security in complex applications.