Skip to content

Centos 7 docker

What ?

This is my Ansible playbook to automatically replace docker ( if installed from yum ) update to latest stable docker-ce version and add latest docker-compose.


I have also implemented fix that solve different runtime on containers if old docker was used. Usually this happens when you had old docker installed and upgraded to latest docker-ce version. Than try to start your old containers and get: Error response from daemon: Unknown runtime specified docker-runc

Why ?

I had to update couple of Centos 7 servers that run old ( very very old ) docker. So this was kind of shortcut ? I spend more time making sure this playbook works that it would take to do it manually. 🙂

But thats mostly because I have tried to use git module to get the latest version number of docker-compose... this of course does not work with python 2.7. Switching the playbook to python 3+ broke the package / yum module and suggested dnf module also did not work on Centos 7. Honestly, this is my biggest love/hate with Ansilbe. On one hand you can do so much quite easily and its maintainable... on the other hand, we known that python 2 will be obsolete for years and Ansible kinda said meh... it will sort it self out eventually... Now we have modules that work under 2.7 up to some version, some now do not support Python 2 at all. Switching to Python 3 breaks stuff. In the end Python 2 will not be even installed anymore so go ahead and create playbooks that will work in every possible situation, going back to shell scripts suddenly does not look as such a bad idea. 🙂

How ?

Copy the yaml file into some file where you have Ansible installed, lets say main.yaml

Change: hosts, remote_user to match your target and run it.

vladoportos@Odin:~/ansible/docker-ce$ ansible-playbook main.yml


- name: Install Docker-ce Centos 7
  remote_user: vladoportos
  become: true
  become_method: sudo
  gather_facts: false

    - name: Remove docker if installed from CentOS repo
          - docker
          - docker-client
          - docker-client-latest
          - docker-common
          - docker-latest
          - docker-latest-logrotate
          - docker-logrotate
          - docker-engine
        state: absent

    - name: Install required pkgs
          - yum-utils
          - device-mapper-persistent-data
          - lvm2
        state: latest

    - name: Add Docker repo
        dest: /etc/yum.repos.d/docker-ce.repo

    - name: Install Docker
          - docker-ce
          - docker-ce-cli
        state: latest

    - name: Convert old images to new runtime
        cmd: " grep -rl 'docker-runc' /var/lib/docker/containers/ | xargs sed -i 's/docker-runc/runc/g' "
      changed_when: false
      failed_when: false

    - name: Start Docker service
        name: docker
        state: started
        enabled: yes

    - name: Get version number of latest docker-compose
        cmd: curl --silent '' |  grep '"tag_name":' | cut -d'"' -f4
      register: docker_compose_latest_svn
      changed_when: false
        warn: no

    - set_fact:
        docker_compose_latest: "{{ docker_compose_latest_svn.stdout }}"
        - docker_compose_latest_svn.stdout is defined

    - name: Check if /usr/local/bin/docker-compose exists.
        path: /usr/local/bin/docker-compose
      register: docker_compose_file

    - name: Check current docker-compose version
        cmd: /usr/local/bin/docker-compose --version | cut -d ' ' -f3 | sed 's/.$//'
      register: docker_compose_vsn
      changed_when: false
      failed_when: false
        warn: no
        - docker_compose_file.stat.exists

    - set_fact:
        docker_compose_current_version: "{{ docker_compose_vsn.stdout }}"
        - docker_compose_vsn.stdout is defined and docker_compose_file.stat.exists

    - name: Install or upgrade docker-compose
        url : "{{ docker_compose_latest }}/docker-compose-Linux-x86_64"
        dest: /usr/local/bin/docker-compose
        mode: 'a+x'
        force: yes
      when: >
        docker_compose_current_version is not defined
        or docker_compose_current_version is version(docker_compose_latest, '<')

Last update: March 22, 2021