What is Docker Compose?
Docker Compose is a tool that lets you define and run multi-container Docker applications using a single docker-compose.yml file.
Why Use Docker Compose?
- Start all services with one command:
docker compose up - Define networks and volumes declaratively
- Manage multiple containers (PHP + MySQL + Nginx)
Docker Compose Commands
| Command | Description |
|---|---|
docker compose up -d | Start all services in background |
docker compose down | Stop and remove all containers |
docker compose ps | List running services |
docker compose logs | View logs |
Example: Nginx + PHP Compose File
version: "3.9"
services:
nginx:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
php:
image: php:8.2-fpm
volumes:
- ./html:/var/www/html
docker compose up -d
Example: Laravel + MySQL Stack
version: "3.9"
services:
app:
build: .
volumes:
- .:/var/www/html
mysql:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: laravel_db
volumes:
- mysql-data:/var/lib/mysql
nginx:
image: nginx:latest
ports:
- "8080:80"
volumes:
- .:/var/www/html
volumes:
mysql-data:# Start the full stack
docker compose up -d
# Run artisan commands
docker compose exec app php artisan migrate
# View logs
docker compose logs -f
# Stop everything
docker compose down
docker-compose.yml Structure
| Key | Description |
|---|---|
services | Define containers |
image | Docker image to use |
build | Build from Dockerfile |
ports | Port mapping |
volumes | Mount volumes |
environment | Environment variables |
Summary
- Docker Compose manages multi-container applications with one YAML file
- Use
docker compose up -dto start everything - Use
docker compose downto stop and remove containers - Perfect for Laravel, Node.js, or any full-stack app
0
likes
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
