Loading Posts...

How Install Docker And Docker Compose Using Ansible Playbooks

This was very useful for me in few of my projects which I used Ansible, Docker and Docker Compose, so I thought to share this in a post to show that How to install Docker and Docker Compose using an Ansible Playbook.

In this Ansible playbook I followed below tasks to complete the Docker installation

  • Install docker packages
  • Add Docker s official GPG key
  • Verify that we have the key with the fingerprint
  • Set up the stable repository
  • Update apt packages
  • Install docker
  • Add remote “ubuntu” user to “docker” group
  • Install docker-compose

Read More: Stern Aggregated Multiple Log Tailing Tool For Kubernetes

Here is the complete Ansible Playbook for the Docker Installation

- hosts: all
  become: yes
  gather_facts: false
  tasks:
  - name: Install docker packages
    remote_user: ubuntu
    apt:
      name: "{{ item }}"
      state: present
      update_cache: yes
    with_items:
      - apt-transport-https
      - ca-certificates
      - curl
      - software-properties-common
    tags:
      - docker
  - name: Add Docker s official GPG key
    remote_user: ubuntu
    apt_key:
      url: https://download.docker.com/linux/ubuntu/gpg
      state: present
    tags:
      - docker
  - name: Verify that we have the key with the fingerprint
    remote_user: ubuntu
    apt_key:
      id: 0EBFCD88
      state: present
    tags:
      - docker
  - name: Set up the stable repository
    remote_user: ubuntu
    apt_repository:
      repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable
      state: present
      update_cache: yes
    tags:
      - docker
  - name: Update apt packages
    remote_user: ubuntu
    apt:
      update_cache: yes
    tags:
      - docker
  - name: Install docker
    remote_user: ubuntu
    apt:
      name: docker-ce
      state: present
      update_cache: yes
    #notify: Start docker on boot
    tags:
      - docker
  - name: Add remote "ubuntu" user to "docker" group
    remote_user: ubuntu
    user:
      name: "ubuntu"
      group: "docker"
      append: yes
    tags:
      - docker
  - name: Install docker-compose
    remote_user: ubuntu
    get_url: 
      url : https://github.com/docker/compose/releases/download/1.25.1-rc1/docker-compose-Linux-x86_64
      dest: /usr/local/bin/docker-compose
      mode: 'u+x,g+x'

I hope this will help you for any deployment or automation tasks with Ansible and Docker.

Click to rate this post!
[Total: 11 Average: 4.7]

Aruna Fernando

"Sharing knowledge doesn't put your job at risk - iron sharpen iron" I heard this and it's true.

Get Updates Directly To Your Inbox!

   

Show 2 comments

Leave a Comment

Loading Posts...