community.docker.docker_compose_v2 module – Manage multi-container Docker applications with Docker Compose CLI plugin

Note

This module is part of the community.docker collection (version 3.13.1).

You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

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.6.0

Synopsis

  • Uses Docker Compose to start or shutdown services.

Requirements

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

  • Docker CLI with Docker compose plugin 2.18.0 or later

  • PyYAML if definition is used

Parameters

Parameter

Comments

api_version

aliases: docker_api_version

string

The version of the Docker API running on the Docker Host.

Defaults to the latest version of the API supported by this collection 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.

Default: "auto"

build

string

Whether to build images before starting containers. This is used when docker compose up is run.

always always builds before starting containers. This is equivalent to the --build option of docker compose up.

never never builds before starting containers. This is equivalent to the --no-build option of docker compose up.

policy uses the policy as defined in the Compose file.

Choices:

  • "always"

  • "never"

  • "policy" ← (default)

ca_path

aliases: ca_cert, tls_ca_cert, cacert_path

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.

check_files_existing

boolean

added in community.docker 3.9.0

If set to false, the module will not check whether one of the files compose.yaml, compose.yml, docker-compose.yaml, or docker-compose.yml exists in project_src if files is not provided.

This can be useful if environment files with COMPOSE_FILE are used to configure a different filename. The module currently does not check for COMPOSE_FILE in environment files or the current environment.

Choices:

  • false

  • true ← (default)

cli_context

string

The Docker CLI context to use.

Mutually exclusive with docker_host.

client_cert

aliases: tls_client_cert, cert_path

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.

client_key

aliases: tls_client_key, key_path

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.

definition

dictionary

added in community.docker 3.9.0

Compose file describing one or more services, networks and volumes.

Mutually exclusive with project_src and files. One of project_src and definition must be provided.

If provided, PyYAML must be available to this module, and project_name must be specified.

Note that a temporary directory will be created and deleted afterwards when using this option.

dependencies

boolean

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

Choices:

  • false

  • true ← (default)

docker_cli

path

Path to the Docker CLI. If not provided, will search for Docker CLI on the PATH.

docker_host

aliases: docker_url

string

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.

Mutually exclusive with cli_context. If neither docker_host nor cli_context are provided, the value unix:///var/run/docker.sock is used.

env_files

list / elements=path

By default environment files are loaded from a .env file located directly under the project_src directory.

env_files can be used to specify the path of one or multiple custom environment files instead.

The path is relative to the project_src directory.

files

list / elements=path

added in community.docker 3.7.0

List of Compose file names relative to project_src to be used instead of the main Compose file (compose.yml, compose.yaml, docker-compose.yml, or docker-compose.yaml).

Files are loaded and merged in the order given.

Mutually exclusive with definition.

profiles

list / elements=string

List of profiles to enable when starting services.

Equivalent to docker compose --profile.

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 Compose file (compose.yml, compose.yaml, docker-compose.yml, or docker-compose.yaml).

If files is provided, will look for these files in this directory instead.

Mutually exclusive with definition. One of project_src and definition must be provided.

pull

string

Whether to pull images before running. This is used when docker compose up is run.

always ensures that the images are always pulled, even when already present on the Docker daemon.

missing only pulls them when they are not present on the Docker daemon.

never never pulls images. If they are not present, the module will fail when trying to create the containers that need them.

policy use the Compose file’s pull_policy defined for the service to figure out what to do.

Choices:

  • "always"

  • "missing"

  • "never"

  • "policy" ← (default)

recreate

string

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.

Choices:

  • "always"

  • "never"

  • "auto" ← (default)

remove_images

string

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

Choices:

  • "all"

  • "local"

remove_orphans

boolean

Remove containers for services not defined in the Compose file.

Choices:

  • false ← (default)

  • true

remove_volumes

boolean

Use with state=absent to remove data volumes.

Choices:

  • false ← (default)

  • true

scale

dictionary

added in community.docker 3.7.0

Define how to scale services when running docker compose up.

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

Specifies a subset of services to be targeted.

state

string

Desired state of the project.

present is equivalent to running docker compose up.

stopped is equivalent to running docker compose stop.

absent is equivalent to running docker compose down.

restarted is equivalent to running docker compose restart.

Choices:

  • "absent"

  • "stopped"

  • "restarted"

  • "present" ← (default)

timeout

integer

Timeout in seconds for container shutdown when attached or when containers are already running.

tls

boolean

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 true 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.

Choices:

  • false ← (default)

  • true

tls_hostname

string

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

aliases: tls_verify

boolean

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.

Choices:

  • false ← (default)

  • true

wait

boolean

added in community.docker 3.8.0

When running docker compose up, pass --wait to wait for services to be running/healthy.

A timeout can be set with the wait_timeout option.

Choices:

  • false ← (default)

  • true

wait_timeout

integer

added in community.docker 3.8.0

When wait=true, wait at most this amount of seconds.

Attributes

Attribute

Support

Description

action_group

Action groups: community.docker.docker, docker

Use group/docker or group/community.docker.docker in module_defaults to set defaults for this module.

check_mode

Support: full

In check mode, pulling the image does not result in a changed result.

Can run in check_mode and return changed status prediction without modifying target.

diff_mode

Support: none

