Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run docker-compose commands with ansible?

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

like image 932
Tatiana Avatar asked Jun 18 '20 14:06

Tatiana


People also ask

Does Ansible work with Docker?

As mentioned, you can use Ansible to automate Docker and to build and deploy Docker containers. First, you'll need to have Docker SDK for Python installed.


2 Answers

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
  docker_compose:
    project_src: /somewhere/yourproject
    files:
    - docker-compose.yml
    - docker-compose.prod.yml
like image 118
Pierre B. Avatar answered Oct 03 '22 18:10

Pierre B.


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/
like image 30
Tatiana Avatar answered Oct 03 '22 17:10

Tatiana