# Best Practices - Docker Compose Up vs Ansible Container Module

**URL:** <https://forum.ansible.com/t/best-practices-docker-compose-up-vs-ansible-container-module/1902>\
**Category:** Project Discussions\
**Tags:** playbook, docker, collections\
**Created:** [October 24, 2023, 10:08am UTC](https://forum.ansible.com/t/best-practices-docker-compose-up-vs-ansible-container-module/1902 "2023-10-24T10:08:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Con1924](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/con1924/32/1303_2.png) [@Con1924](https://forum.ansible.com/u/Con1924)\
**Post date:** [October 24, 2023, 10:08am UTC](https://forum.ansible.com/t/best-practices-docker-compose-up-vs-ansible-container-module/1902/1 "2023-10-24T10:08:08Z")

</div>

Hi,

Looking for some best practice advice. I am deploying multiple docker compose stacks wit a few containers. Grafana, influxdb, nodered, mqtt ect.

Is it generally considered better to add a docker compose file to my role and then have a task docker composes up. Or should I scrap the compose file and define all my containers in task? I’m not sure which is considered ‘better’. The main advantage I see of using compose is that I can reuse it else where.

On a side note. After my containers are running, I have to add my traefik container to the network that was created in my compose. I’m also wanting to take advantage of docker namespaces

Maybe I should be using the community.docker.docker\_compose module as I am currently copying files to the target and using a command for " docker-compose --project-name xyz --file xyz.yaml --env-file xyz.env up -d"

Addition: Reading the docs for community.docker.docker\_compose it only supports Docker compose V1.x, which is deprecated

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [October 24, 2023, 1:54pm UTC](https://forum.ansible.com/t/best-practices-docker-compose-up-vs-ansible-container-module/1902/2 "2023-10-24T13:54:25Z")

</div>

For the config, I’d suggest templating all the compose configuration files using a template something like this:

```yaml
# {{ ansible_managed }}

{{ docker_compose_config | to_nice_yaml }}

# vim: syntax=yaml

```

And then you can simply use YAML to define the content of each file.

---

<div class="post-metadata">

**Author:** ![Con1924](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/con1924/32/1303_2.png) [@Con1924](https://forum.ansible.com/u/Con1924)\
**Post date:** [October 24, 2023, 1:59pm UTC](https://forum.ansible.com/t/best-practices-docker-compose-up-vs-ansible-container-module/1902/3 "2023-10-24T13:59:08Z")

</div>

Do you mean use docker vars in my compose and pass ansible vars into them?

Or do you mean write my config in yaml and use a template to convert it to docker compose? or visa versa?

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [October 24, 2023, 2:12pm UTC](https://forum.ansible.com/t/best-practices-docker-compose-up-vs-ansible-container-module/1902/4 "2023-10-24T14:12:23Z")

</div>

> [@Con1924](#):
>
> do you mean write my config in yaml and use a template to convert it to docker compose

Yes, exactly, of course there are many way it could be done but I’d do it this way.

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [October 25, 2023, 4:35pm UTC](https://forum.ansible.com/t/best-practices-docker-compose-up-vs-ansible-container-module/1902/5 "2023-10-25T16:35:06Z")

</div>

Hi,

> Reading the docs for community.docker.docker\_compose it only supports Docker compose V1.x, which is deprecated

Just so you know, there is a [PR draft](https://github.com/ansible-collections/community.docker/pull/586) on compose v2 support for community.docker collection, though it’s kinda stale as of now.

As for how I deploy docker containers with Ansible, I currently use community.docker.(network|volume|container) in my roles / playbook. It lacks a few features (like `depends_on`), but it works fine overall, and syntax looks a lot like compose.

But yeah, the compose file template option followed by a task running `docker compose up...` is an equally good idea IMO.

---

<div class="post-metadata">

**Author:** ![gotmax23](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/gotmax23/32/106_2.png) [@gotmax23](https://forum.ansible.com/u/gotmax23)\
**Post date:** [October 27, 2023, 3:05am UTC](https://forum.ansible.com/t/best-practices-docker-compose-up-vs-ansible-container-module/1902/6 "2023-10-27T03:05:28Z")

</div>

I do indeed like @chris’s idea of templating a docker-compose file like that. Creative! The other approach would be to store the final docker-compose.yml in `files/` and just `copy` that directly.

> [@ptn](#):
>
> Just so you know, there is a [PR draft](https://github.com/ansible-collections/community.docker/pull/586) on compose v2 support for community.docker collection, though it’s kinda stale as of now.

Yeah, right. There have been a couple of attempts to add a new `docker_compose_v2` module, but unfortunately, none of them panned out. The current `docker_compose` module depends on the unmaintained Python docker-compose v1. docker-compose v2 is written in Go as a Docker CLI plugin.

I still use the `docker_compose` module due to legacy reasons, but I’d lean towards the more native `docker_container` and friends if you can. `docker_container` and the other modules interact with the Docker API directly as opposed to the deprecated docker-compose v1 Python innerworkings.

---

<div class="post-metadata">

**Author:** ![aviellg](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/aviellg/32/2977_2.png) [@aviellg](https://forum.ansible.com/u/aviellg)\
**Post date:** [April 23, 2024, 10:36pm UTC](https://forum.ansible.com/t/best-practices-docker-compose-up-vs-ansible-container-module/1902/7 "2024-04-23T22:36:49Z")

</div>

i know the post is finished already , do any of you have examples of complete project with the different ways to use docker compose with templates?
