How to install docker using Ansible-playbook

Ansible is an open-source software platform for automating and configuring computer systems. It is used for configuration management, application deployment, and task automation, and it allows you to manage and control a large number of servers from a single central location. The main purpose of Ansible is to reduce manual operations and automate everything using the YAML code.


In this blog, we will show how to install docker using the ansible-playbook and run a sample Nginx container.
Want to learn Ansible from the beginning visit our Ansible tutorials Ansible

DOCKER is an open-source platform for developing, shipping and running applications. It allows developers to package their applications and dependencies into containers, which are lightweight, standalone, and executable units that can be easily deployed and run on any host.
Want to learn more about Docker visit our previous tutorials Docker blogs

NGINX is a free and open-source web server that is known for its high performance, stability, and low resource consumption. It is often used as a reverse proxy, load balancer, and HTTP cache, and it is widely used by some of the busiest websites in the world.
visit our previous blog to learn how to deploy a sample Nginx image as a container
How to run docker images as a container

Prerequisites:

1: Need to install Ansible on the server. Follow this blog in order to install an ansible on Ubuntu 20.04.
https://www.linuxlearninghub.com/how-to-install-and-configuring-ansible-on-ubuntu-20-04/.

We are using the ansible playbook to install and start the docker engine and CLI and also pull a sample Nginx image from the docker hub and run it as a container in the host

The playbook

This is an example of an Ansible playbook that can be used to install and run a Nginx web server as a Docker container on a remote host. The playbook is composed of multiple tasks, each defined by a “name” field.

In an Ansible playbook, the “hosts” field specifies the managed nodes on which the playbook will be executed. The host’s field can contain a list of hostnames or IP addresses, or it can specify a group of hosts defined in the Ansible inventory. In this playbook, it runs against the localhost

---
# This playbook installs nginx as a Docker container on a remote host and mounts a volume

- hosts: localhost
  become: yes
  tasks:
    - name: Install Docker Engine
      apt:
        name: docker.io
        state: present
    - name: Install Docker CLI
      apt:
        name: docker-compose
        state: present
    - name: Start Docker service
      service:
        name: docker
        state: started
        enabled: true
    - name: Pull nginx Docker image
      docker_image:
        name: nginx
        source: pull
    - name: Create volume
      docker_volume:
        name: nginx-data
        driver: local
    - name: Run nginx Docker container
      docker_container:
        name: nginx
        image: nginx
        state: started
        ports:
          - "80:80"
        volumes:
          - "nginx-data:/usr/share/nginx/html:ro"
  • It’s important to note that the playbook is using the become: yes option, which means that the tasks will be executed with root permissions.
  • The first task, “Install Docker Engine“, installs the Docker Engine package using the ansible apt module. The package’s state is set to “present” to ensure that it is installed.
  • The second task, “Install Docker CLI”, installs the Docker Compose package using the “apt” module. The package’s state is set to “present” to ensure that it is installed.
  • The third task, “Start Docker service“, starts the Docker service and sets it to be automatically started at boot time using the “service” module.
  • The fourth task, “Pull nginx Docker image“, pulls the official Nginx Docker image from the Docker Hub registry using the “docker_image” module.
  • The fifth task, “Create volume“, creates a Docker volume named “nginx-data” using the “docker_volume” module with a driver as local.
  • The final task, “Run nginx Docker container“, runs a Docker container using the “docker_container” module. The container is based on the official Nginx image pulled in the previous task, and is named “nginx”. The container is set to be in the “started” state and exposes port 80. Additionally, it mounts a volume “nginx-data” to the container’s “/usr/share/nginx/html” directory as read-only.

Next, we need to create the ansible-playbook in our server using vi editor.

vi docker-nginx.yaml

After creating the YAML file and inserting the ansible code, we can run the playbook as the root user.

ansible-playbook docker-nginx.yaml

After the successful completion of the playbook, we can see a similar output as below.

Next thing we can verify whether docker has been installed on the system using systemctl command.

Ansible playbook debug option you can use the -v option also use the -vvv option to increase the verbosity further ie ansible-playbook docker-nginx.yaml -vvv

systemctl status docker.service

Also, I verified the creation of the Nginx image and Nginx container using docker-ps and docker images commands in order to check whether my Nginx container was running successfully as expected.

Next, you can just enter your IP address on Chrome and you will see the default Nginx page which confirms that the Nginx container is running successfully.

Summary:
In this blog, we have learned how to install docker and run Nginx image as a container using ansible-playbook.

Leave a Reply

Your email address will not be published. Required fields are marked *