> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/docker/compose.md).

# Compose

#### **Docker Compose Made Simple** 🚀

Docker Compose is a tool that lets you manage multi-container applications with a single command using a YAML configuration file. Let’s break it down step-by-step:

***

#### **What is Docker Compose?**

* Docker Compose simplifies the process of running **multiple containers** that work together (e.g., a web app with a database).
* You define your services (containers) in a `docker-compose.yml` file and start/stop everything with simple commands.

***

#### **How to Use Docker Compose?**

**1️⃣ Create a `docker-compose.yml` File**

This file defines all your services, networks, and volumes in one place.

Example:

```yaml
version: '3'  # Docker Compose version

services:     # Define your services (containers)
  web:        # Service name (can be anything)
    image: nginx  # Use the Nginx image
    ports:
      - "8080:80" # Map port 8080 on your machine to port 80 in the container

  db:         # Another service for the database
    image: mysql  # Use the MySQL image
    environment:  # Set environment variables for MySQL
      MYSQL_ROOT_PASSWORD: example
```

***

**2️⃣ Start All Services**

Run this command to start all services:

```bash
docker-compose up -d
```

* **`-d`**: Runs the services in detached mode (in the background).
* This will:
  * Pull the `nginx` and `mysql` images (if not already available).
  * Create containers for `web` and `db`.
  * Automatically create a network to connect the services.

***

**3️⃣ Stop All Services**

To stop and remove containers, networks, and volumes created by Docker Compose:

```bash
docker-compose down
```

***

**4️⃣ View Running Services**

Use:

```bash
docker-compose ps
```

This shows the status of your services.

***

#### **Key Features of Docker Compose**

**🛠 Environment Variables**

Store sensitive or dynamic data in an `.env` file:

```env
MYSQL_ROOT_PASSWORD=example
```

Then reference it in your `docker-compose.yml`:

```yaml
environment:
  MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
```

***

**🛠 Volumes**

You can persist data using volumes:

```yaml
services:
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: example
    volumes:
      - dbdata:/var/lib/mysql

volumes:
  dbdata:
```

***

**🛠 Networks**

Compose automatically creates a network for all services, but you can customize it:

```yaml
services:
  web:
    image: nginx
    networks:
      - custom_network

  db:
    image: mysql
    networks:
      - custom_network

networks:
  custom_network:
```

***

#### **Commands to Know**

| Command                | Description                          |
| ---------------------- | ------------------------------------ |
| `docker-compose up`    | Start all services in the YAML file. |
| `docker-compose up -d` | Start services in detached mode.     |
| `docker-compose down`  | Stop and remove services.            |
| `docker-compose ps`    | List running services.               |
| `docker-compose logs`  | View logs for all services.          |
| `docker-compose build` | Build services from a Dockerfile.    |

***

#### **How Services Communicate**

* Containers in the same `docker-compose.yml` file can talk to each other **using their service names**.
* Example: The `web` service can connect to `db` using `db` as the hostname.

***

#### **Why Use Docker Compose?**

1. Simplifies managing multi-container apps.
2. Reproducibility: Share your `docker-compose.yml` and recreate the environment anywhere.
3. Scales easily with `docker-compose up --scale`.

***

#### **In Summary**

Docker Compose is a great tool for managing multi-container applications:

1. Define all your services in `docker-compose.yml`.
2. Start and stop everything with simple commands.
3. Use networks and volumes for communication and persistence.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/docker/compose.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
