Running a single container is easyβ¦
But what if your app needs:
- Database
- Backend
- Frontend
Thatβs where Docker Compose comes in.
πΉ What is Docker Compose?
Docker Compose lets you define and run multi-container applications using a simple YAML file.
πΉ Install Docker Compose
sudo apt install docker-compose-plugin
Verify:
docker compose version
πΉ Example: Run Nginx with One File
Create a file:
nano docker-compose.yml
Paste this:
version: '3.8'
services:
web:
image: nginx:latest
container_name: my-nginx
ports:
- "8080:80"
restart: unless-stopped
πΉ Start the App
docker compose up -d
Now open:
http://your-server-ip:8080
πΉ Stop the App
docker compose down
πΉ Example: App + Database
version: '3.8'
services:
app:
image: node:18
working_dir: /app
volumes:
- ./app:/app
command: npm start
ports:
- "3000:3000"
db:
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
πΉ Why Use Docker Compose?
- π§ Simple configuration
- π One command to start everything
- π Easy to version control
- π Perfect for development & production
πΉ Pro Tips (Advanced)
- Use
.envfile for secrets - Always use
restart: unless-stopped - Use volumes for persistence
- Keep services isolated
πΉ Conclusion
With Docker Compose you can:
- Run full apps with one command
- Manage services easily
- Scale your infrastructure