Skip to content

How to install docker and docker-compose on your Raspberry Pi with Ansible

raspberry pi docker ansible image

Recipe on How to Install docker and docker-compose on your Raspberry Pi with Ansible.

We saw how to manually install docker and docker-compose on the Raspberry Pi in our last post. If you are using ansible, you can benefit from this recipe that does exactly the same but in an automated way.

Prerequisites:

Installation and configuration of Ansible is out of the scope of this post. Anyway we need some prerequisites in order to execute this playbook.

  • Ansible working installation for the control node.
  • Ansible inventory file with the hosts where we want to install docker and docker-compose.
  • Public Key Infrastructure (PKI) between control and managed nodes, see notes below.

Playbook:

- hosts: all
  become: yes

  vars:
    pkgstoinstall: [ libffi-dev, libssl-dev, python3, python3-pip ]

  tasks:
    - name: Install a list of packages
      apt:
        name: "{{ pkgstoinstall }}"
        update_cache: yes

    - name: Remove python-configparser package
      apt:
        name: python-configparser
        state: absent

    - name: get docker convenience script
      shell: curl -fsSL https://get.docker.com -o get-docker.sh
      args:
        creates: /home/pi/get-docker.sh

    - name: install docker
      shell: sh /home/pi/get-docker.sh
      args:
        creates: /usr/bin/docker

    - name: make pi user execute docker commands
      shell: usermod -aG docker pi

    - name: install docker-compose
      shell: pip3 -v install docker-compose
      args:
        creates: /usr/local/bin/docker-compose

You can execute this playbook with the following command:

ansible-playbook -i <inventory_file> <playbook_file.yml> -upi

Notes:

  • Inventory file: this playbook assumes that you use an <inventory_file> only with the hosts where you want to execute it. If you use a default inventory file, you can omit the ‘-i’ part and select hosts from inventory using patterns. You can do this changing line 1 of the playbook or selecting hosts in the command line with ‘-l’. Find out more about patterns in the documentation.
  • Connection user and privilege escalation: the command assumes that you are using the pi user to connect to managed nodes, that the home directory exists for that user, that PKI is used to connect to the pi user and that the user pi has passwordless sudo privileges to execute commands as root. In case you want to use another user you should change ‘-upi’ in the command and change lines 21, 24 and 29 in the playbook to reflect the different home directory and user. Also, If no PKI is used for the user you want to connect with, you can add ‘-k’ (lowercase k) in the command line for ansible to ask for this password. In the event that no passwordless sudo is configured for the selected user in the managed node you can tell ansible to ask for the sudo password using the ‘-K’ (uppercase K) flag. That user still need sudo privileges to execute commands as root. More information here.

That’s it. After the execution of the playbook you will have a working docker and docker-compose installation. You can validate it checking the version with the commands ‘docker –version’ and ‘docker-compose –version’. Enjoy.

Leave a Reply

Your email address will not be published. Required fields are marked *