Skip to content

Instantly share code, notes, and snippets.

@srijan
Last active February 10, 2024 04:17
Show Gist options
  • Save srijan/2028af568459195cb9a3dae8d111e754 to your computer and use it in GitHub Desktop.
Save srijan/2028af568459195cb9a3dae8d111e754 to your computer and use it in GitHub Desktop.
Install docker and docker-compose using ansible https://srijan.ch/install-docker-and-docker-compose-using-ansible
---
- name: Install packages using apt
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- gnupg2
- software-properties-common
state: present
cache_valid_time: 86400
- name: Add Docker GPG apt Key
apt_key:
url: "{{ docker_apt_gpg_key }}"
state: present
- name: Add Docker Repository
apt_repository:
repo: "{{ docker_apt_repository }}"
state: present
update_cache: true
- name: Install docker-ce
apt:
name: docker-ce
state: present
cache_valid_time: 86400
- name: Install docker compose
apt:
name: docker-compose-plugin
state: present
cache_valid_time: 86400
- name: Run and enable docker
service:
name: docker
state: started
enabled: true
- name: Install docker-compose v1
when:
- docker_compose_install_v1 is defined
- docker_compose_install_v1
block:
- name: Check current docker-compose version.
command: docker-compose --version
register: docker_compose_vsn
changed_when: false
failed_when: false
check_mode: no
- name: Set docker compose current version
set_fact:
docker_compose_current_version: "{{ docker_compose_vsn.stdout | regex_search('(\\d+(\\.\\d+)+)') }}"
when:
- docker_compose_vsn.stdout is defined
- name: Install or upgrade docker-compose
get_url:
url : "https://github.com/docker/compose/releases/download/{{ docker_compose_version_v1 }}/docker-compose-Linux-x86_64"
dest: /usr/local/bin/docker-compose
mode: 'a+x'
force: yes
when: >
docker_compose_current_version == ""
or docker_compose_current_version is version(docker_compose_version_v1, '<')
---
docker_apt_release_channel: stable
docker_apt_arch: amd64
docker_apt_repository: "deb [arch={{ docker_apt_arch }}] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} {{ docker_apt_release_channel }}"
docker_apt_gpg_key: https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg
docker_compose_install_v1: true
docker_compose_version_v1: "1.29.2"
@jaydrogers
Copy link

docker_compose_current_version == ""

This helped me out a ton (especially for new server provisions that do not have docker-compose installed yet). Thanks @schaerli!

@NurdinDev
Copy link

Thank you @srijan for your article, I faced an issue during the Install or upgrade docker-compose step
I got this error Destination /usr/local/bin/docker-compose is not writable after some researches about the issue I tried to add become: true to this step and it's worked for me.
so is it working with you without become?

@srijan
Copy link
Author

srijan commented Jan 16, 2022

@NurdinDev - I have set become: true in the main file where this task is imported. It's required for all the steps here, not just the compose step.

@NurdinDev
Copy link

I got it @srijan, Thank you so much 👍

@scottix
Copy link

scottix commented Apr 26, 2022

Nice script, I needed to change the get_url section and added the checksum.

- name: Install or upgrade docker-compose
  become: true
  get_url:
    url: "https://github.com/docker/compose/releases/download/v{{ docker_compose_version }}/docker-compose-linux-x86_64"
    checksum: "sha256:https://github.com/docker/compose/releases/download/v{{ docker_compose_version }}/docker-compose-linux-x86_64.sha256"
    dest: /usr/local/bin/docker-compose
    mode: 'a+x'
    force: yes
  when: >
    docker_compose_current_version is not defined
    or docker_compose_current_version is version(docker_compose_version, '<')

@chamalis
Copy link

chamalis commented Nov 18, 2023

There are several separate issues with this gist. Failing in clean debian 10.4. Issues found:

  1. -name: Install packages using apt
    fails because of update_cache: no

  2. "-name: setup-servers : Install or upgrade docker-compose"

fails because:

    docker_compose_current_version is not defined

should be:

- docker_compose_current_version == ""
    • name: Run and enable docker

fails with:

No module named 'requests'
fatal: [vm01]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (Docker SDK for Python: docker>=5.0.0 (Python >= 3.6) or docker<5.0.0 (Python 2.7))

because python3-requests also needs to be installed

  1. When using docker-compose:
 ModuleNotFoundError: No module named 'docker' 

@srijan
Copy link
Author

srijan commented Nov 20, 2023

@chamalis - thanks. I plan to update the gist and linked blog post with a more complete solution. These days docker compose v2 is available via apt as well, so no need to download from github releases anymore if you don't need v1.

@srijan
Copy link
Author

srijan commented Nov 21, 2023

@chamalis - Updated, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment