|
| 1 | +--- |
| 2 | +sidebar_position: 7 |
| 3 | +title: "Docker Compose: The Fleet Commander" |
| 4 | +sidebar_label: "7. Docker Compose" |
| 5 | +description: "Learn how to orchestrate multiple containers (Frontend, Backend, DB) using a single YAML file. Understand the key keywords like services, build, ports, and depends_on. Master the essential docker-compose commands to manage your entire stack with ease." |
| 6 | +--- |
| 7 | + |
| 8 | +Until now, if you wanted to run a Full-stack app, you had to: |
| 9 | +1. Create a network. |
| 10 | +2. Start the Database container. |
| 11 | +3. Start the Backend container (linking it to the DB). |
| 12 | +4. Start the Frontend container (linking it to the Backend). |
| 13 | + |
| 14 | +That is a lot of typing! **Docker Compose** allows you to write all these instructions in a single `docker-compose.yml` file. When you run `docker-compose up`, Docker reads the file and starts everything in the correct order. It's like being a **Fleet Commander** instead of a solo driver. |
| 15 | + |
| 16 | +## 1. The "Orchestra" Analogy |
| 17 | + |
| 18 | +Think of your containers as **Musicians**: |
| 19 | +* **The Dockerfile:** Is the sheet music for *one* instrument (e.g., just the Violin). |
| 20 | +* **Docker Compose:** Is the **Conductor**. The conductor doesn't play an instrument; they tell every musician when to start, how loud to play, and how to stay in sync. |
| 21 | + |
| 22 | +## 2. The YAML Blueprint |
| 23 | + |
| 24 | +Docker Compose uses **YAML** (Yet Another Markup Language). It is easy to read because it uses indentation instead of curly braces. |
| 25 | + |
| 26 | +Here is a standard `docker-compose.yml` for a **CodeHarborHub** project: |
| 27 | + |
| 28 | +```yaml |
| 29 | +version: '3.8' |
| 30 | + |
| 31 | +services: |
| 32 | + frontend: |
| 33 | + build: ./frontend |
| 34 | + ports: |
| 35 | + - "3000:3000" |
| 36 | + depends_on: |
| 37 | + - backend |
| 38 | + |
| 39 | + backend: |
| 40 | + build: ./backend |
| 41 | + ports: |
| 42 | + - "5000:5000" |
| 43 | + environment: |
| 44 | + - DB_URL=mongodb://database:27017/hub_db |
| 45 | + depends_on: |
| 46 | + - database |
| 47 | + |
| 48 | + database: |
| 49 | + image: mongo:latest |
| 50 | + volumes: |
| 51 | + - hub_data:/data/db |
| 52 | + |
| 53 | +volumes: |
| 54 | + hub_data: |
| 55 | +``` |
| 56 | +
|
| 57 | +## 3. The 3 Steps of Compose |
| 58 | +
|
| 59 | +To get your entire system running, you only need three steps: |
| 60 | +
|
| 61 | +1. **Define** each service's environment with its own `Dockerfile`. |
| 62 | +2. **Define** how they connect in the `docker-compose.yml`. |
| 63 | +3. **Run** `docker-compose up` to start the whole world. |
| 64 | + |
| 65 | +## 4. Key Keywords to Remember |
| 66 | + |
| 67 | +* **services:** These are your containers (e.g., `web`, `api`, `db`). |
| 68 | +* **build:** Tells Docker to look for a `Dockerfile` in a specific folder. |
| 69 | +* **image:** Tells Docker to download a pre-built image instead of building one. |
| 70 | +* **ports:** Maps the Host port to the Container port (just like `-p`). |
| 71 | +* **depends_on:** Tells Docker the order of operations (e.g., "Don't start the Backend until the Database is ready"). |
| 72 | +* **environment:** Passes variables (like API keys) into the container. |
| 73 | + |
| 74 | +## The Logic of Orchestration |
| 75 | + |
| 76 | +When you run `docker-compose up`, Docker performs the following "Math": |
| 77 | + |
| 78 | +```mermaid |
| 79 | +graph TD |
| 80 | + A[Read docker-compose.yml] --> B[Create shared Network] |
| 81 | + B --> C[Create shared Volumes] |
| 82 | + C --> D[Pull/Build Images] |
| 83 | + D --> E[Start Containers in Order] |
| 84 | + E --> F{Health Check} |
| 85 | +``` |
| 86 | + |
| 87 | +$$Total\_Stack = (Net + Vol) + \sum(Service_{1...n})$$ |
| 88 | + |
| 89 | +Where: |
| 90 | + |
| 91 | +* **Net:** The shared network for all services. |
| 92 | +* **Vol:** The shared volumes for data persistence. |
| 93 | +* **Service:** Each individual container with its own configuration. |
| 94 | + |
| 95 | +## Essential Compose Commands |
| 96 | + |
| 97 | +| Command | Action | |
| 98 | +| :--- | :--- | |
| 99 | +| `docker-compose up` | Build, create, and start all containers. | |
| 100 | +| `docker-compose up -d` | Run everything in the background (Detached). | |
| 101 | +| `docker-compose ps` | See the status of your entire stack. | |
| 102 | +| `docker-compose logs -f` | Watch the output from all containers at once. | |
| 103 | +| `docker-compose stop` | Stop the services (but keep the containers). | |
| 104 | +| `docker-compose down` | **The Cleanup:** Stop and REMOVE all containers and networks. | |
| 105 | + |
| 106 | +## Summary Checklist |
| 107 | +* [x] I understand that Compose is for **multi-container** apps. |
| 108 | +* [x] I know that `docker-compose.yml` uses YAML indentation. |
| 109 | +* [x] I can explain what `depends_on` does for startup order. |
| 110 | +* [x] I understand that `docker-compose down` wipes the environment clean. |
| 111 | + |
| 112 | +:::success 🎉 Docker Module Complete! |
| 113 | +Congratulations! You have moved from a simple "Hello World" to orchestrating a complex, multi-service architecture. You are now officially a **Container Pro**. |
| 114 | +::: |
0 commit comments