How to install Docker Engine in Debian 11 using Ansible

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.

Ansible can be used to provision the underlying infrastructure of your environment, virtualized hosts and hypervisors, network devices, and bare metal servers. It can also install services, add compute hosts, and provision resources, services, and applications inside of your cloud.

In this guide we will learn how to install docker using ansible on a Debian 11 instance.

Related content:

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: Update apt repositories and cache on server
  apt:
    update_cache: yes
    force_apt_get: yes
    cache_valid_time: 3600

- name: Upgrade all packages on server
  apt:
    upgrade: dist
    force_apt_get: yes

The above tasks are similar to doing an apt-get update before finally doing apt-get dist-upgrade.

2. Uninstall old docker versions

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

- name: Ensure older versions of docker are uninstalled
  apt:
    name:
      - docker
      - docker-engine
      - docker.io
      - containerd
      - runc
    state: absent

3. Set up docker repository

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

First we need to ensure that packages that allow apt to use a repository over https are installed.

- name: Install packages to allow apt to use a repository over HTTPS
  apt:
    name:
      - ca-certificates
      - curl
      - gnupg
      - lsb-release
    state: latest

Next, add docker’s official pgp key

- name: Add Dockers official GPG key
  shell: |
    mkdir -p /etc/apt/keyrings
    curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Finally set up the repository

    - name: Set up the repository
      shell: |
        echo \
          "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
          $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

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 Docker Engine
  apt:
    name:
      - docker-ce
      - docker-ce-cli
      - containerd.io
      - docker-compose-plugin
    state: present
    update_cache: yes
  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: admin
    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 Debian 11. I have it saved as docker-debian.yaml.

---
- name: Install docker in Debian 11
  hosts: debiansrv-one
  gather_facts: false
  become: true
  tasks:
    - name: Update apt repositories and cache on server
      apt:
        update_cache: yes
        force_apt_get: yes
        cache_valid_time: 3600

    - name: Upgrade all packages on server
      apt:
        upgrade: dist
        force_apt_get: yes

    - name: Ensure older versions of docker are uninstalled
      apt:
        name:
          - docker
          - docker-engine
          - docker.io
          - containerd
          - runc
        state: absent

    - name: Install packages to allow apt to use a repository over HTTPS
      apt:
        name:
          - ca-certificates
          - curl
          - gnupg
          - lsb-release
        state: latest

    - name: Add Dockers official GPG key
      shell: |
        mkdir -p /etc/apt/keyrings
        curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg

    - name: Set up the repository
      shell: |
        echo \
          "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
          $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

    - name: Install the Docker Engine
      apt:
        name:
          - docker-ce
          - docker-ce-cli
          - containerd.io
          - docker-compose-plugin
        state: present
        update_cache: yes
      notify:
        - Start and Enable docker

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

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

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:
    debiansrv-one:
      ansible_ssh_host: 10.2.11.10
      ansible_ssh_user: admin
      ansible_ssh_private_key_file: ~/.ssh/devops.pem

We can finally run the playbook with this command:

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