# Compose

### Why

* Configure relationships between containers
* Save docker container run settings in easy-to-read file
* create one-liner development environment startups

Comprised of 2 separate but related things:

* YAML formatted file that describes solution options for:
  * containers
  * volumes
  * networks
* A CLI tool docker-compose used for local dev/test automation

Installed automatically on Mac and Windows with Docker Desktop, for Linux must be downloaded separately.

### Links

* [Compose on GitHub](https://github.com/docker/compose)
* [Compose documentation](https://docs.docker.com/compose/compose-file/)

### Example

```yaml
version: '3'

services:
  drupal:
    image: drupal:8-apache
    ports:
    - '8080:80'
    volumes:
    - drupal-modules:/var/www/html/modules
    - drupal-profiles:/var/www/html/profiles
    - drupal-sites:/var/www/html/sites
    - drupal-themes:/var/www/html/themes
    restart: always
  postgres:
    image: postgres:10
    environment:
      POSTGRES_PASSWORD: pgPass@1
    restart: always

volumes:
  drupal-modules:
  drupal-profiles:
  drupal-sites:
  drupal-themes:

```

Commands

To start

```yaml
docker-compose up [--build] [...options]
```

To tear down

```yaml
docker-compose down [-v] [...other options]
```

### Using compose with build

* compose can also build your images
* will build them with `docker-compose up` if not found in cache
* also rebuild with `docker-compose build`
* great for complex builds that have lots of vars or build args

Example

```yaml
version: '3'

services:
  proxy:
    build:
      context: .
      dockerfile: nginx.Dockerfile
    image: nginx-custom
    ports:
    - '80:80'
  web:
    image: httpd
    volumes:
    - ./html:/usr/local/apache2/htdocs/
```

### Restart policies

* `"no"` - never attempt to restart this container if it stops or crashes (must be in "")
* `always` - if this container stops, for any reason, always attempt to restart it
* `on-failure` - only restart if the container stops with an error code
* `unless-stopped` - always restart unless we forcibly stop it


---

# Agent Instructions: 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://notebook.iuriioapps.com/containers-and-microservices/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.
