Published:

Updated:

How to install Docker on CentOS with Ansible

bookmark 1 min read

I am playing with Kubernetes and Docker Swarm's these days. Usually I use Debian as my Server Operating System. It's easy to install Docker on Debian, with or without Ansible.

However, today I had to use CentOS for a change. So I made a very simple Ansible Playbook to install Docker and Docker Compose.

Note: If you use this Playbook, please make sure to update the url in Download and install Docker Compose to the current version of Docker Compose. (I really don't understand why the Docker guys don't add Docker Compose to their apt/yum repositories. They already have docker there, so they know how to do it)!

---

- name: Install Docker and Docker Compose on CentOS
  hosts: all
  become: true

  tasks:
    - name: Upgrade all packages
      yum: name=* state=latest

    - name: Check if Docker is installed
      command: systemctl status docker
      register: docker_check
      ignore_errors: yes

    - name: Download the Docker installer
      get_url:
        url: https://get.docker.com/
        dest: /root/install_docker.sh
        mode: 0700
      when: docker_check.stderr.find('service could not be found') != -1

    - name: Install Docker
      shell: /root/install_docker.sh
      when: docker_check.stderr.find('service could not be found') != -1

    - name: Remove the Docker installer file.
      file:
        state: absent
        path: /root/install_docker.sh

    - name: Enable the Docker daemon in systemd
      systemd:
        name: docker
        enabled: yes
        masked: no

    - name: Start the Docker daemon
      systemd:
        name: docker
        state: started
        masked: no

    - name: Check if Docker Compose is installed
      command: docker-compose --version
      register: docker_compose_check
      ignore_errors: yes

    - name: Download and install Docker Compose
      get_url:
        url: https://github.com/docker/compose/releases/download/1.21.2/docker-compose-Linux-x86_64
        dest: /usr/bin/docker-compose
        mode: 0755
      when:
        - docker_compose_check.msg is defined
        - docker_compose_check.msg.find('No such file or directory') != -1

This should work on RedHat and probably on Fedora as well, but I have not tested that.

Have fun!