Skip to content

replace ansible docker_compose module with command #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions operations/deployment/ansible/tasks/docker_cleanup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@
ignore_errors: true

- name: Stop and cleanup Docker
docker_compose:
project_src: "{{ app_install_root }}/{{ app_repo_name }}"
state: absent
remove_orphans: true
remove_images: all
remove_volumes: true
ansible.builtin.command: "docker compose --project-directory {{ app_install_root }}/{{ app_repo_name }} down --remove-orphans --rmi all --volumes"
register: output
when: docker_check.rc == 0

- name: Prune Docker system
command: docker system prune --all --force --volumes
when: docker_check.rc == 0
when: docker_check.rc == 0
7 changes: 2 additions & 5 deletions operations/deployment/ansible/tasks/ec2_cleanup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
register: folder_stat

- name: Stop Docker
docker_compose:
project_src: "{{ app_install_root }}/{{ app_repo_name }}"
state: present
stopped: true
ansible.builtin.command: "docker compose --project-directory {{ app_install_root }}/{{ app_repo_name }} stop"
when: folder_stat.stat.exists

- name: Find the NFS volume in fstab
Expand Down Expand Up @@ -46,4 +43,4 @@
format: gz
force_archive: true
remove: true
when: folder_stat.stat.exists
when: folder_stat.stat.exists
4 changes: 1 addition & 3 deletions operations/deployment/ansible/tasks/install.yml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would change it to be

# install docker
- name: Install required system packages
  apt: name={{ item }} state=latest update_cache=yes
  loop:
    - 'apt-transport-https'
    - 'ca-certificates'
    - 'curl'
    - 'software-properties-common'
    - 'python3-pip'
    - 'virtualenv'
    - 'python3-setuptools'
    
- name: Create the keyrings folder
  file:
    path: /etc/apt/keyrings
    state: directory
    mode: '0755'

- name: Add Docker GPG apt Key
  get_url:
    url: https://download.docker.com/linux/ubuntu/gpg
    dest: /etc/apt/keyrings/docker.asc

- name: Fix Docker key permissions
  file:
    path: /etc/apt/keyrings/docker.asc
    mode: '0666'
    
- name: Get APT package architecture
  command: dpkg --print-architecture
  register: dpkg_arch

- name: Add Docker repo
  apt_repository:
    repo: "deb [arch={{ dpkg_arch.stdout }} signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
    state: present

- name: Update apt repositories
  apt:
    update_cache: yes
  
- name: Install Docker and Docker Compose
  apt:
    name:
      - docker-ce
      - docker-ce-cli
      - containerd.io
      - docker-buildx-plugin
      - docker-compose-plugin

- name: Install Docker Module for Python
  pip:
    name:
      - docker
      - docker-compose

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • name: Install Docker and Docker Compose
    apt:
    name:
    - docker-ce
    - docker-ce-cli
    - containerd.io
    - docker-buildx-plugin
    - docker-compose-plugin

You only need to install docker-ce package, the other modules will come along as a dependency.

  • name: Install Docker Module for Python
    pip:
    name:
    - docker
    - docker-compose

docker-compose python module is no longer used so no need to install.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Just in case and to leave it documented, confirmed this in Ubuntu 18/20/22.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

- name: Add Docker Repository
apt_repository:
repo: deb https://download.docker.com/linux/ubuntu bionic stable
repo: deb https://download.docker.com/linux/ubuntu jammy stable
state: present

- name: Update apt and install docker-ce
Expand All @@ -27,5 +27,3 @@
pip:
name:
- docker
- docker-compose

9 changes: 1 addition & 8 deletions operations/deployment/ansible/tasks/start.yml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docker flag to remove orphans doesn't accept values. So another approach would be this one.

- name: Start docker-compose with remove orphans
  ansible.builtin.command: "docker compose --project-directory {{ app_install_root }}/{{ app_repo_name }} up --detach --build --force-recreate --remove-orphans"
  register: output
  when:  docker_remove_orphans | bool 

- name: Start docker-compose without remove orphans
  ansible.builtin.command: "docker compose --project-directory {{ app_install_root }}/{{ app_repo_name }} up --detach --build --force-recreate"
  register: output
  when:  not ( docker_remove_orphans | bool )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What makes you say that it doesn't accept values? My version 2.21.0 most certainly does work that way. Tested on MacOS and Ubuntu.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though what you say is true about the flag, it doesn't exist in any official docker documentation. (Or at least I couldn't find any)
So, just to follow the standard documented way, would keep it this way.

Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
- name: Start docker-compose
docker_compose:
project_src: "{{ app_install_root }}/{{ app_repo_name }}"
restarted: true
build: yes
recreate: always
nocache: yes
remove_orphans: "{{ docker_remove_orphans }}"

ansible.builtin.command: "docker compose --project-directory {{ app_install_root }}/{{ app_repo_name }} up --detach --build --force-recreate --remove-orphans={{ docker_remove_orphans }}"
register: output

- ansible.builtin.debug:
Expand Down