DEV Community

Daniel Pepuho
Daniel Pepuho

Posted on • Updated on

Install Docker on Remote Server using Ansible

Long time no see, now i will show you how to install Docker using Ansible Playbook. Let's start!!!

Setup

First, you need to install Ansible. Just follow this link to install Ansible on your operating system installation guide.

After installation, create new directory called ansible-docker.

$ mkdir ansible-docker && cd ansible-docker
Enter fullscreen mode Exit fullscreen mode

Create a new file called ansible.cfg as the Ansible configuration setting and then define the inventory file.

[defaults]
inventory = hosts
host_key_checking = True
deprecation_warnings = False
collections = ansible.posix, community.general
Enter fullscreen mode Exit fullscreen mode

Then create a new file called hosts, where the name is defined on ansible.cfg.

[example-server]
0.0.0.0 ansible_user=root
Enter fullscreen mode Exit fullscreen mode

NB: Don't forget to change the IP Address and host name.

After setup the Ansible configuration setting & inventory file, let's create a YAML called playbook.yml

---
- name: Setup Docker on Ubuntu Server 22.04
  hosts: all
  become: true
  remote_user: root
  roles:
    - config
    - docker
Enter fullscreen mode Exit fullscreen mode

Then create roles directory:

  • Config, On this directory I will create a directory called tasks. After that, I should create yaml file called main.yml to run update, upgrade & install many dependencies.
---
- name: Update&Upgrade
  ansible.builtin.apt:
    name: aptitude
    state: present
    update_cache: true

- name: Install dependencies
  ansible.builtin.apt:
    name:
      - net-tools
      - apt-transport-https
      - ca-certificates
      - curl
      - software-properties-common
      - python3-pip
      - virtualenv
      - python3-setuptools
      - gnupg-agent
      - autoconf
      - dpkg-dev
      - file
      - g++
      - gcc
      - libc-dev
      - make
      - pkg-config
      - re2c
      - wget
    state: present
    update_cache: true
Enter fullscreen mode Exit fullscreen mode
  • Docker, On this directory create 2 directories called tasks & templates.

On tasks directory create new file called main.yml. This file contains Docker installation, Docker Compose installation & private registry setup.

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

- name: Add repository into sources list
  ansible.builtin.apt_repository:
    repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_lsb.codename }} stable
    state: present
    filename: docker

- name: Install Docker 23.0.1-1
  ansible.builtin.apt:
    name:
      - docker-ce=5:23.0.1-1~ubuntu.22.04~jammy
      - docker-ce-cli=5:23.0.1-1~ubuntu.22.04~jammy
      - containerd.io
    state: present
    update_cache: true

- name: Setup docker user
  ansible.builtin.user:
    name: docker
    groups: "docker,sudo"
    append: true

- name: Install Docker module for Python
  ansible.builtin.pip:
    name: docker

- name: Install Docker-Compose&Set Permission
  ansible.builtin.get_url:
    url: https://github.com/docker/compose/releases/download/1.29.2/docker-compose-Linux-x86_64
    dest: /usr/local/bin/docker-compose
    mode: '755'

- name: Create Docker-Compose symlink
  ansible.builtin.command:
    cmd: ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
    creates: /usr/bin/docker-compose

- name: Add private registry
  ansible.builtin.template:
    src: daemon.j2
    dest: /etc/docker/daemon.json
    mode: preserve

- name: Restart Docker
  ansible.builtin.service:
    name: docker
    state: restarted
    enabled: true
Enter fullscreen mode Exit fullscreen mode

In the template, create a template file using a jinja file named daemon.j2. This file contains configuration for private registry settings (optional).

{
    "insecure-registries" : ["http://0.0.0.0:5000"]
}
Enter fullscreen mode Exit fullscreen mode

NB: Field the IP using your remote server private IP

After all setup, Your project directory should look like this:

$ tree
.
├── ansible.cfg
├── config
│   └── tasks
│       └── main.yml
├── docker
│   ├── tasks
│   │   └── main.yml
│   └── templates
│       └── daemon.j2
├── hosts
└── playbook.yml
Enter fullscreen mode Exit fullscreen mode

Test & Run

Okay, now test Your playbook.yml file using this command.

$ ansible-playbook --syntax-check playbook.yml
Enter fullscreen mode Exit fullscreen mode

If You don't have any errors, run the playbook using this command.

$ ansible-playbook -i hosts playbook.yml
Enter fullscreen mode Exit fullscreen mode

Output, wait until finish.

 ____________________________________________
< PLAY [Setup Docker on Ubuntu Server 22.04] >
 --------------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

 ________________________
< TASK [Gathering Facts] >
 ------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Enter fullscreen mode Exit fullscreen mode

Conclusion

In this post, I just show you how to install Docker in a specific version using Ansible Playbook when you have one or more servers.

Thank You for reading this post, If You have suggestions or questions please leave them below. Thanks

NB: In this case, I just set the user as root. I installed the Docker on Ubuntu Server 22.04. For full code follow this link ansible-docker.

Top comments (0)