Using Ansible to install and configure docker In Rocky Alma Linux 8

Docker is an open source containerization platform. It enables developers to package applications into containers—standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment.

In this guide we will learn how to install docker using ansible on a Rocky Linux 8 instance.

Table of Content

  1. Ensuring that the server is updated
  2. Uninstall old docker versions
  3. Set up docker repository
  4. Install the latest version of docker and containerd
  5. Add the current user to the docker group
  6. Set up handlers
  7. Full playbook
  8. Run the playbook

1. Ensure that the server packages are updated

It is always a good practice to ensure that the server packages are up dated. Use this task to achieve that:

- name: Upgrade all packages on servers
  dnf:
    name: '*'
    state: latest

2. Uninstall old docker versions

If older versions of docker are installed in the system, uninstall them, along with associated dependencies.

- name: Uninstall old versions
  dnf:
    name:
      - docker-client
      - docker-client-latest
      - docker-common
      - docker-latest
      - docker-latest-logrotate
      - docker-logrotate
      - docker-engine
    state: absent

3. Set up docker repository

The docker packages are not found in the default Rocky Linux repositories. You need to set up the Docker repository when installing docker in a new system.

Install the yum-utils package (which provides the yum-config-manager utility) and set up the stable repository.

- name: Set up docker repository
  shell: |
    dnf install -y yum-utils
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  args:
    warn: no

4. Install the latest version of Docker Engine and containerd

Once the repositories have been added, we can install the docker engine. This task will also install the command line client and the containerd engine.

We are also notifying a handler to

Start and enable docker. The handlers will be run after the tasks are completed.
- name: Install the latest version of Docker Engine and containerd
  dnf:
    name:
      - docker-ce
      - docker-ce-cli
      - containerd.io
  notify: Start and enable docker

5. Add the current user to the docker group

Docker will not work for no-root users or without sudo. To use docker without Sudo, add the current user to the docker group.

- name: Add the current user to the docker group
  user:
    name: rocky
    groups: docker
    append: yes

6. Set up handlers

Handlers are tasks that only run when a change is mad. We are using handlers in our playbook to start and enable the docker service on boot.

- name: Start and enable docker
  systemd:
    name: docker
    state: started
    enabled: yes

7. Full Playbook

This is the full playbook to install and set up docker in Rocky Linux 8. I have it saved as

docker-rocky.yaml.

---
- name: Install docker in Rocky
  hosts: rockylinuxsrv
  gather_facts: false
  become: true
  tasks:
    - name: Upgrade all packages on servers
      dnf:
        name: '*'
        state: latest

    - name: Uninstall old versions
      dnf:
        name:
          - docker-client
          - docker-client-latest
          - docker-common
          - docker-latest
          - docker-latest-logrotate
          - docker-logrotate
          - docker-engine
        state: absent

    - name: Set up docker repository
      shell: |
        dnf install -y yum-utils
        yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
      args:
        warn: no

    - name: Install the latest version of Docker Engine and containerd
      dnf:
        name:
          - docker-ce
          - docker-ce-cli
          - containerd.io
      notify: Start and enable docker

    - name: Add the current user to the docker group
      user:
        name: rocky
        groups: docker
        append: yes

  handlers:
    - name: Start and enable docker
      systemd:
        name: docker
        state: started
        enabled: yes

8. Run the playbook

To run the playbook you need to have ansible installed. Install ansible with this pip command:

sudo pip install ansible

You also need to define a hosts file with definition to the server to run the tasks against. This is my hosts file. Save it as hosts.yaml

all:
  hosts:
    rockylinuxsrv:
      ansible_ssh_host: 10.2.11.10
      ansible_ssh_user: rocky
      ansible_ssh_private_key_file: ~/.ssh/devops.pem

We can finally run the playbook with this command:

ansible-playbook -i hosts.yaml docker-rocky.yaml -vv
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy