Best Practices - Docker Compose Up vs Ansible Container Module

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

2 Likes

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

# {{ 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.

3 Likes

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?

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

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

2 Likes

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.

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.

5 Likes

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?