Controlling container name with docker-compose-v2 community package

Hi

I recently upgraded ansible from an older version (I got a new work laptop). Then I found out that the old docker compose had been deprecated.

To my surprise after the upgrade my containers changed their names. underscore had been exchanged with hyphens. Is there a way to control that? I would like to have underscore all the way.

My playbooks have container definitions like this:

  - name: Start Distribute Products components (all NRT processing modes)
    docker_compose_v2:
      project_name: gpac
      scale:
        web_publisher: "{{ component_info['web_publisher']['scaling'] }}"
      definition:
        services:
          web_publisher:
            image: "{{ docker_url }}/gpac-web-publisher:{{ component_info['web_publisher']['version'] }}"
            restart: always
            volumes:
              - /data/gpac:/data

Ansible version information:

$ ansible-playbook --version
ansible-playbook [core 2.16.3]
  config file = None
  configured module search path = ['.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = .ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible-playbook
  python version = 3.12.3 (main, Feb  4 2025, 14:48:35) [GCC 13.3.0] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True

$ ls ~/.ansible/collections/ansible_collections/
community  community.docker-4.4.0.info  community.library_inventory_filtering_v1-1.0.2.info

The module is doing nothing more than docker compose up (with a few more parameters to remove ambiguities and parse the output), so if something changes, this is a result of migrating from Docker Compose v1 to Docker Compose v2, and unrelated to (and out of control of) Ansible.

A quick search on the web resulted in docker-compose container name use dash (-) instead of underscore (_) - Stack Overflow, and some issues in the Compose GitHub repository with more information / complaints / …

1 Like

So does the community docker compose v2 ansible module have this --compatibilty flag mentioned in the SO post you linked?

There doesn’t seem to be an option for --compatibilty flag but you can set the environment variable to control it:

- community.docker.docker_compose_v2:
    project_name: gpac
    ...
  environment:
    COMPOSE_COMPATIBILITY: "true"

You can also set environment at play level, which may help if you have many compose_v2 tasks.

I tried what you described setting the enviroment variable at play level like so:
ansible-playbook ./playbooks/gpac_start_broker.yml -e "target=sentinel6_ntc_test processing_mode=ntc_test COMPOSE_COMPATIBILITY=true"

However the container name is still using hyphen as separator.

Originally I thought it might be because my variable was parsed as string and not bool so I read somewhere that you could input extra-vars in json format to “force” parse as boolean like so:
ansible-playbook ./playbooks/gpac_start_broker.yml -e "target=sentinel6_ntc_test processing_mode=ntc_test" --extra-vars '{"COMPOSE_COMPATIBILITY": true}'
However the container name separator is still a hyphen.

Good ideas are more than welcome. :slight_smile:

Setting compose compatibility in the playbook helped.

What you were setting via cli is ansible variable, not enviroment variable.

Setting it in the playbook is the correct way.