Get into DevOps is a blog about methodologies, practices and tools to improve software delivery.

Installing Docker on Ubuntu with Ansible

Installing Docker on Ubuntu with Ansible

In your efforts to run hosts for Docker containers, you will inevitably come across the need to install Docker automatically via configuration management.

Here is a quick Ansible playbook to install Docker on Ubuntu 14.04 or 16.04 LTS, and optionally, enable Docker to use insecure registries in a defined IP address or range:

- hosts: all
  become: true

  tasks:
    - name: ensure repository key is installed
      apt_key:
        id: "58118E89F3A912897C070ADBF76221572C52609D"
        keyserver: "hkp://p80.pool.sks-keyservers.net:80"
        state: present

    - name: ensure docker registry is available
      # For Ubuntu 14.04 LTS, use this repository:
      apt_repository: repo='deb https://apt.dockerproject.org/repo ubuntu-trusty main' state=present
      # For Ubuntu 16.04 LTS, use this repo instead:
      # apt_repository: repo='deb https://apt.dockerproject.org/repo ubuntu-xenial main' state=present

    - name: ensure docker and dependencies are installed
      apt: name=docker-engine update_cache=yes

    # Uncomment the following to enable insecure registries with Docker
    #- name: ensure docker can use insecure registries in 10.11.0.0/16
    #  lineinfile: "dest=/etc/default/docker regexp=^DOCKER_OPTS line=DOCKER_OPTS='--insecure-registry 10.11.0.0/16'"

    - service: name=docker state=restarted

Please note, though, that using insecure registries is made difficult on purpose. You should opt for using a secured registry whenever possible.

Now that you have Docker installed, why not spin up some containers with Ansible?

Building a DevOps organisation

Building a DevOps organisation

Launching Docker containers with Ansible

Launching Docker containers with Ansible