# docker_compose – Manage multi-container Docker applications with Docker Compose

- Synopsis
- Requirements
- Parameters
- Notes
- Examples
- Return Values
- Status

## Synopsis

- Uses Docker Compose to start, shutdown and scale services.
- Works with compose versions 1 and 2.
- Configuration can be read from a `docker-compose.yml` or `docker-compose.yaml` file or inline using the definition option.
- See the examples for more details.
- Supports check mode.
- This module was called `docker_service` before Ansible 2.8. The usage did not change.

Aliases: docker_service

## Requirements

The below requirements are needed on the host that executes this module.

- Docker API >= 1.20
- Docker SDK for Python: Please note that the docker-py Python module has been superseded by docker (see here for details). For Python 2.6, `docker-py` must be used. Otherwise, it is recommended to install the `docker` Python module. Note that both modules should not be installed at the same time. Also note that when both modules are installed and one of them is uninstalled, the other might no longer function and a reinstall of it is required.
- Docker SDK for Python >= 1.8.0 (use docker-py for Python 2.6)
- PyYAML >= 3.11
- docker-compose >= 1.7.0

## Parameters

Parameter | Choices/Defaults | Comments
--- | --- | ---

api_version
string

Default:
"auto"

The version of the Docker API running on the Docker Host.
Defaults to the latest version of the API supported by Docker SDK for Python and the docker daemon.
If the value is not specified in the task, the value of environment variable `DOCKER_API_VERSION` will be used instead. If the environment variable is not set, the default value will be used.
aliases: docker_api_version

build
boolean

Choices:
- no ←
- yes

Use with state `present` to always build images prior to starting the application.
Same as running `docker-compose build` with the pull option.
Images will only be rebuilt if Docker detects a change in the Dockerfile or build directory contents.
Use the nocache option to ignore the image cache when performing the build.
If an existing image is replaced, services using the image will be recreated unless recreate is `never`.

ca_cert path |  | Use a CA certificate when performing server verification by providing the path to a CA certificate file. If the value is not specified in the task and the environment variable `DOCKER_CERT_PATH` is set, the file `ca.pem` from the directory specified in the environment variable `DOCKER_CERT_PATH` will be used. aliases: tls_ca_cert, cacert_path
client_cert path |  | Path to the client's TLS certificate file. If the value is not specified in the task and the environment variable `DOCKER_CERT_PATH` is set, the file `cert.pem` from the directory specified in the environment variable `DOCKER_CERT_PATH` will be used. aliases: tls_client_cert, cert_path
client_key path |  | Path to the client's TLS key file. If the value is not specified in the task and the environment variable `DOCKER_CERT_PATH` is set, the file `key.pem` from the directory specified in the environment variable `DOCKER_CERT_PATH` will be used. aliases: tls_client_key, key_path

debug
boolean

Choices:
- no ←
- yes

Debug mode

definition dictionary |  | Compose file describing one or more services, networks and volumes. Mutually exclusive with project_src and files.

dependencies
boolean

Choices:
- no
- yes ←

When state is `present` specify whether or not to include linked services.

docker_host
string

Default:
"unix://var/run/docker.sock"

The URL or Unix socket path used to connect to the Docker API. To connect to a remote host, provide the TCP connection string. For example, `tcp://192.0.2.23:2376`. If TLS is used to encrypt the connection, the module will automatically replace `tcp` in the connection URL with `https`.
If the value is not specified in the task, the value of environment variable `DOCKER_HOST` will be used instead. If the environment variable is not set, the default value will be used.
aliases: docker_url

files list / elements=path |  | List of Compose file names relative to project_src. Overrides `docker-compose.yml` or `docker-compose.yaml`. Files are loaded and merged in the order given.

hostname_check
boolean

Choices:
- no ←
- yes

Whether or not to check the Docker daemon's hostname against the name provided in the client certificate.

nocache
boolean

Choices:
- no ←
- yes

Use with the build option to ignore the cache during the image build process.

project_name string |  | Provide a project name. If not provided, the project name is taken from the basename of project_src. Required when definition is provided.
project_src path |  | Path to a directory containing a `docker-compose.yml` or `docker-compose.yaml` file. Mutually exclusive with definition. Required when no definition is provided.

pull
boolean

Choices:
- no ←
- yes

Use with state `present` to always pull images prior to starting the application.
Same as running `docker-compose pull`.
When a new image is pulled, services using the image will be recreated unless recreate is `never`.

