# docker compose

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you define a multi-container application in a single file, then spin up your application with a single command. Here's a comprehensive reference for Docker Compose:

#### **Docker Compose Basics:**

**1. Compose File Format:**

* Docker Compose files are written in YAML. The default file name is `docker-compose.yml`, but you can use a different filename with the `-f` option.

**2. Service Definitions:**

* Each service in a Compose file is defined under the `services` key. A service typically corresponds to a container.

```yaml
services:
  web:
    image: nginx:latest
```

**3. Build Configuration:**

* You can specify build configurations for services, allowing you to build an image from a Dockerfile.

```yaml
services:
  web:
    build:
      context: ./path/to/build-context
      dockerfile: Dockerfile.dev
```

**4. Container Naming:**

* By default, Docker Compose generates container names based on the project name and service name. You can customize container names using the `container_name` field.

```yaml
services:
  web:
    container_name: my-nginx
```

**5. Environment Variables:**

* Use the `environment` key to set environment variables for a service.

```yaml
services:
  web:
    environment:
      - MYSQL_ROOT_PASSWORD=mysecretpassword
      - MYSQL_DATABASE=mydatabase
```

**6. Volumes:**

* Define volumes under the `volumes` key to persist data.

```yaml
services:
  web:
    volumes:
      - /path/on/host:/path/in/container
```

**7. Ports:**

* Use the `ports` key to map ports from the container to the host.

```yaml
services:
  web:
    ports:
      - "8080:80"
```

**8. Networks:**

* Create custom networks for your services.

```yaml
services:
  web:
    networks:
      - frontend
networks:
  frontend:
```

**9. Depends On:**

* Specify dependencies between services.

```yaml
services:
  web:
    depends_on:
      - db
```

**10. Command Override:**

* Override the default command for a service.

```yaml
services:
  web:
    command: ["nginx", "-g", "daemon off;"]
```

**11. Links:**

* Define links between services. Note: The use of links is discouraged in favor of user-defined networks.

```yaml
services:
  web:
    links:
      - db
```

**12. Logging:**

* Control logging options.

```yaml
services:
  web:
    logging:
      driver: "json-file"
      options:
        max-size: "200k"
        max-file: "10"
```

**13. Scale Services:**

* Use the `scale` command to create multiple instances of a service.

```bash
docker-compose up --scale web=3
```

#### **Example Docker Compose File:**

```yaml
version: "3"
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
  db:
    image: postgres:latest
    environment:
      POSTGRES_PASSWORD: example
```

#### **Docker Compose Commands:**

**1. docker-compose up:**

* Build and start services defined in the Compose file.

```bash
docker-compose up
```

**2. docker-compose down:**

* Stop and remove containers, networks, and volumes.

```bash
docker-compose down
```

**3. docker-compose build:**

* Build services defined in the Compose file.

```bash
docker-compose build
```

**4. docker-compose ps:**

* List running containers.

```bash
docker-compose ps
```

**5. docker-compose logs:**

* View output from containers.

```bash
docker-compose logs
```

**6. docker-compose exec:**

* Execute commands in a running container.

```bash
docker-compose exec service_name command
```

**7. docker-compose config:**

* Validate and view the Compose file configuration.

```bash
docker-compose config
```

**8. docker-compose pause/unpause:**

* Pause or unpause services.

```bash
docker-compose pause
docker-compose unpause
```

These are the fundamental concepts and commands for using Docker Compose. Docker Compose simplifies the management of multi-container Docker applications and is especially useful for development, testing, and staging environments.
