Published:

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)!

 1---
 2
 3- name: Install Docker and Docker Compose on CentOS
 4  hosts: all
 5  become: true
 6
 7  tasks:
 8    - name: Upgrade all packages
 9      yum: name=* state=latest
10
11    - name: Check if Docker is installed
12      command: systemctl status docker
13      register: docker_check
14      ignore_errors: yes
15
16    - name: Download the Docker installer
17      get_url:
18        url: https://get.docker.com/
19        dest: /root/install_docker.sh
20        mode: 0700
21      when: docker_check.stderr.find('service could not be found') != -1
22
23    - name: Install Docker
24      shell: /root/install_docker.sh
25      when: docker_check.stderr.find('service could not be found') != -1
26
27    - name: Remove the Docker installer file.
28      file:
29        state: absent
30        path: /root/install_docker.sh
31
32    - name: Enable the Docker daemon in systemd
33      systemd:
34        name: docker
35        enabled: yes
36        masked: no
37
38    - name: Start the Docker daemon
39      systemd:
40        name: docker
41        state: started
42        masked: no
43
44    - name: Check if Docker Compose is installed
45      command: docker-compose --version
46      register: docker_compose_check
47      ignore_errors: yes
48
49    - name: Download and install Docker Compose
50      get_url:
51        url: https://github.com/docker/compose/releases/download/1.21.2/docker-compose-Linux-x86_64
52        dest: /usr/bin/docker-compose
53        mode: 0755
54      when:
55        - docker_compose_check.msg is defined
56        - 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!