recreate
string

Choices:
- always
- never
- smart ←

By default containers will be recreated when their configuration differs from the service definition.
Setting to `never` ignores configuration differences and leaves existing containers unchanged.
Setting to `always` forces recreation of all existing containers.

remove_images
string

Choices:
- all
- local

Use with state `absent` to remove all images or only local images.

remove_orphans
boolean

Choices:
- no ←
- yes

Remove containers for services not defined in the Compose file.

remove_volumes
boolean

Choices:
- no ←
- yes

Use with state `absent` to remove data volumes.

restarted
boolean

Choices:
- no ←
- yes

Use with state `present` to restart all containers defined in the Compose file.
If services is defined, only the containers listed there will be restarted.

scale dictionary |  | When state is `present` scale services. Provide a dictionary of key/value pairs where the key is the name of the service and the value is an integer count for the number of containers.
services list / elements=string |  | When state is `present` run `docker-compose up` resp. `docker-compose stop` (with stopped) resp. `docker-compose restart` (with restarted) on a subset of services. If empty, which is the default, the operation will be performed on all services defined in the Compose file (or inline definition).
ssl_version string |  | Provide a valid SSL version number. Default value determined by ssl.py module. If the value is not specified in the task, the value of environment variable `DOCKER_SSL_VERSION` will be used instead.

state
string

Choices:
- absent
- present ←

Desired state of the project.
Specifying `present` is the same as running `docker-compose up` resp. `docker-compose stop` (with stopped) resp. `docker-compose restart` (with restarted).
Specifying `absent` is the same as running `docker-compose down`.

stopped
boolean

Choices:
- no ←
- yes

Use with state `present` to stop all containers defined in the Compose file.
If services is defined, only the containers listed there will be stopped.

timeout integer | Default: 10 | timeout in seconds for container shutdown when attached or when containers are already running.

tls
boolean

Choices:
- no ←
- yes

Secure the connection to the API by using TLS without verifying the authenticity of the Docker host server. Note that if validate_certs is set to `yes` as well, it will take precedence.
If the value is not specified in the task, the value of environment variable `DOCKER_TLS` will be used instead. If the environment variable is not set, the default value will be used.

tls_hostname string | Default: "localhost" | When verifying the authenticity of the Docker Host server, provide the expected name of the server. If the value is not specified in the task, the value of environment variable `DOCKER_TLS_HOSTNAME` will be used instead. If the environment variable is not set, the default value will be used.

validate_certs
boolean

Choices:
- no ←
- yes

Secure the connection to the API by using TLS and verifying the authenticity of the Docker host server.
If the value is not specified in the task, the value of environment variable `DOCKER_TLS_VERIFY` will be used instead. If the environment variable is not set, the default value will be used.
aliases: tls_verify

## Notes

Note

- Connect to the Docker daemon by providing parameters with each task or by defining environment variables. You can define `DOCKER_HOST`, `DOCKER_TLS_HOSTNAME`, `DOCKER_API_VERSION`, `DOCKER_CERT_PATH`, `DOCKER_SSL_VERSION`, `DOCKER_TLS`, `DOCKER_TLS_VERIFY` and `DOCKER_TIMEOUT`. If you are using docker machine, run the script shipped with the product that sets up the environment. It will set these variables for you. See https://docs.docker.com/machine/reference/env/ for more details.
- When connecting to Docker daemon with TLS, you might need to install additional Python packages. For the Docker SDK for Python, version 2.4 or newer, this can be done by installing `docker[tls]` with pip.
- Note that the Docker SDK for Python only allows to specify the path to the Docker configuration for very few functions. In general, it will use `$HOME/.docker/config.json` if the `DOCKER_CONFIG` environment variable is not specified, and use `$DOCKER_CONFIG/config.json` otherwise.

