How to Use Docker Compose
Purpose
Learn how to define and manage multiple Docker containers using a simple YAML configuration file with Docker Compose.
1. Install Docker Compose
If you don't already have Docker Compose installed, install it:
sudo apt update
sudo apt install docker-compose-plugin -y
2. Create a Docker Compose File
Create a directory for your project and add a docker-compose.yml
file inside it:
mkdir my-project
cd my-project
nano docker-compose.yml
Example docker-compose.yml
:
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
3. Start the Application
Use Docker Compose to bring up your containers:
docker compose up -d
The -d
option runs it in the background (detached mode).
4. Stop the Application
When you want to stop and remove all containers defined in the Compose file:
docker compose down
✅ Summary
- Compose simplifies multi-container deployments with a single file.
- Use volumes inside
docker-compose.yml
to persist your data. - Keep your Compose files under version control (like Git) for easy recovery and updates.
No Comments