In ansible playbook I need to run docker-compose commands. How can I do it? I need to run command: docker-compose -f docker-compose.yml -f docker-compose.prod.yml up
Asked
Active
Viewed 4.2k times
28

Tatiana
- 576
- 1
- 6
- 23
-
1[Check this module](https://docs.ansible.com/ansible/latest/modules/docker_compose_module.html) for the details. – Moon Jun 18 '20 at 14:29
2 Answers
38
You should copy your Docker Compose files and use docker_compose
module such as:
- name: copy Docker Compose files
copy:
src: files/{{ item }}
dest: /somewhere/yourproject/{{ item }}
loop:
- docker-compose.yml
- docker-compose.prod.yml
# use files parameter to use multiple docker-compose.yml files
- name: deploy Docker Compose stack
community.docker.docker_compose:
project_src: /somewhere/yourproject
files:
- docker-compose.yml
- docker-compose.prod.yml
Edit 2023-08-22: as of today Compose v2 is not supported by Ansible, it only works with v1. There's ongoing work towards docker_compose_v2
module but it's not available yet. In the meantime you can use shell
as per @Tatiana's answer

Pierre B.
- 11,612
- 1
- 37
- 58
-
Oh, I see that to use docker_compose I need to install a lot of dependencies. Thanks for your answer! – Tatiana Jun 18 '20 at 15:31
-
-
1You mean old `community.general.docker_compose`? Just update to use recent `community.docker.docker_compose` – Pierre B. Jun 08 '22 at 08:30
-
You're maybe confusing Ansible Docker collection currently at version `3.3.1` (https://galaxy.ansible.com/community/docker?extIdCarryOver=true&sc_cid=701f2000001OH7YAAW) and Docker Compose specs currently at `3.8` (https://docs.docker.com/compose/compose-file/compose-versioning/). Ansible Docker collection (packaging compose module) version is not correlated to Docker Compose itself, and is perfectly able to run latest Docker Compose specs and binaries as of time of writing this comment :) (just tested to make sure) – Pierre B. Jan 19 '23 at 10:32
-
1It sounds like community.docker collection only works with the old EOL docker compose 1.x https://github.com/ansible-collections/community.docker/issues/216 – idolize Aug 22 '23 at 19:15
-
Indeed, from the issue you linked this MR is mentioned which seems to add `docker_compose_v2` https://github.com/ansible-collections/community.docker/pull/586. I updated answer accordingly regarding Compose v1 and will update again if a v2 module make it's way to main – Pierre B. Aug 22 '23 at 20:23
10
At the end I decided to use to use shell module. I think @pierre-b answer is more correct but anyway:
- name: Run container
become: True
shell:
cmd: "docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d"
chdir: /mydir/

Tatiana
- 576
- 1
- 6
- 23
-
4You should definitely avoid using shell when there is a module doing the job for you. Now you also have to manage idempotency (which is done automatically by the module). – Zeitounator Jun 18 '20 at 15:39
-
1Thanks, your answer is also valid but as @Zeitounator pointed it's better to use an Ansible module where possible and avoid using shell commands. Why did the module did not suit you? – Pierre B. Jun 18 '20 at 15:57
-
1Too many requirements for the module. But probably I will use it in next time, it looks cool. – Tatiana Jun 18 '20 at 16:44
-
7"Definitely" - not at all. The module relies on docker-py which is unmaintained and outdated. Don't use modules that don't keep pace / have good community support unless you're ok with staying behind in compatibility and having to rewrite later. Ansible community will eventually figure it out though. – aehlke Jun 07 '22 at 20:31
-
If I install docker-compose-plugin, or a docker-compose for my flavour of server, I do not see why I should not use this, as it is the simplest and gets the job done. – Gogol Sep 11 '22 at 21:06
-
2@aehlke, thanks for pointing this out. Apparently, the module only supports docker-compose 1x only, [here's the link](https://github.com/ansible-collections/community.docker/issues/676#issuecomment-1656146970) – Budianto IP Aug 06 '23 at 04:32