Define and run multi-container applications with a single YAML file.
Docker Compose simplifies managing multiple containers that work together.
Docker Compose allows you to define and run multi-container applications using a single YAML file. Instead of running each container separately, you define all services in one place and start them all with a single command.
```yaml services:
web:
image: nginx:latest
ports:
- "8080:80"
networks:
- app-network
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
volumes:
- postgres-data:/var/lib/postgresql/data
networks:
- app-network
networks:
app-network:
volumes:
postgres-data:
```
```bash docker compose up -d # Start services in background docker compose down # Stop and remove containers docker compose logs # View logs docker compose ps # List containers docker compose restart # Restart services docker compose up -d –build # Rebuild and start ```
Containers on the same network can reach each other using service names. In the example above, the web container can reach the database at `db:5432`.
Services:
Networks:
Volumes: