# 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:

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary" id="bkmrk-bash"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary select-none rounded-t-[5px]">bash</div><div class="overflow-y-auto p-4" dir="ltr"></div></div>```bash
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:

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary" id="bkmrk-bash-1"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary select-none rounded-t-[5px]">bash</div><div class="overflow-y-auto p-4" dir="ltr"></div></div>```bash
mkdir my-project
cd my-project
nano docker-compose.yml
```

Example `docker-compose.yml`:

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary" id="bkmrk-yaml"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary select-none rounded-t-[5px]">yaml</div><div class="overflow-y-auto p-4" dir="ltr"></div></div>```yaml
version: '3'

services:
  web:
    image: nginx
    ports:
      - "8080:80"
```

---

## 3. Start the Application

Use Docker Compose to bring up your containers:

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary" id="bkmrk-bash-2"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary select-none rounded-t-[5px]">bash</div><div class="overflow-y-auto p-4" dir="ltr"></div></div>```bash
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:

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary" id="bkmrk-bash-3"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary select-none rounded-t-[5px]">bash</div><div class="overflow-y-auto p-4" dir="ltr"></div></div>```bash
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.