community.docker.docker_compose_v2 module – Manage multi-container Docker applications with Docker Compose v2.
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.
New in community.docker 3.5.0
Synopsis
Uses Docker Compose v2 to manage docker services. This module requires docker-compose >= 2.0.0. and python >= 3.7
Configuration can be read from a
docker-compose.yamlfile.See the examples for more details.
Supports check mode.
Requirements
The below requirements are needed on the host that executes this module.
Python on Whales >= 0.55.0
docker-compose >= 2.0.0
python >= 3.7
Parameters
Parameter |
Comments |
|---|---|
Use with state Same as running 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 Choices:
|
|
Use with the build option to ignore the cache during the image build process. Choices:
|
|
Path to a directory containing a Required. |
|
Use with state Same as running 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 Choices:
|
|
Use with state Choices:
|
|
Remove containers for services not defined in the Compose file when not Choices:
|
|
Use with state Choices:
|
|
Use with state Choices:
|
|
Desired state of the project. Specifying Specifying Choices:
|
|
Use with state Choices:
|
Attributes
Attribute |
Support |
Description |
|---|---|---|
Support: full |
Can run in |
|
Support: none |
Will return details on what has changed (or possibly needs changing in |
Examples
# Set up a virtual environment for the module and install python on whales
- name: Set up virtual env for compose v2
ansible.builtin.pip:
name:
- python-on-whales
virtualenv: ~/ansible-compose-v2
# Ensure project directory is present
- name: Create directory if it does not exist
ansible.builtin.file:
path: uptime-kuma
state: directory
# Create the docker compose file
- name: Create compose file with content
ansible.builtin.copy:
dest: "uptime-kuma/docker-compose.yaml"
content: |
version: '3.3'
services:
uptime-kuma:
image: louislam/uptime-kuma:1
container_name: uptime-kuma
volumes:
- ./uptime-kuma-data:/app/data
ports:
- 3001:3001
restart: always
# Tear down any containers, remove images and volumes
- name: Tear down all existing containers
vars:
ansible_python_interpreter: ansible-compose-v2/bin/python
community.docker.docker_compose_v2:
project_src: uptime-kuma/docker-compose.yaml
remove_volumes: true
remove_orphans: true
remove_images: all
state: absent
# Pull the latest image and then start services
- name: Pull image and start service
vars:
ansible_python_interpreter: ansible-compose-v2/bin/python
community.docker.docker_compose_v2:
project_src: uptime-kuma/docker-compose.yaml
pull: true
state: present
# Restart services
- name: Restart service
vars:
ansible_python_interpreter: ansible-compose-v2/bin/python
community.docker.docker_compose_v2:
project_src: uptime-kuma/docker-compose.yaml
restarted: true
state: present
# Stop services
- name: Stop service
vars:
ansible_python_interpreter: ansible-compose-v2/bin/python
community.docker.docker_compose_v2:
project_src: uptime-kuma/docker-compose.yaml
stopped: true
state: present
# Start services, recreating all containers
- name: Recreate service
vars:
ansible_python_interpreter: ansible-compose-v2/bin/python
community.docker.docker_compose_v2:
project_src: uptime-kuma/docker-compose.yaml
recreate: always
state: present
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
|---|---|
List stating which compose commands were run (not the entire command itself). Returned: success Sample: |
|
If something was changed (currently, changed is (almost) always true as there is no check of current state). Returned: always Sample: |
|
In docker failures, has docker command + some description. In other failures, stringified exception is returned. In success, descriptive message of what happened. Returned: always Sample: |
|
Return code of executed docker command. Returned: failure from docker Sample: |
Collection links
Issue Tracker Repository (Sources) Submit a bug report Request a feature Communication