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 18.04 Bionic, 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:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present

    - name: ensure docker registry is available
      apt_repository: repo='deb https://download.docker.com/linux/ubuntu bionic stable' state=present

    - name: ensure docker and dependencies are installed
      apt: name=docker-ce 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.


Releaseworks Academy has a free online training course on Docker & Jenkins best practices: https://www.releaseworksacademy.com/courses/best-practices-docker-jenkins