community.docker.docker_compose_v2 module – Manage multi-container Docker applications with Docker Compose.
Note
This module is part of the community.docker collection (version 3.5.0).
To install it, use: ansible-galaxy collection install community.docker.
You need further requirements to be able to use this module,
see Requirements for details.
To use it in a playbook, specify: community.docker.docker_compose_v2.
Synopsis
Uses Docker Compose to start and shutdown services.
Swarm mode is not supported (thus secrets and configs are not supported).
Configuration can be read from a Compose file or inline using the definition option.
See the examples for more details.
Requirements
The below requirements are needed on the host that executes this module.
docker-compose >= 2.0.0
PyYAML
Parameters
Parameter |
Comments |
|---|---|
Use with state Equivalent to Images will only be rebuilt if Docker detects a change in the Dockerfile or build directory contents. If an existing image is replaced, services using the image will be recreated unless recreate is Choices:
|
|
Compose file describing one or more services, networks and volumes. Mutually exclusive with files. |
|
When state is When false, equivalent to When state is When true, equivalent to Choices:
|
|
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, If the value is not specified in the task, the value of environment variable Default: |
|
By default environment files are loaded from a env_file can be used to specify the path of a custom environment file instead. The path is relative to the project_src directory. Equivalent to |
|
List of Compose files. Files are passed to docker-compose in the order given. Equivalent to |
|
Use with the build option to ignore the cache during the image build process. Equivalent to Choices:
|
|
List of profiles to enable when starting services. Equivalent to |
|
Provide a project name. Equivalent to |
|
Path to the root directory of the Compose project. Required when definition is provided. |
|
Use with state Equivalent to When a new image is pulled, services using the image will be recreated unless recreate is Choices:
|
|
By default containers will be recreated when their configuration differs from the service definition. Setting to Setting to When set to When set to Choices:
|
|
Use with state Equivalent to Choices:
|
|
Remove containers for services not defined in the Compose file. Equivalent to Choices:
|
|
Use with state Equivalent to Choices:
|
|
When state is If empty, which is the default, the operation will be performed on all services defined in the Compose file (or inline definition). |
|
Desired state of the project. Specifying Specifying Specifying Specifying Specifying Specifying Choices:
|
|
Timeout in seconds for container shutdown when attached or when containers are already running. By default |
Attributes
Attribute |
Support |
Description |
|---|---|---|
Support: none |
Check mode is not supported because the `docker-compose` CLI doesn’t allow it. |
|
Support: none |
Diff mode is not supported. |
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: false
tasks:
- name: Tear down existing services
community.docker.docker_compose:
project_src: flask
state: absent
- name: Create and start services
community.docker.docker_compose:
project_src: flask
register: output
- ansible.builtin.debug:
var: output
- name: Run `docker-compose up` again
community.docker.docker_compose:
project_src: flask
build: false
register: output
- ansible.builtin.debug:
var: output
- ansible.builtin.assert:
that: not output.changed
- name: Stop all services
community.docker.docker_compose:
project_src: flask
build: false
stopped: true
register: output
- ansible.builtin.debug:
var: output
- ansible.builtin.assert:
that:
- "'stopped' in containers['flask_web_1'] | default([])"
- "'stopped' in containers['flask_db_1'] | default([])"
- name: Restart services
community.docker.docker_compose:
project_src: flask
build: false
restarted: true
register: output
- ansible.builtin.debug:
var: output
- ansible.builtin.assert:
that:
- "'started' in containers['flask_web_1'] | default([])"
- "'started' in containers['flask_db_1'] | default([])"
- name: Run with inline Compose file version 2
# https://docs.docker.com/compose/compose-file/compose-file-v2/
hosts: localhost
gather_facts: false
tasks:
- community.docker.docker_compose:
project_src: flask
state: absent
- community.docker.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
- ansible.builtin.debug:
var: output
- ansible.builtin.assert:
that:
- "'started' in containers['flask_web_1'] | default([])"
- "'started' in containers['flask_db_1'] | default([])"
- name: Run with inline Compose file version 1
# https://docs.docker.com/compose/compose-file/compose-file-v1/
hosts: localhost
gather_facts: false
tasks:
- community.docker.docker_compose:
project_src: flask
state: absent
- community.docker.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
- ansible.builtin.debug:
var: output
- ansible.builtin.assert:
that:
- "'started' in containers['flask_web_1'] | default([])"
- "'started' in containers['flask_db_1'] | default([])"
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
|---|---|
A dictionary mapping containers to the various status they went through during A dict of lists, where dict keys are containers names and lists elements are statuses. Returned: always, unless when Sample: |
|
A dictionary mapping images to the various status they went through during A dict of lists, where dict keys are services names and lists elements are statuses.
Returned: always, unless when Sample: |
|
A dictionary mapping networks to the various status they went through during A dict of lists, where dict keys are networks names and lists elements are statuses. Returned: always, unless when Sample: |
|
The stderr from docker-compose. Returned: always, unless when |
|
The stdout from docker-compose. Returned: always, unless when |
|
A dictionary mapping volumes to the various status they went through during A dict of lists, where dict keys are volumes names and lists elements are statuses. Returned: always, unless when Sample: |
Collection links
Issue Tracker Repository (Sources) Submit a bug report Request a feature Communication