Docker

Using Ansible to Install and Configure Docker

Server automation is a system administrator’s most influential and valuable skill. It removes the need to repeat a set of annoying and time-consuming tasks to over a dozen machines during configuration.

Luckily, with the invention of tools such as Ansible, the processing of automating the server setup and configuration, especially new machines, has become streamlined and efficient. This also removes the chance of human errors when configuring new systems.

In this tutorial, we will learn how to use Ansible to automate the process of setting up a new Debian server and installing Docker with ease.

Requirements:

Before proceeding, you need to ensure that you have the following:

  1. A target Debian-based system
  2. An Ansible controller with the hosts to the target system already configured
  3. Basics of writing and executing Ansible playbooks
  4. Sufficient permissions on the target machine

NOTE: This tutorial does not cover the basics of either Docker or Ansible. You need to ensure that you have both of these tools set up in your host machine before proceeding.

Step 1: Prepare the Playbook

The first step is to create a file to store the playbook definition. For universality, we name the file “playbook.yml” in the current working directory. Feel free to choose any directory and filename that you deem appropriate for your project.

$ cd /home/ubuntu
$ touch playbook.yml

We write all our Ansible tasks in this playbook file and execute them upon completion. An Ansible playbook is a small unit of work that we can automate using Ansible.

Once created, add the following entries to the playbook file:

---
- hosts
: all
become
: true

The given declarations are very universal across Ansible playbooks. The first directive tells Ansible which hosts we wish to target. This should be easy to understand if you know how to configure the Ansible hosts.

Since we only have one host in the Ansible inventory, we target all the hosts in the playbook.

The last block tells Ansible that we wish to execute all the commands that are defined in the playbook as root. This is essential as we will install the packages on the target machine.

Step 2: Install the Required Packages

The next step is to tell Ansible to install the tools that are required to install and run the Docker on a Debian system. In this case, we use the “apt” module provided by Ansible Unix tools. You can check our tutorial on Ansible “apt” to learn more.

Add the following entries to the playbook file:

- name: Install the required packages
      apt
:
        pkg
:
         - apt-transport-https
          - ca-certificates
          - curl
          - software-properties-common
          - python3-pip
          - python3-setuptools
        state
: latest
        update_cache
: true

In this case, we tell Ansible that we wish to install the “apt-transport-https”, “ca-certificates”, “curl”, “software-properties-common”, “python3-pip”, and “python3-setuptools” on the system.

Step 3: Install Docker

As you can guess, the final step is to tell Ansible to install Docker on the host system. We start by fetching the Docker GPG key to verify the download. We then add the official repository as a new package source and use it to install Docker.

Add the following entries to accomplish the installation:

- name: Add Docker GPG apt Key
      apt_key
:
        url
: https://download.docker.com/linux/debian/gpg
        state
: present
    - name
: Add Docker Repository
      apt_repository
:
        repo
: deb https://download.docker.com/linux/debian   bookworm stable
        state
: present
  - name
: Update APT and install Docker and Tools.
      apt
:
        pkg
:
         - docker-ce
          - docker-ce-cli
          - containerd.io
          - docker-buildx-plugin
          - docker-compose-plugin
        state
: latest
        update_cache
: true

This should set up the Docker Engine and all the associated tools.

Step 4: Review Final Playbook

Finally, as shown in the following, you should have a playbook to install and configure Docker on your Debian system:

---
- name
: Install and Configure Docker
  hosts
: all
  become
: yes
  tasks
:
    - name
: Install the required packages
      apt
:
        name
:
         - apt-transport-https
          - ca-certificates
          - curl
          - software-properties-common
          - python3-pip
          - python3-setuptools
        state
: latest
        update_cache
: true

    - name
: Add Docker GPG apt Key
      apt_key
:
        url
: https://download.docker.com/linux/debian/gpg
        state
: present

    - name
: Add Docker Repository
      apt_repository
:
        repo
: deb [arch=amd64] https://download.docker.com/linux/debian bookworm stable
        state
: present

    - name
: Update APT and install Docker and Tools
      apt
:
        name
:
         - docker-ce
          - docker-ce-cli
          - containerd.io
          - docker-buildx-plugin
          - docker-compose-plugin
        state
: latest
        update_cache
: true

Step 5: Run the Playbook

With all the changes configured, run the following command to execute your playbook and setup the defined tasks in the playbook:

$ ansible-playbook playbook.yml

This should run all the defined tasks in the playbook and install Docker on your system.

Conclusion

In this post, we discussed how we can easily use Ansible to automate the process of installing and configuring Docker on a Debian-based system.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list