Jarle Aase

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

    - 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
        dest: /root/
        mode: 0700
      when: docker_check.stderr.find('service could not be found') != -1

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

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

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

    - name: Start the Docker daemon
        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
        dest: /usr/bin/docker-compose
        mode: 0755
        - 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!