Will return details on what has changed (or possibly needs changing in check_mode), when in diff mode.

Notes

Note

  • The Docker compose CLI plugin has no stable output format (see for example https://github.com/docker/compose/issues/10872), and for the main operations also no machine friendly output format. The module tries to accomodate this with various version-dependent behavior adjustments and with testing older and newer versions of the Docker compose CLI plugin. Currently the module is tested with multiple plugin versions between 2.18.1 and 2.23.3. The exact list of plugin versions will change over time. New releases of the Docker compose CLI plugin can break this module at any time.

  • 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_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.

  • This module does not use the Docker SDK for Python to communicate with the Docker daemon. It directly calls the Docker CLI program.

See Also

See also

community.docker.docker_compose

Manage multi-container Docker applications with Docker Compose V1.

community.docker.docker_compose_v2_pull

Pull a Docker compose project.

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_v2:
        project_src: flask
        state: absent

    - name: Create and start services
      community.docker.docker_compose_v2:
        project_src: flask
      register: output

    - name: Show results
      ansible.builtin.debug:
        var: output

    - name: Run `docker compose up` again
      community.docker.docker_compose_v2:
        project_src: flask
      register: output

    - name: Show results
      ansible.builtin.debug:
        var: output

    - ansible.builtin.assert:
        that: not output.changed

    - name: Stop all services
      community.docker.docker_compose_v2:
        project_src: flask
        state: stopped
      register: output

    - name: Show results
      ansible.builtin.debug:
        var: output

    - name: Verify that web and db services are not running
      ansible.builtin.assert:
        that:
          - web_container.State != 'running'
          - db_container.State != 'running'
      vars:
        web_container: >-
          {{ output.containers | selectattr("Service", "equalto", "web") | first }}
        db_container: >-
          {{ output.containers | selectattr("Service", "equalto", "db") | first }}

    - name: Restart services
      community.docker.docker_compose_v2:
        project_src: flask
        state: restarted
      register: output

    - name: Show results
      ansible.builtin.debug:
        var: output

    - name: Verify that web and db services are running
      ansible.builtin.assert:
        that:
          - web_container.State == 'running'
          - db_container.State == 'running'
      vars:
        web_container: >-
          {{ output.containers | selectattr("Service", "equalto", "web") | first }}
        db_container: >-
          {{ output.containers | selectattr("Service", "equalto", "db") | first }}

Return Values

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

Key

Description

actions

list / elements=dictionary

A list of actions that have been applied.

Returned: success

id

string

The ID of the resource that was changed.

Returned: success

Sample: "container"

status

string

The status change that happened.

Returned: success

Can only return:

  • "Starting"

  • "Exiting"

  • "Restarting"

  • "Creating"

  • "Stopping"

  • "Killing"

  • "Removing"

  • "Recreating"

  • "Pulling"

  • "Building"

Sample: "Creating"

what

string

What kind of resource was changed.

Returned: success

Can only return:

  • "container"

  • "image"

  • "network"

  • "service"

  • "unknown"

  • "volume"

Sample: "container"

containers

list / elements=dictionary

A list of containers associated to the service.

Returned: success

Command

any

The container’s command.

Returned: success

CreatedAt

string

The timestamp when the container was created.

Returned: success

Sample: "2024-01-02 12:20:41 +0100 CET"

ExitCode

integer

The container’s exit code.

Returned: success

Health

any

The container’s health check.

Returned: success

ID

string

The container’s ID.

Returned: success

Sample: "44a7d607219a60b7db0a4817fb3205dce46e91df2cb4b78a6100b6e27b0d3135"

Image

string

The container’s image.

Returned: success

Labels

dictionary

Labels for this container.

Returned: success

LocalVolumes

string

The local volumes count.

Returned: success

Mounts

string

Mounts.

Returned: success

Name

string

The container’s primary name.

Returned: success

Names

list / elements=string

List of names of the container.

Returned: success

Networks

list / elements=string

List of networks attached to this container.

Returned: success

Ports

string

List of port assignments as a string.

Returned: success

Publishers

list / elements=dictionary

List of port assigments.

Returned: success

Protocol

string

The protocol.

Returned: success

Can only return:

  • "tcp"

  • "udp"

PublishedPort

integer

The port that is published.

Returned: success

TargetPort

integer

The container’s port the published port maps to.

Returned: success

URL

string

Interface the port is bound to.

Returned: success

RunningFor

string

Amount of time the container runs.

Returned: success

Service

string

The name of the service.

Returned: success

Size

string

The container’s size.

Returned: success

Sample: "0B"

State

string

The container’s state.

Returned: success

Sample: "running"

Status

string

The container’s status.

Returned: success

Sample: "Up About a minute"

images

list / elements=dictionary

A list of images associated to the service.

Returned: success

ContainerName

string

Name of the conainer this image is used by.

Returned: success

ID

string

The image’s ID.

Returned: success

Sample: "sha256:c8bccc0af9571ec0d006a43acb5a8d08c4ce42b6cc7194dd6eb167976f501ef1"

Repository

string

The repository where this image belongs to.

Returned: success

Size

integer

The image’s size in bytes.

Returned: success

Tag

string

The tag of the image.

Returned: success

Authors

  • Felix Fontein (@felixfontein)