# Controlling container name with docker-compose-v2 community package

**URL:** <https://forum.ansible.com/t/controlling-container-name-with-docker-compose-v2-community-package/41286>\
**Category:** Get Help\
**Tags:** docker-compose\
**Created:** [March 24, 2025, 12:24pm UTC](https://forum.ansible.com/t/controlling-container-name-with-docker-compose-v2-community-package/41286 "2025-03-24T12:24:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erling\_Moeller](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/erling_moeller/32/7874_2.png) [@Erling\_Moeller](https://forum.ansible.com/u/Erling_Moeller)\
**Post date:** [March 24, 2025, 12:24pm UTC](https://forum.ansible.com/t/controlling-container-name-with-docker-compose-v2-community-package/41286/1 "2025-03-24T12:24:44Z")

</div>

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:

```auto
  - 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:

```auto
$ 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

```

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [March 24, 2025, 5:07pm UTC](https://forum.ansible.com/t/controlling-container-name-with-docker-compose-v2-community-package/41286/2 "2025-03-24T17:07:32Z")

</div>

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](https://stackoverflow.com/a/69519102), and some issues in the Compose GitHub repository with more information / complaints / …

---

<div class="post-metadata">

**Author:** ![Erling\_Moeller](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/erling_moeller/32/7874_2.png) [@Erling\_Moeller](https://forum.ansible.com/u/Erling_Moeller)\
**Post date:** [March 25, 2025, 12:42pm UTC](https://forum.ansible.com/t/controlling-container-name-with-docker-compose-v2-community-package/41286/3 "2025-03-25T12:42:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kristianheljas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kristianheljas/32/7702_2.png) [@kristianheljas](https://forum.ansible.com/u/kristianheljas)\
**Post date:** [March 25, 2025, 1:53pm UTC](https://forum.ansible.com/t/controlling-container-name-with-docker-compose-v2-community-package/41286/4 "2025-03-25T13:53:54Z")

</div>

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

```auto
- 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.

---

<div class="post-metadata">

**Author:** ![Erling\_Moeller](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/erling_moeller/32/7874_2.png) [@Erling\_Moeller](https://forum.ansible.com/u/Erling_Moeller)\
**Post date:** [March 26, 2025, 9:50am UTC](https://forum.ansible.com/t/controlling-container-name-with-docker-compose-v2-community-package/41286/5 "2025-03-26T09:50:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Erling\_Moeller](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/erling_moeller/32/7874_2.png) [@Erling\_Moeller](https://forum.ansible.com/u/Erling_Moeller)\
**Post date:** [March 26, 2025, 10:05am UTC](https://forum.ansible.com/t/controlling-container-name-with-docker-compose-v2-community-package/41286/6 "2025-03-26T10:05:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Erling\_Moeller](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/erling_moeller/32/7874_2.png) [@Erling\_Moeller](https://forum.ansible.com/u/Erling_Moeller)\
**Post date:** [March 26, 2025, 10:30am UTC](https://forum.ansible.com/t/controlling-container-name-with-docker-compose-v2-community-package/41286/7 "2025-03-26T10:30:22Z")

</div>

Setting compose compatibility in the playbook helped.

---

<div class="post-metadata">

**Author:** ![kristianheljas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kristianheljas/32/7702_2.png) [@kristianheljas](https://forum.ansible.com/u/kristianheljas)\
**Post date:** [March 26, 2025, 10:52am UTC](https://forum.ansible.com/t/controlling-container-name-with-docker-compose-v2-community-package/41286/8 "2025-03-26T10:52:47Z")

</div>

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

Setting it in the playbook is the correct way.