## Examples

    # Examples use the django example at https://docs.docker.com/compose/django. Follow it to create the
    # flask directory

    - name: Run using a project directory
      hosts: localhost
      gather_facts: no
      tasks:
        - name: Tear down existing services
          docker_compose:
            project_src: flask
            state: absent

        - name: Create and start services
          docker_compose:
            project_src: flask
          register: output

        - debug:
            var: output

        - name: Run `docker-compose up` again
          docker_compose:
            project_src: flask
            build: no
          register: output

        - debug:
            var: output

        - assert:
            that: "not output.changed "

        - name: Stop all services
          docker_compose:
            project_src: flask
            build: no
            stopped: yes
          register: output

        - debug:
            var: output

        - assert:
            that:
              - "not web.flask_web_1.state.running"
              - "not db.flask_db_1.state.running"

        - name: Restart services
          docker_compose:
            project_src: flask
            build: no
            restarted: yes
          register: output

        - debug:
            var: output

        - assert:
            that:
              - "web.flask_web_1.state.running"
              - "db.flask_db_1.state.running"

    - name: Scale the web service to 2
      hosts: localhost
      gather_facts: no
      tasks:
        - docker_compose:
            project_src: flask
            scale:
              web: 2
          register: output

        - debug:
            var: output

    - name: Run with inline v2 compose
      hosts: localhost
      gather_facts: no
      tasks:
        - docker_compose:
            project_src: flask
            state: absent

        - docker_compose:
            project_name: flask
            definition:
              version: '2'
              services:
                db:
                  image: postgres
                web:
                  build: "{{ playbook_dir }}/flask"
                  command: "python manage.py runserver 0.0.0.0:8000"
                  volumes:
                    - "{{ playbook_dir }}/flask:/code"
                  ports:
                    - "8000:8000"
                  depends_on:
                    - db
          register: output

        - debug:
            var: output

        - assert:
            that:
              - "web.flask_web_1.state.running"
              - "db.flask_db_1.state.running"

    - name: Run with inline v1 compose
      hosts: localhost
      gather_facts: no
      tasks:
        - docker_compose:
            project_src: flask
            state: absent

        - docker_compose:
            project_name: flask
            definition:
                db:
                  image: postgres
                web:
                  build: "{{ playbook_dir }}/flask"
                  command: "python manage.py runserver 0.0.0.0:8000"
                  volumes:
                    - "{{ playbook_dir }}/flask:/code"
                  ports:
                    - "8000:8000"
                  links:
                    - db
          register: output

        - debug:
            var: output

        - assert:
            that:
              - "web.flask_web_1.state.running"
              - "db.flask_db_1.state.running"

## Return Values

Common return values are documented here, the following are the fields unique to this module:

Key | Returned | Description
--- | --- | ---
actions complex | when in check mode or debug is `yes` | Provides the actions to be taken on each service as determined by compose.
| service_name complex | always | Name of the service.
|  | action list / elements=string | always | A descriptive name of the action to be performed on the service's containers.
|  |  | id string | always | the container's long ID
|  |  | name string | always | the container's name
|  |  | short_id string | always | the container's short ID
|  | built_image complex | on image build | Provides image details when a new image is built for the service.
|  |  | id string | always | image hash
|  |  | name string | always | name of the image
|  | pulled_image complex | on image pull | Provides image details when a new image is pulled for the service.
|  |  | id string | always | image hash
|  |  | name string | always | name of the image
services complex | success | A dictionary mapping the service's name to a dictionary of containers. Note that facts are part of the registered vars since Ansible 2.8. For compatibility reasons, the facts are also accessible directly. The service's name is the variable with which the container dictionary can be accessed. Note that the returned facts will be removed in Ansible 2.12.
| container_name complex | success | Name of the container. Format is `project_service_#`.
|  | cmd list / elements=string | success | One or more commands to be executed in the container.
|  | image string | success | Name of the image from which the container was built.
|  | labels dictionary | success | Meta data assigned to the container.
|  | networks list / elements=dictionary | success | Contains a dictionary for each network to which the container is a member.
|  |  | aliases list / elements=string | success | Aliases assigned to the container by the network.
|  |  | globalIPv6 string | success | IPv6 address assigned to the container.
|  |  | globalIPv6PrefixLen integer | success | IPv6 subnet length.
|  |  | IPAddress string | success | The IP address assigned to the container.
|  |  | IPPrefixLen integer | success | Number of bits used by the subnet.
|  |  | links list / elements=string | success | List of container names to which this container is linked.
|  |  | macAddress string | success | Mac Address assigned to the virtual NIC.
|  | state dictionary | success | Information regarding the current disposition of the container.
|  |  | running boolean | success | Whether or not the container is up with a running process.
|  |  | status string | success | Description of the running state.

## Status

- This module is not guaranteed to have a backwards compatible interface. [preview]
- This module is maintained by the Ansible Community. [community]

### Authors

- Chris Houseknecht (@chouseknecht)

Hint

If you notice any issues in this documentation, you can edit this document to improve it.